leetcode 剑指offer重复题目

来源:互联网 发布:西方意识形态 知乎 编辑:程序博客网 时间:2024/06/01 07:58

226. Invert Binary Tree    28 

class Solution {public:    TreeNode* invertTree(TreeNode* root) {        if(root==nullptr) return root;        invert(root);        return root;    }    void invert(TreeNode* root){        if(root==nullptr) return;        if(root->left==nullptr&&root->right==nullptr)            return;        TreeNode*temp=root->left;        root->left=root->right;        root->right=temp;        invert(root->left);        invert(root->right);    }};

263. Ugly Number

class Solution {
public:
    bool isUgly(int num) {
        if(num==0) return false;
        while(num%2==0)
            num/=2;
        while(num%3==0)
            num/=3;
        while(num%5==0)
            num/=5;


        return num==1;
    }
};


105. Construct Binary Tree from Preorder and Inorder Traversal  7 


155. Min Stack   30

class MinStack {public:    /** initialize your data structure here. */    MinStack() {            }        void push(int x) {        mains.push(x);        if(fuzhu.size()==0||fuzhu.top()>x)            fuzhu.push(x);        else         {            fuzhu.push(fuzhu.top());        }    }        void pop() {        mains.pop();        fuzhu.pop();    }        int top() {        return mains.top();    }        int getMin() {        return fuzhu.top();    }    stack<int> fuzhu;    stack<int> mains; };


225. Implement Stack using Queues 9 相关

解答:http://blog.csdn.net/m0_37693059/article/details/76422264

三种方法


232. Implement Queue using Stacks 9

class MyQueue {public:    /** Initialize your data structure here. */    MyQueue() {            }        /** Push element x to the back of queue. */    void push(int x) {        stack1.push(x);    }        /** Removes the element from in front of queue and returns that element. */    int pop() {        if(stack2.empty())        {            while(!stack1.empty())            {                stack2.push(stack1.top());                stack1.pop();            }        }        int temp=stack2.top();        stack2.pop();        return temp;    }        /** Get the front element. */    int peek() {         if(stack2.empty())        {            while(!stack1.empty())            {                stack2.push(stack1.top());                stack1.pop();            }        }        return stack2.top();    }        /** Returns whether the queue is empty. */    bool empty() {        return stack1.empty()&&stack2.empty();    }    stack<int> stack1;    stack<int> stack2;};

238. Product of Array Except Self 66



572. Subtree of Another Tree   26


/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    bool isSubtree(TreeNode* s, TreeNode* t) {        bool result=false;        if(s&&t)        {            if(s->val==t->val)//入口点                result=hasSub(s,t);            if(result==false)                result=isSubtree(s->left,t);            if(result==false)                result=isSubtree(s->right,t);        }        return result;    }    bool hasSub(TreeNode* owner,TreeNode* sub){        if(owner==nullptr&&sub==nullptr) return true;        if(owner==nullptr||sub==nullptr) return false;        if(owner->val!=sub->val) return false;        return hasSub(owner->left,sub->left)&&hasSub(owner->right,sub->right);    }};


233   43

http://blog.csdn.net/m0_37693059/article/details/76468223




原创粉丝点击