LeetCode 2015.7.20 148,150,151,152,153,208,209,162,236

本文解析了LeetCode上的多个经典算法题目,包括列表排序、逆波兰表达式求值、字符串单词翻转、最大子数组乘积等。通过具体实现代码展示了不同问题的解决思路和技术细节。
LeetCode 2015.7.20 148,150,151,152,153,208,209,162,236

148 Sort List


class Solution {
public:
    struct Node {
        int val;
        int cnt;
        Node* next;
        Node(int x) : val(x),cnt(1),next(NULL) {}
    };

    ListNode* sortList(ListNode* head) {
        if (head==NULL) return NULL;
        Node* shead = new Node(head->val);
        Node* sp;
        Node* tmpsp;
        ListNode* p=head->next;
        while (p!=NULL)
        {

            if (p->val < shead->val)
            {
                Node* node2 = new Node(p->val);
                node2->next=shead;
                shead=node2;
                p=p->next;
                continue;
            }
            sp = shead;
            tmpsp = sp;
            while ((sp!=NULL)&&(sp->val < p->val))
            {
                tmpsp=sp;
                sp=sp->next;
            }
            if (tmpsp->val == p->val)
                tmpsp->cnt = tmpsp->cnt+1;
            else
            {
                Node* node = new Node(p->val);
                node->next=tmpsp->next;
                tmpsp->next=node;
            }
            p=p->next;
        }
        ListNode* h = new ListNode(0);
        ListNode* x = h;
        while (shead!=NULL)
        {
            for(int i=1;i<=shead->cnt;i++)
            {
                ListNode* n = new ListNode(shead->val);
                x->next = n;
                x=x->next;
            }
            shead = shead->next;
        }
        return h->next;
    }
};

150 Evaluate Reverse Polish Notation
class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        bool flag[300]={0};
        flag['+']=true;
        flag['-']=true;
        flag['*']=true;
        flag['/']=true;
        stack<int> s;
        while (!s.empty()) s.pop();
        for(int i=0;i<tokens.size();i++)
        {
            if ((tokens[i].size()==1) && (flag[tokens[i][0]]))
            {
                int p2=s.top();
                s.pop();
                int p1=s.top();
                s.pop();
                int ans;
                switch (tokens[i][0])
                {
                case '+':
                    ans = p1 + p2;
                    break;
                case '-':
                    ans = p1 - p2;
                    break;
                case '*':
                    ans = p1 * p2;
                    break;
                case '/':
                    ans = p1 / p2;
                    break;
                }
                s.push(ans);
            }
            else
            {
                int number = atoi(tokens[i].c_str());
                s.push(number);
            }
        }
        return s.top();
    }
};

151 Reverse Words in a String
class Solution {
public:
    void reverseWords(string &s) {
        if (s.size()==0) return ;
        vector<string> ans;
        ans.clear();
        if (s[0]!=' ') s=' '+s;
        bool flag = false;
        string tmpstr="";
        for(int i=0;i<s.size();i++)
        {
            if (s[i]==' ')
            {
                flag = false;
                continue;
            }
            if (!flag)
            {
                ans.push_back(tmpstr);
                tmpstr="";
                flag=true;
            }
            if (flag)
            {
                tmpstr=tmpstr+s[i];
            }
        }
        if (ans.size()==0)
        {
            s="";
            return ;
        }
        if (tmpstr!="") ans.push_back(tmpstr);
        s=ans[ans.size()-1];
        for(int i=ans.size()-2;i>0;i--)
        {
            s=s+" ";
            for(int j=0;j<ans[i].size();j++)
                s=s+ans[i][j];
        }
        return ;
    }
};

152 Maximum Product Subarray
class Solution {
public:
    int maxProduct(vector<int>& nums) {
        vector<int> f,g;
        int ans=-INT_MAX;
        f.clear();
        g.clear();
        g.push_back(1);
        f.push_back(1);
        for(int i=0;i<nums.size();i++)
        {
            int tmp=max(nums[i],f[i]*nums[i]);
            tmp=max(tmp,g[i]*nums[i]);
            f.push_back(tmp);
            tmp=min(nums[i],f[i]*nums[i]);
            tmp=min(tmp,g[i]*nums[i]);
            g.push_back(tmp);
            if (f[i+1]>ans) ans=f[i+1];
        }
        return ans;
    }
};

