LeetCode 2015.8.6 17,216,131,215,230,34,92

本文深入探讨了LeetCode平台上的多个经典算法问题,包括数字组合、组合总和、回文分割等。详细介绍了每个问题的解决策略和代码实现,旨在帮助读者提升算法思维和编程技能。
LeetCode 2015.8.6 17,216,131,215,230,34,92

17 Letter Combinations of a Phone Number
class Solution {
public:
    vector<string> ans;
    string tmp;
    int len[10]={0,0,3,3,3,3,3,4,3,4};
    char ch[10][4]={  {' ',' ',' ',' '},
                    {' ',' ',' ',' '},
                    {'a','b','c',' '},
                    {'d','e','f',' '},
                    {'g','h','i',' '},
                    {'j','k','l',' '},
                    {'m','n','o',' '},
                    {'p','q','r','s'},
                    {'t','u','v',' '},
                    {'w','x','y','z'}
                  };
    vector<string> letterCombinations(string digits) {
        int n = digits.size();
        tmp.clear();
        if (n==0) return ans;
        depthSearch(n,0,digits);
        return ans;
    }
    void depthSearch(int n, int k, string d)
    {
        if (n==k)
        {
            ans.push_back(tmp);
            tmp.clear();
            return ;
        }
        int p=d[k]-'0';
        for(int i=0;i<len[p];i++)
        {
            string save = tmp;
            tmp=tmp+ch[p][i];
            depthSearch(n,k+1,d);
            tmp=save;
        }
        return ;
    }
};

216 Combination Sum III
class Solution {
public:
    vector< vector<int> > ans;
    vector<int> tmpans;
    vector< vector<int> > combinationSum3(int k, int n) {
        if (n==0 || k==0) return ans;
        depthSearch(k,1,n);
        return ans;
    }
    void depthSearch(int k, int p, int left)
    {
        if (k==0 && left==0)
        {
            ans.push_back(tmpans);
            return ;
        }
        for(int i=p;i<=min(left,9);i++)
        {
            tmpans.push_back(i);
            depthSearch(k-1,i+1,left-i);
            tmpans.pop_back();
        }
        return ;
    }
};

131 Palindrome Partitioning
class Solution {
public:
    vector< vector<string> > ans;
    vector< string > tmpans;
    vector< vector<string> > partition(string s) {
        int n = s.size();
        depthSearch(n,1,0,s);
        return ans;
    }

    bool justify(string s)
    {
        int len = s.size()-1;
        int len2= len/2;
        for(int i=0;i<=len2;i++)
        {
            if (s[i]!=s[len-i]) return false;
        }
        return true;
    }

    void depthSearch(int n, int k, int p,string s)
    {
        if (n==p)
        {
            ans.push_back(tmpans);
            return ;
        }
        for(int i=k;i<=n;i++)
        {
            string mid="";
            for(int j=p;j<i;j++)
            {
                mid += s[j];
            }
            if (justify(mid))
            {
                tmpans.push_back(mid);
                depthSearch(n,i+1,i,s);
                tmpans.pop_back();
            }
        }
        return ;
    }

};

215 Kth Largest Element in an Array
class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        sort(nums.begin(),nums.end());
        return (nums[nums.size()-k]);
    }
};

230 Kth Smallest Element in a BST
class Solution {
public:
    int cnt;
    int ans;
    int kthSmallest(TreeNode* root, int k) {
        cnt = 0;
        depthSearch(root,k);
        return ans;
    }

    void depthSearch(TreeNode* root, int k)
    {
        if (root->left != NULL) depthSearch(root->left,k);
        cnt++;
        if (cnt==k)
        {
            ans = root->val;
            return ;
        }
        if (root->right != NULL) depthSearch(root->right,k);
    }
};

34 Search for a Range
class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> ans;
        if (nums.size()==0) return ans;
        int left = 0,right = nums.size()-1,mid;
        while (left<=right)
        {
            mid = left +(right-left)/2;
            if (nums[mid] == target) break;
            if (nums[mid]>target)
                right = mid - 1;
            else
                left = mid + 1;
        }
        if (left>right)
        {
            ans.push_back(-1);
            ans.push_back(-1);
            return ans;
        }
        int l=mid , r=mid;
        while (l-1>=0 && nums[l-1]==target) l--;
        while (r+1<nums.size() && nums[r+1]==target) r++;
        ans.push_back(l);
        ans.push_back(r);
        return ans;
    }
};
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        ListNode* left;
        ListNode* h=new ListNode(0);
        ListNode* right = new ListNode(0);
        h->next=head;
        ListNode* p=h;
        int i;
        for(i=1;i<m;i++)
            p=p->next;
        left=p;
        p=p->next;
        right->next=p;
        while (i<n)
        {
            i++;
            ListNode* tmp = right->next;
            right->next=p->next;
            p->next=right->next->next;
            right->next->next=tmp;
        }
        left->next=right->next;
        return h->next;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值