Recursion or Iteration?

前端 未结 30 2599
小鲜肉
小鲜肉 2020-11-22 14:44

Is there a performance hit if we use a loop instead of recursion or vice versa in algorithms where both can serve the same purpose? Eg: Check if the given string is a palind

30条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 15:31

    Recursion? Where do I start, wiki will tell you “it’s the process of repeating items in a self-similar way"

    Back in day when I was doing C, C++ recursion was a god send, stuff like "Tail recursion". You'll also find many sorting algorithms use recursion. Quick sort example: http://alienryderflex.com/quicksort/

    Recursion is like any other algorithm useful for a specific problem. Perhaps you mightn't find a use straight away or often but there will be problem you’ll be glad it’s available.

提交回复
热议问题