How to think in recursive way?

前端 未结 7 1075
逝去的感伤
逝去的感伤 2021-01-30 19:02

In order to understand the advanced algorithm concepts like greedy methods and dynamic programming, one first need to be well versed in recursion.

I am relatively new to

7条回答
  •  情歌与酒
    2021-01-30 19:28

    The CAT problem can be solved like this : (Python code)

    s = "CAT"
    op = []
    def sub(s):
        # terminating condition.
        if (len(s) == 0) : return
    
        if s not in op : op.append(s)
        
        # slices from begning
        sub(s[1:])
        # slices from the end
        sub(s[:-1])
    
    sub(s)
    print(op)
    
    Output : ['CAT', 'AT', 'T', 'A', 'CA', 'C']
    

提交回复
热议问题