[面試算法] 劍指Offer代碼彙總

此博客彙總劍指offer上的部分代碼,更新中.......

6.重建二叉樹(前序和中序)

TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin) {
    if (pre.empty() || vin.empty())
        return NULL;
    return reConstruct(pre, vin, 0, pre.size() - 1, 0, vin.size() - 1);
}

TreeNode* reConstruct(vector<int> &pre, vector<int> &vin, int pStart, int pEnd, int vStart, int vEnd) {
    if (vStart>vEnd)
        return NULL;
    TreeNode *root = new TreeNode(pre[pStart]);
    for (int i = vStart; i <= vEnd; i++) {
        if (pre[pStart] == vin[i]) {
            root->left = reConstruct(pre, vin, pStart + 1, pStart + i - vStart, vStart, i - 1);
            root->right = reConstruct(pre, vin, pEnd - vEnd + i + 1, pEnd, i + 1, vEnd);
            break;
        }
    }
    return root;
}

17.合併兩個排序鏈表

ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
    if (!pHead1)
        return pHead2;
    if (!pHead2)
        return pHead1;

    if (pHead1->val<pHead2->val) {
        pHead1->next = Merge(pHead1->next, pHead2);
        return pHead1;
    }
    else {
        pHead2->next = Merge(pHead1, pHead2->next);
        return pHead2;
    }
}
18.樹的子結構
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
    if (!pRoot1 || !pRoot2) return false;
    return IsSubtree(pRoot1, pRoot2) || HasSubtree(pRoot1->left, pRoot2) || HasSubtree(pRoot1->right, pRoot2);
}

bool IsSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
    if (!pRoot2) return true;    //注意判斷的順序
    if (!pRoot1) return false;
    if (pRoot1->val != pRoot2->val) return false;
    return IsSubtree(pRoot1->left, pRoot2->left) && IsSubtree(pRoot1->right, pRoot2->right);
}

20.順時針打印矩陣

vector<int> spiralOrder(vector<vector<int>>& matrix) {
    vector<int> res;
    if (matrix.empty())
        return res;
    int m = matrix.size() - 1, n = matrix[0].size() - 1;
    for (int i = 0, j = 0; i <= m && j <= n; i++, j++) {

        for (int k = j; k <= n; k++)
            res.push_back(matrix[i][k]);

        for (int k = i + 1; k <= m; k++)
            res.push_back(matrix[k][n]);

        for (int k = n - 1; k >= j && i != m; k--)
            res.push_back(matrix[m][k]);

        for (int k = m - 1; k>i && j != n; k--)
            res.push_back(matrix[k][j]);

        m--;
        n--;
    }
    return res;
}

21.包含min函數的棧

class MinStack {
public:
    int min = INT_MAX;
    stack<int> stack1;

    MinStack() {

    }

    void push(int x) {
        if (x <= min) {
            stack1.push(min);
            min = x;
        }
        stack1.push(x);
    }

    void pop() {
        if (stack1.top() == min) {
            stack1.pop();
            min = stack1.top();
        }
        stack1.pop();
    }

    int top() {
        return stack1.top();
    }

    int getMin() {
        return min;
    }
};

22.棧的輸入輸出隊列

bool IsPopOrder(vector<int> pushV, vector<int> popV) {
    stack<int> stk;
    int j = 0;
    for (int i = 0; i < pushV.size(); i++) {
        stk.push(pushV[i]);
        while (j < popV.size() && stk.top() == popV[j]) {
            stk.pop();
            j++;
        }
    }
    return stk.empty();
}

27.二叉樹轉雙向鏈表

TreeNode* Convert(TreeNode* pRootOfTree)
{
    if (pRootOfTree == NULL) return NULL;
    pRootOfTree = ConvertNode(pRootOfTree);
    while (pRootOfTree->left) pRootOfTree = pRootOfTree->left;
    return pRootOfTree;
}

TreeNode* ConvertNode(TreeNode* root) {
    if (root->left) {
        TreeNode* left = ConvertNode(root->left);
        while (left->right) left = left->right;
        left->right = root;
        root->left = left;
    }
    if (root->right) {
        TreeNode* right = ConvertNode(root->right);
        while (right->left) right = right->left;
        right->left = root;
        root->right = right;
    }
    return root;
}

29.數組中出現次數超過一半的數字

int majorityElement(vector<int>& nums) {
    int x = nums[0], count = 1;
    for (int i = 1; i < nums.size(); i++) {
        if (count == 0) {
            x = nums[i];
            count = 1;
            continue;
        }
        if (nums[i] == x)  count++;
        else  count--;
    }
    return x;
}
32.1到n整數中1出現的次數
int NumberOf1Between1AndN_Solution(int n)
{
    int count = 0, loc = 1;
    int high = 0, cur = 0, low = 0;
    while (n / loc) {
        low = n - (n / loc)*loc;
        cur = (n / loc) % 10;
        high = n / (loc * 10);
        if (cur == 0)
            count += high*loc;
        else if (cur == 1)
            count += high*loc + low + 1;
        else
            count += (high + 1)*loc;
        loc = loc * 10;
    }
    return count;
}

36.數組中逆序對

