Space complexity of recursive algorithm

前端 未结 2 1029
耶瑟儿~
耶瑟儿~ 2021-02-04 13:17

I was asked at an interview, the efficient way to solve a problem checking for pallindrome.

Now i can do two things:

  1. starting from i = 0 to i = n/2 and co
2条回答
  •  自闭症患者
    2021-02-04 13:46

    Have a read at

    1. http://www.codeproject.com/Articles/21194/Iterative-vs-Recursive-Approaches
    2. Recursion or Iteration?

    Basically, a recursive algorithm will add overhead since you store recursive calls in the execution stack.

    But if the recursive function is the last line of the call (tail recursion) then there is no additional penalty.

    That is of course both algorithms are the same.

提交回复
热议问题