剑指offer.C++.code46-50

2018-03-22  本文已影响0人  小异_Summer

46. 圆圈中最后剩下的数【约瑟夫环问题】

// Solution:(1)使用环形链表模拟,时间复杂度O(mn),空间复杂度O(n);
//          (2)直接找出最后一个数字规律
class Solution {
public:
    int LastRemaining_Solution(int n, int m)
    {
        if (n < 1 || m < 1) 
            return -1;
        list<int> numbers;
        for (int i = 0; i < n; i ++) 
            numbers.push_back(i);
        list<int>::iterator cur = numbers.begin();
        while (numbers.size() > 1) {
            for (int i = 1; i < m; i ++) {
                cur ++;
                if (cur == numbers.end())    // 模拟环尾
                    cur = numbers.begin();
            }
            list<int>::iterator deleteIt = cur;
            cur ++;
            if (cur == numbers.end()) 
                cur = numbers.begin();
            numbers.erase(deleteIt);
        }
        return *(cur);
    }
};
// Solution:(1)使用环形链表模拟,时间复杂度O(mn),空间复杂度O(n);
//          (2)直接找出最后一个数字规律,f(n,m) = 0, n=1,时间O(n),空间O(1)
//                                  f(n,m) = [f(n-1, m)+m]%n, n>1
class Solution {
public:
    int LastRemaining_Solution(int n, int m)
    {
        if (n < 1 || m < 1) 
            return -1;
        int last = 0;
        for (int i = 2; i <= n; i ++)
            last = (last + m) % i;
        return last;
    }
};

47. 求1+2+...+n【发散思维】

// Solution:(1)利用构造函数求解
class Temp {
public:
    Temp() { N ++; Sum += N;}
    static void Reset() { N = 0; Sum = 0;}
    static unsigned int GetSum() { return Sum;}
private:
    static unsigned int N;
    static unsigned int Sum;
};

unsigned int Temp::N = 0;
unsigned int Temp::Sum = 0;

class Solution {
public:
    int Sum_Solution(int n) {
        Temp::Reset();
        Temp *a = new Temp[n];
        delete []a;
        a = NULL;
        return Temp::GetSum();
    }
};
// Solution:(2)利用虚函数求解,一个函数递归,一个函数判断终止条件
class A;
A* Array[2];

class A {
    public:
    virtual unsigned int Sum(unsigned int n) {
        return 0;
    }
};

class B: public A {
    public:
    virtual unsigned int Sum(unsigned int n) {
        return Array[!!n]->Sum(n-1) + n;
    }
};

class Solution {
public:
    int Sum_Solution(int n) {
        A a;
        B b;
        Array[0] = &a;
        Array[1] = &b;
        int value = Array[1]->Sum(n);
        return value;
    }
};
// Solution:(3)利用函数指针求解
typedef int (*fun) (int);   
class Solution {
public:
    static int Sum_terminator(int n) {
        return 0;
    }
    static int Sum_Solution(int n) {
        static fun f[2] = {Sum_terminator, Sum_Solution};
        return n + f[!!n](n-1);
    }
};

48. 不用加减乘除做加法

// Solution:位运算
class Solution {
public:
    int Add(int num1, int num2)
    {
        int sum, carry;
        do {
            sum = num1 ^ num2;
            carry = (num1 & num2) << 1;
            
            num1 = sum;
            num2 = carry;
        } while (num2 != 0);
        return num1;
    }
};

49. 把字符串转换成整数

输入描述:
输入一个字符串,包括数字字母符号,可以为空
输出描述:
如果是合法的数值表达则返回该数字,否则返回0
示例1
输入
+2147483647
1a33
输出
2147483647
0

// 考虑:(1)输入""; (2)输入非0-9; (3)输入"+/-"等; (4)溢出
class Solution {
public:
    int StrToInt(string str) {
        int gFlag = -1;
        int len = str.size();
        if (len  == 0) {
            gFlag = 1;
            return 0;
        }
        int minus = 1;
        long long num = 0;
        
        int i = 0;
        if (str[i] == '+') {
            i ++;
        } else if (str[i] == '-') {
            i ++;
            minus = -1;
        }
        
        while (str[i] != '\0') {
            if (str[i] >= '0' && str[i] <= '9') {
                num = num*10 + str[i]-'0';
                i ++;
                if (((minus > 0) && (num > 0x7FFFFFFF)) || 
                    ((minus < 0) && (num > 0x80000000))) {
                    num = 0;
                    gFlag = 2;
                    break;
                }
            } else {
                num = 0;
                gFlag = 3;
                break;
            }
        }
        return (int)minus*num;
    }
};

50. 树中两个结点的最低公共祖先

solution solution solution1
solution2
soluntion3
bool GetNodePath(TreeNode * pRoot, TreeNode * pNode, list<TreeNode *> &path) {
    if (pRoot == pNode) 
        return true;
    path.push_back(pRoot);
    bool found = false;
    vector<TreeNode *>::iterator i = pRoot->children.begin();
    while (! found && i < pRoot->children.end()) {
        found = GetNodePath(*i, pNode, path);
        i ++;
    }
    if (! found) 
        path.pop_back();
    return found;
}

TreeNode * GetLastCommonNode(const list<TreeNode*> &path1, const list<TreeNode*> &path2) {
    list<TreeNode*>::const_iterator iterator1 = path1.begin();
    list<TreeNode*>::const_iterator iterator2 = path2.begin();
    TreeNode* pLast = NULL;
    while (iterator1 != path1.end() && iterator2 != path2.end()) {
        if (*iterator1 == *iterator2)
            pLast = *iterator1;
        iterator1 ++;
        iterator2 ++;
    }
    return pLast;
}

TreeNode * GetLastCommonParent(TreeNode * pRoot, TreeNode * pNode1, TreeNode * pNode2) {
    if (pRoot == NULL || pNode1 == NULL || pNode2 == NULL)
        return NULL;
    List<TreeNode *> path1;
    GetNodePath(pRoot, pNode1, path1);
    List<TreeNode *> path2;
    GetNodePath(pRoot, pNode2, path2);
    return GetLastCommonNode(path1, path2);
}
上一篇下一篇

猜你喜欢

热点阅读