int result;
void merge(vector<int> &nums, int left, int right, int mid) {
    vector<int> temp(right - left + 1, 0);
    int lstart = left, rstart = mid + 1;
    int tempIndex = 0;
    while (lstart <= mid && rstart <= right) {
        if (nums[lstart] <= nums[rstart])
            temp[tempIndex++] = nums[rstart++];
        else {
            temp[tempIndex++] = nums[lstart++];
            result += right - rstart + 1;
        }
    }
    while (lstart <= mid)  temp[tempIndex++] = nums[lstart++];
    while (rstart <= right)  temp[tempIndex++] = nums[rstart++];
    for (int i = left; i <= right; i++) {
        nums[i] = temp[i - left];
    }
}

void mergeSort(vector<int> &nums, int left, int right) {
    if (left < right) {
        int mid = (left + right) >> 1;
        mergeSort(nums, left, mid);
        mergeSort(nums, mid + 1, right);
        merge(nums, left, right, mid);
    }
}

39.平衡二叉樹判斷

bool IsBalanced(TreeNode* pRoot, int &depth) {
    if (!pRoot) {
        depth = -1;
        return true;
    }
    int left, right;
    if (IsBalanced(pRoot->left, left) && IsBalanced(pRoot->right, right)) {
        int diff = left - right;
        if (diff <= 1 && diff >= -1) {
            depth = 1 + (left>right ? left : right);
            return true;
        }
    }
    return false;
}

bool IsBalanced_Solution(TreeNode* pRoot) {
    int depth;
    return IsBalanced(pRoot, depth);
}

42.翻轉單詞順序

string ReverseSentence(string str) {
    int len = str.size();
    int start = 0;
    for (int i = 0; i < len; i++)
    {
        if (str[i] == ' ')
        {
            reverse(str.begin() + start, str.begin() + i);
            start = i + 1;
        }
        if (i == len - 1)
        {
            reverse(str.begin() + start, str.end());
        }
    }
    reverse(str.begin(), str.end());
    return str;
}

47.不用加減乘除做加法

int Add(int num1, int num2){
	if (!num2) return num1;
	return Add(num1^num2, (num1&num2) << 1);
}

49.把字符串轉化成整數

bool isTrue = false;
long long  myAtoi(string str) {
    int i = 0, minus = 1;
    long long result = 0;
    if (str.empty())
        return result;
    while (i < str.size()) {
        if (str[i] == ' ') {
            i++;
            continue;
        }
        if (str[i] == '-') {
            minus = -1;
            i++;
        }
        else if (str[i] == '+')
            i++;
        break;
    }
    while (i < str.size()) {
        if (str[i] >= '0' && str[i] <= '9') {
            result = result * 10 + minus*(str[i] - '0');
            if (minus == 1 && result >INT_MAX || minus == -1 && result < INT_MIN)
                result = minus == 1 ? INT_MAX : INT_MIN;
            i++;
        }
        else
            break;
    }
    if (i == str.size())
        isTrue = true;
    return result;
}
50.二叉樹最小公共祖先
bool getPath(TreeNode* root, TreeNode* node, vector<TreeNode*> &path) {
    if (root == node) {
        path.push_back(root);
        return true;
    }
    path.push_back(root);
    if (root->left && getPath(root->left, node, path) ||
        root->right && getPath(root->right, node, path))
        return true;
    path.pop_back();
    return false;
}

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    if (!root || !p || !q) return NULL;
    vector<TreeNode*> path1;
    vector<TreeNode*> path2;
    getPath(root, p, path1);
    getPath(root, q, path2);

    TreeNode *plast = NULL;
    for (int i = 0; i<path1.size() && i <path2.size(); i++) {
        if (path1[i] == path2[i])
            plast = path1[i];
        else
            break;
    }
    return plast;
}
51.數組中重複的數
bool duplicate(int numbers[], int length, int* duplication) {
    if (numbers == NULL || length <= 0)
        return false;
    for (int i = 0; i<length; i++) {
        if (numbers[i]<0 || numbers[i]>length - 1)
            return false;
    }
    for (int i = 0; i<length; i++) {
        while (i != numbers[i]) {
            if (numbers[i] == numbers[numbers[i]]) {
                *duplication = numbers[i];
                return true;
            }
            swap(numbers[i], numbers[numbers[i]]);
        }
    }
    return false;
}

59.對稱的二叉樹

bool SymmetircFunc(TreeNode* node1, TreeNode *node2){
	if (!node1 && !node2) return true;
	if (!node1 || !node2) return false;
	if (node1->val != node2->val) return false;
	return SymmetircFunc(node1->right, node2->left) && SymmetircFunc(node1->left, node2->right);
}

bool isSymmetrical(TreeNode* pRoot){
	if (!pRoot) return true;
	return SymmetircFunc(pRoot->left, pRoot->right);
}

64.數據流中的中位數

class Solution {
public:
    void Insert(int num)
    {
        count++;
        if (count & 0x1 == 1) {
            minheap.push(num);
            maxheap.push(minheap.top());
            minheap.pop();
        }
        else {
            maxheap.push(num);
            minheap.push(maxheap.top());
            maxheap.pop();
        }
    }

    double GetMedian()
    {
        if (count & 0x1 == 1)
            return maxheap.top();
        else
            return (maxheap.top() + minheap.top()) / 2.0;  //注意浮點數
    }
private:
    int count = 0;
    priority_queue<int> maxheap;
    priority_queue<int, vector<int>, greater<int>> minheap;
};

GitHub-Leetcode: https://github.com/wenwu313/LeetCode

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章