zoukankan      html  css  js  c++  java
  • 使用递归解决问题

    How do I thoroughly understand Recursive Algorithm and not feel like recursion is magic?

    Recursion is magic! But any sufficiently advanced technology is indistinguishable from magic!

    what is tail-recursion?

    This results in the last statement being in the form of (return (recursive-function params)).

    关于时间复杂度:

    Are 2^n and n*2^n in the same time complexity?

    The Three Laws of Recursion

    Like the robots of Asimov, all recursive algorithms must obey three important laws:

    1. A recursive algorithm must call itself, recursively.
    2. A recursive algorithm must have a base case.
    3. A recursive algorithm must change its state and move toward the base case.

    A base case is the condition that allows the algorithm to stop recursing.

    • A base case is typically a problem that is small enough to solve directly.
    • In the factorial algorithm the base case is n=1.

    We must arrange for a change of state that moves the algorithm toward the base case.

    • A change of state means that some data that the algorithm is using is modified.
    • Usually the data that represents our problem gets smaller in some way.
    • In the factorial n decreases.

    编写代码前的思考:

    1. Break the problem I am trying to solve down into a problem that is one step simpler
    2. Assume that my function will work to solve the simpler problem — really believe it beyond any doubt
    3. Ask myself: Since I know I can solve the simpler problem, how would I solve the more complex problem?

    One common mistake that I see people make when trying to develop a recursive algorithm to solve a problem is that they try to think about how to break the problem down all the way to the base case(递归条件)

    I would like to emphasize that in order to develop the function above, I did not think about how I could break the problem down all the way to the base case. That is the function’s job, not yours. Instead, only thought about the problem that is one step simpler than the problem I am really trying to solve, and then I wrote my recursive algorithm to build up from there to solve the real problem.

    反转字符串 c++:

    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    // 递归函数
    string reverseString(vector<char>& vec){
        // 递归条件 
        if(vec.size()< 2) 
            return string(1,vec[0]); 
    
        // 递归链条 
        vector<char> vec0;
        vec0.push_back(vec[0]);
        vec.erase(vec.begin());
    
        return reverseString(vec) + reverseString(vec0);
    }
    
    int main(){
        vector<char> vec;
        string s = "helloworld";
        for(int i=0;i<(int)s.size();i++){
            vec.push_back(s[i]);
        }
        string ret = reverseString(vec);
        cout << ret << endl;
    
        return 0;
    }
  • 相关阅读:
    慈不掌兵,义不行贾,烂好人难成大业!
    克服焦虑
    静态路由汇总(路由聚合)
    OSPF协议介绍及配置 (下)
    OSPF协议介绍及配置 (上)
    我为什么鼓励工程师写blog
    佛弟子有三样东西需要永远保密
    【交换机在江湖】第十三章 VLAN划分篇
    FileZilla Server ftp 服务器下通过alias别名设置虚拟目录(多个分区)
    Java 对象和类
  • 原文地址:https://www.cnblogs.com/zach0812/p/13658979.html
Copyright © 2011-2022 走看看