153 Find Minimum in Rotated Sorted Array
class Solution {
public:
    int findMin(vector<int>& nums) {
        int left=0,right=nums.size()-1;
        while (left<=right)
        {
            if (left==right) return nums[left];
            if (left+1==right) return min(nums[left],nums[right]);
            int mid=(left+right)/2;
            if (nums[mid]<nums[left] && nums[mid]<nums[right])
                right=mid;
            else
            {
                if (nums[left]<nums[right])
                    right=mid-1;
                else
                    left=mid+1;
            }
        }
        return 0;
    }
};

208 Implement Trie (Prefix Tree)
class TrieNode {
public:
    // Initialize your data structure here.
    TrieNode* child[27];
    bool word;
    TrieNode() {
        word=false;
        for(int i=1;i<=26;i++)
            child[i]=NULL;
    }
};

class Trie {
public:
    Trie() {
        root = new TrieNode();
    }

    // Inserts a word into the trie.
    void insert(string word) {
        TrieNode* tmproot=root;
        for(int i=0;i<word.size();i++)
        {
            int ch=word[i]-'a'+1;
            if (tmproot->child[ch]!=NULL)
            {
                tmproot=tmproot->child[ch];
                continue;
            }
            else
            {
                TrieNode* tmpnode = new TrieNode();
                tmproot->child[ch]= tmpnode;
                tmproot=tmpnode;
            }
        }
        tmproot->word=true;
    }

    // Returns if the word is in the trie.
    bool search(string word) {
        TrieNode* tmproot=root;
        for(int i=0;i<word.size();i++)
        {
            int ch=word[i]-'a'+1;
            if (tmproot->child[ch]==NULL) return false;
            tmproot=tmproot->child[ch];
        }
        return tmproot->word;
    }

    // Returns if there is any word in the trie
    // that starts with the given prefix.
    bool startsWith(string prefix) {
        TrieNode* tmproot=root;
        for(int i=0;i<prefix.size();i++)
        {
            int ch=prefix[i]-'a'+1;
            if (tmproot->child[ch]==NULL) return false;
            tmproot=tmproot->child[ch];
        }
        return true;
    }

private:
    TrieNode* root;
};

209 Minimum Size Subarray Sum
class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        if (nums.size()==0) return 0;
        vector<int> sum;
        sum.clear();
        sum.push_back(0);
        sum.push_back(nums[0]);
        for(int i=1;i<nums.size();i++)
        {
            sum.push_back(sum[i]+nums[i]);
        }
        if (sum[sum.size()-1]<s) return 0;
        if (sum[sum.size()-1]==s) return nums.size();
        for(int k=1;k<nums.size();k++)
        {
            for(int i=k;i<sum.size();i++)
                if (sum[i]-sum[i-k]>=s) return k;
        }
        return 0;
    }
};

162 Find Peak Element
class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        int left = 0,right = nums.size()-1;
        while (left<=right){
            if (left==right)
                return left;
            int mid=(left+right)/2;
            if (nums[mid]<nums[mid+1])
                left=mid+1;
            else
                right=mid;
        }
    }
};

236 Lowest Common Ancestor of a Binary Tree
class Solution {
public:
    stack<TreeNode*> s[2];
    bool stop;

    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        while (!s[0].empty()) s[0].pop();
        while (!s[1].empty()) s[1].pop();

        stop = false;
        depthsearch(0,root,p);
        stop = false;
        depthsearch(1,root,q);

        set<TreeNode*> common;
        common.clear();
        while(!s[0].empty())
        {
            common.insert(s[0].top());
            s[0].pop();
        }
        while(!s[1].empty())
        {
            if (common.find(s[1].top())!=common.end())
                return s[1].top();
            s[1].pop();
        }
    }
    void depthsearch(int flag,TreeNode* root,TreeNode* x)
    {
        if (stop) return ;
        s[flag].push(root);
        if (root==x)
        {
            stop = true;
            return ;
        }
        if (root->left!=NULL)
            depthsearch(flag,root->left,x);
        if (root->right!=NULL)
            depthsearch(flag,root->right,x);
        if (!stop) s[flag].pop();
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值