LeetCode题解

来源:互联网 发布:什么软件防蓝光好 编辑:程序博客网 时间:2024/06/05 22:57

最近刷了一些题,就贴在这里了!方便以后查看改进!

感觉简单的都比较简单,难的都很难!很明显我水平现在还不够,且行且成长吧!

vector<vector<int>> levelOrder(TreeNode* root) //Binary Tree Level Order Traversal{    vector<vector<int> > v;    int i = 0, flag = 0;    if (!root)        return v;    queue<TreeNode *> q, qq;    q.push(root);    v.resize(1);    while(!q.empty())    {        TreeNode *tmp = q.front();        v[i].push_back(tmp->val);        if (tmp->left)            qq.push(tmp->left);        if (tmp->right)            qq.push(tmp->right);        q.pop();        if (q.empty())        {            ++i;            while(!qq.empty())            {                q.push(qq.front());                qq.pop();                flag = 1;            }            if (flag)            {                v.resize(i + 1);                flag = 0;            }        }    }    return v;}

class Solution {//Balanced Binary Treepublic:    int depth (TreeNode *root){    int ldepth, rdepth;    if (!root)        return 0;    ldepth = depth(root->left) + 1;    rdepth = depth(root->right) + 1;    return max(ldepth, rdepth);}
bool isBalanced(TreeNode* root){    if (!root)        return true;    return isBalanced(root->left) && isBalanced(root->right)        && abs(depth(root->left) - depth(root->right)) <= 1;}};


int maxProfit(vector<int>& prices) {        if(!prices.size())            return 0;        int p = 0, minp=prices[0],mp=0;        for(int i=1;i<prices.size();++i)        {            if(prices[i]<minp)                minp=prices[i];            else            {                p=prices[i]-minp;                if(p>mp)                    mp=p;            }        }        return mp;    }


int minDepth(TreeNode* root) {        int l, r;        if (!root)            return 0;        if (!root->left && !root->right)            return 1;        else if (root->left && !root->right)            {                l = minDepth (root->left) + 1;                 r = l + 1;            }        else if (!root->left && root->right)        {        r = minDepth (root->right) + 1;        l = r + 1;        }        else        {            l = minDepth(root->left) + 1;            r = minDepth(root->right) + 1;        }        return min (l, r);    }

class Solution { //Symmetric Treepublic:    bool f (TreeNode* l, TreeNode* r)    {        if (!l || !r)            return l == r;        return l->val == r->val &&            f (l->left, r->right) &&            f (l->right, r->left);    }        bool isSymmetric(TreeNode* root) {        if (root == NULL)            return true;        return f (root->left, root->right);    }};


int singleNumber(vector<int>& nums) {    map<int, int> m;    for (int i = 0; i < nums.size(); ++i)    {        if (m.find(nums[i]) != m.end())             m[nums[i]]++;        else            m[nums[i]] = 0;    }    for (map<int, int> :: iterator it = m.begin(); it != m.end(); it++)    {        if (it->second == 0)            return it->first;    }}


ListNode* deleteDuplicates(ListNode* head) {        if (head == NULL)            return head;        ListNode *p, *q;        p = head;        q = p->next;        while(q)        {            if (p->val == q->val)            {                ListNode *tmp = q;                q = q->next;                p->next = q;                delete tmp;                tmp = NULL;            }            else            {                p = p->next;                q = q->next;            }        }        return head;    }

int lengthOfLastWord(string s){    int n = s.size();    int len = 0;    for (int i = n - 1; i >= 0; )    {        if (s[i] == ' ')            --i;        else        {            do            {                len++;                i--;            }while (i >= 0 && s[i] != ' ');            break;        }    }    return len;}

bool isSameTree(TreeNode* p, TreeNode* q)//same tree{    if (p && q)    {        return isSameTree(p->left, q->left) &&            isSameTree(p->right, q->right) && (p->val == q->val);    }    return p == q;}


原创粉丝点击