Leetcode

3. 无重复字符的最长子串

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        vector<int> m(256,-1);
        int res = 0;
        int left = -1;
        for(int i=0;i<s.size();i++){
            left = max(left, m[s[i]]);
            m[s[i]]=i;
            res = max(res, i-left);
        }
        return res;
        
    }
};

5. 最长回文子串

class Solution {
public:
    string longestPalindrome(string s) {
        if(s.empty())return "";
        int dp[s.size()][s.size()]={0};
        int left=0,right=0,len=0;
        for(int i=0;i<s.size();i++){
            for(int j=0;j<i;j++){
                dp[j][i]=(s[j]==s[i])&&((i-j<2)||(dp[j+1][i-1]));
                if(dp[j][i]==1&&len<i-j+1){
                    len=i-j+1;
                    left=j;
                    right=i;
                }
            }
            dp[i][i]=1;
        }
        return s.substr(left, right-left+1);
    }
};

7. 反转整数

class Solution {
public:
    int reverse(int x) {
        bool negative = x < 0;
        if (negative) x = -x;
        long r = 0;
        while (x>0) {
            r = r * 10 + x % 10;
            x /= 10;
        }
        if (negative) r = -r;
        if (r > 2147483647|| r < -2147483648) return 0;
        return (int)r;
    }
};

11. 盛最多水的容器

class Solution {
public:
    int maxArea(vector<int>& height) {
        int maxvalue=0;
        for(int i=0;i<height.size();i++){
            for(int j=0;j<i;j++){
                maxvalue = max(maxvalue, getArea(height, i, j));
            }
        }
        return maxvalue;
    }
    int getArea(vector<int>& height, int i, int j)
    {
        return ((i-j)*min(height[i], height[j]));
    }
};

 

14. 最长公共前缀

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if(strs.empty()) return "";
        if(strs.size()==1) return strs[0];
        string res;
        for(int i =0; i < strs[0].size();i++){
            char s = strs[0][i];
            for(int j=1;j<strs.size();j++){
                if(strs[j][i]!=s) return res;
            }
            res.push_back(s);
        }
        return res;
    }
};

19. 删除链表的倒数第N个节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head==NULL) return NULL;
        ListNode* fast = head;
        ListNode* slow = head;
        while(n--){
            fast=fast->next;
        }
        if(fast==NULL) return head->next;
        while(fast->next){
            slow = slow->next;
            fast = fast->next;
        }
        slow->next = slow->next->next;
        return head;
    }
};

20. 有效的括号

class Solution {
public:
   bool isValid(string s) {  
       stack<char> paren;
       for(char&c : s){
           switch(c){
               case '(':
               case '{':
               case '[': paren.push(c);break;
               case ')': if(paren.empty() || paren.top()!='(') return false; else paren.pop(); break;
               case '}': if(paren.empty() || paren.top()!='{') return false; else paren.pop(); break;
               case ']': if(paren.empty() || paren.top()!='[') return false; else paren.pop(); break;
               default: ;
                   
           }

       }
       return paren.empty();
    }  
};
class Solution {
public:
    bool isValid(string s) {
        stack<char> con;
        for (int i = 0;i<s.size();i++) {
            if (s[i] == '(' || s[i] == '[' || s[i] == '{') con.push(s[i]);
            else {
                if(con.empty()) return false;
                if (s[i] == ')' && con.top() != '(') return false;
                if (s[i] == ']' && con.top() != '[') return false; 
                if (s[i] == '}' && con.top() != '{') return false; 
                con.pop();
            }	
        }
        return con.empty();
    }
};

21. 合并两个有序链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(l1==NULL&&l2==NULL) return NULL;
        if(!l1) return l2;
        if(!l2) return l1;
        ListNode* head;
        if(l1->val<=l2->val){
            head=l1;
            head->next = mergeTwoLists(l1->next, l2);
        }
        else{
            head = l2;
            head->next = mergeTwoLists(l1, l2->next);
        }
        return head;
    }
};

23. 合并K个排序链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        if(lists.size()==0) return NULL;
        if(lists.size()==1) return lists[0];
        int begin=0;
        int end=lists.size()-1;
        while(end){
            begin=0;
            while(begin<end){
                lists[begin] = merge(lists[begin], lists[end]);
                begin++;
                end--;
            }
        }
        return lists[0];
    }
    ListNode* merge(ListNode* l1, ListNode* l2){
        if(!l1&&!l2) return NULL;
        if(!l1) return l2;
        if(!l2) return l1;
        ListNode* head;
        if(l1->val<=l2->val){
            head = l1;
            head->next = merge(l1->next, l2);
        }
        else{
            head=l2;
            head->next = merge(l1, l2->next);
        }
        return head;
    }
};

24. 两两交换链表中的节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(head==NULL||head->next==NULL) return head;
        ListNode* p=head;
        ListNode* pnextnext = p->next->next;
        ListNode* swaphead = p->next;
        swaphead->next = p;
        ListNode* swapnextnext = swapPairs(pnextnext);
        p->next = swapnextnext;
        return swaphead;
    }
};

26. 删除排序数组中的重复项

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
         if (nums.empty()) return 0;
          int j = 0, n = nums.size();
          for (int i = 0; i < n; ++i) {
              if (nums[i] != nums[j]) nums[++j] = nums[i];
          }
          return j + 1;
    }
};

27. 移除元素

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        if(nums.empty()) return 0;
        int j=0;
        for(int i=0;i<nums.size();i++){
            if(nums[i]!=val){
                nums[j++]=nums[i];
            }
        }
        return j;
    }
};

28. 实现strStr()

class Solution {
public:
    int strStr(string haystack, string needle) {
        if(needle.size()==0) return 0;
        if(haystack.size()==0) return -1;
        for(int i=0;i<haystack.size();i++){
            if(haystack[i] == needle[0]){
                for(int j=0;j<needle.size();j++){
                    if(haystack[i+j]!=needle[j]) break;
                    if(j==needle.size()-1) return i;
                }
            }
        }
        return -1;
    
    }
};

30. 与所有单词相关联的字串

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> res;
        if(s.empty()||words.empty()) return res;
        unordered_map<string, int> m1;
        int n=words.size();
        int m = words[0].size();
        for(auto &a:words){
            ++m1[a];
        }
        for(int i=0;i<=(int)s.size()-n*m;++i){
            unordered_map<string, int> m2;
            int j=0;
            for(j=0;j<n;++j){
                string t = s.substr(i+j*m,m);
                if(m1.find(t)==m1.end()) break;
                ++m2[t];
                if(m2[t]>m1[t]) break;
            }
            if(j==n) res.push_back(i);
        }
        return res;
        
    }
    
};

31. 下一个排列

class Solution {
public:
    void nextPermutation(vector<int>& num) {
        int i, j, n = num.size();
        for (i = n - 2; i >= 0; --i) {
            if (num[i + 1] > num[i]) {
                for (j = n - 1; j >= i; --j) {
                    if (num[j] > num[i]) break;
                }
                swap(num[i], num[j]);
                reverse(num.begin() + i + 1, num.end());
                return;
            }
        }
        reverse(num.begin(), num.end());
    }
};

33. 搜索旋转排序数组

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

34. 在排序数组中查找元素的第一个和最后一个位置

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> res(2, -1);
        int left = 0;
        int right = nums.size() - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) left = mid + 1;
            else right = mid;
        }
        if (nums[right] != target) return res;
        res[0] = right;
        right = nums.size();
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] <= target) left = mid + 1;
            else right= mid;
        }
        res[1] = left - 1;
        return res;
    }
};

35. 搜索插入位置

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        if(nums.size()==0) return 0;
        for(int i=0;i<nums.size();i++){
            if(target<=nums[i]){
                return i;
            }
            
        }
        return nums.size();
    }
};

36. 有效的数独

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        if(board.empty()||board[0].empty())return false;
        int m = board.size(), n=board[0].size();
        vector<vector<bool>> rowflag(m, vector<bool>(n, false));
        vector<vector<bool>> colflag(m, vector<bool>(n, false));
        vector<vector<bool>> cellflag(m, vector<bool>(n, false));
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(board[i][j]>='1'&&board[i][j]<='9'){  
                    int c = board[i][j]-'1';
                    if(rowflag[i][c]||colflag[c][j]||cellflag[3*(i/3)+j/3][c]) return false;
                    rowflag[i][c]=true;
                    colflag[c][j]=true;
                    cellflag[3*(i/3)+j/3][c]=true;
                }
            }
        }
        return true;
    }
};

37. 解数独

class Solution {
public:
    void solveSudoku(vector<vector<char>>& board) {
        solveSudoku(board, 0, 0);
    }
    bool check(vector<vector<char>> &board, int i, int j, char val)
    {
            int row = i - i%3, column = j - j%3;
            for(int x=0; x<9; x++) if(board[x][j] == val) return false;
            for(int y=0; y<9; y++) if(board[i][y] == val) return false;
            for(int x=0; x<3; x++)
            for(int y=0; y<3; y++)
                if(board[row+x][column+y] == val) return false;
            return true;
    }
    bool solveSudoku(vector<vector<char>> &board, int i, int j)
    {
        if(i==9) return true;
        if(j==9) return solveSudoku(board, i+1, 0);
        if(board[i][j] != '.') return solveSudoku(board, i, j+1);

        for(char c='1'; c<='9'; c++)
        {
            if(check(board, i, j, c))
            {
                board[i][j] = c;
                if(solveSudoku(board, i, j+1)) return true;
                board[i][j] = '.';
            }
        }

        return false;
    }    
};

38. 报数

class Solution {
public:
    string countAndSay(int n) {
        if(n<=0) return "";
        string res = "1";
        while(--n){
            string cur = "";
            for(int i=0;i<res.size();++i){
                int cnt=1;
                while(i+1<res.size()&&res[i]==res[i+1]){
                    ++cnt;
                    ++i;
                }
                cur += to_string(cnt)+res[i];
            }
            res = cur;
        }
        return res;
    }
    
};

39. 组合总和

class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> res;
        vector<int> out;
        sort(candidates.begin(), candidates.end());
        combinationSumDFS(candidates, target, 0, res, out);
        
        return res;
    }
    
    void combinationSumDFS(vector<int> &candidates, int target, int start, vector<vector<int>> &res, vector<int> & out){
        if(target<0) return;
        else if(target==0)res.push_back(out);
        else{ 
            for(int i=start;i<candidates.size();i++){
                out.push_back(candidates[i]);
                combinationSumDFS(candidates, target-candidates[i], i, res, out);
                out.pop_back();
            }
            
        }
    }
};

40. 组合总和 II

class Solution {
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<vector<int>> res;
        vector<int> out;
        sort(candidates.begin(), candidates.end());
        combinationdfs(candidates, target, 0, res, out);
        return res;
    }
    void combinationdfs(vector<int>& candidates, int target, int start, vector<vector<int>>&res, vector<int> &out){
        if(target<0)return;
        else if(target==0) res.push_back(out);
        else{
            for(int i=start;i<candidates.size();i++){
                if(i>start&&candidates[i]==candidates[i-1]) continue;
                out.push_back(candidates[i]);
                combinationdfs(candidates, target-candidates[i], i+1, res, out);
                out.pop_back();
            }
        }
    }
      
};

41. 缺失的第一个正数

class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        for(int i=0;i<nums.size();i++){
            while(nums[i]>0&&nums[i]<nums.size()&&nums[i]!=nums[nums[i]-1]){
                swap(nums[i],nums[nums[i]-1]);
            }
        }
        for(int i=0;i<nums.size();i++){
            if(nums[i]!=(i+1)) return (i+1);
        }
        return nums.size()+1;
    }
};

42. 接雨水

class Solution {
public:
    int trap(vector<int>& height) {
        if(height.size()<3) return 0;
        int res = 0;
        int nl = 1;
        int nr = height.size()-2;
        int ml = height.front();
        int mr = height.back();
        while(nl<=nr){
            if(ml<mr){
                if( ml < height[nl] ){ ml = height[nl];}
                else{ res+= ml-height[nl];}                                
                nl++;
            }   
            else{ 
                if( mr < height[nr] ){mr = height[nr];}
                else{ res += mr-height[nr];}
                nr--;        
            }       
        }
        return res;
    }
};

43. 字符串相乘

class Solution {
public:
    string multiply(string num1, string num2) {
        int m=num1.size();
        int n = num2.size();
        vector<int> array(m+n);
        string ans = "";
        for(int i=m-1;i>=0;i--){
            for(int j=n-1;j>=0;j--){
                int index1=i+j;
                int index2 = i+j+1;
                int sum = ((num1[i]-'0')*(num2[j]-'0'))+array[index2];
                array[index1] += sum/10;
                array[index2] = sum%10; 
            }
        }
        for(auto v:array){
            if(ans.size()!=0||v != 0){
                ans+=to_string(v);
            }
        }
        return ans.size()==0?"0":ans;
    }
};

44. 通配符匹配

class Solution {
public:
    bool isMatch(string s, string p) {
        int m = s.size(), n = p.size();
        vector<vector<bool>> dp(m+1, vector<bool>(n+1, false));
        dp[0][0]=true;
        for(int i=1;i<=n;++i){
            if(p[i-1]=='*')dp[0][i]=dp[0][i-1];
        }
        for(int i=1;i<=m;++i){
            for(int j=1;j<=n;++j){
                if(p[j-1]=='*'){
                    dp[i][j]=dp[i-1][j] || dp[i][j-1];
                }
                else{
                    dp[i][j]=(s[i-1]==p[j-1]||p[j-1]=='?')&&dp[i-1][j-1];
                }
            }
        }
        return dp[m][n];
    }
};

45. 跳跃游戏 II

class Solution {
public:
    int jump(vector<int>& nums) {
        if(nums.size()<=1)return 0;
        int max=0, index=0;
        int i=0, step=0;
        int len = nums.size();
        while(i<len){
            if(i+nums[i]>=len-1){
                step++;
                break;
            }
            max = 0;
            index = i+1;
            for(int j=i+1;j-i<=nums[i];j++){
                if(max<j-i+nums[j]){
                    max = j-i+nums[j];
                    index = j;
                }
            }
            i = index;
            step++;
        }
        return step;
    }
};

46. 全排列

class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> res;
        premutedfs(nums, 0, res);
        return res;
    }
    void premutedfs(vector<int>& nums, int start, vector<vector<int>>& res){
        if(start==nums.size()) res.push_back(nums);
        else{
            for(int i=start;i<nums.size();i++){
                swap(nums[i], nums[start]);
                premutedfs(nums, start+1, res);
                swap(nums[i], nums[start]);
            }
        }
    }
};

47. 全排列 II

class Solution {
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<vector<int>> res;
        premuteUniqueMain(nums, 0, res);
        return res;
    }

    void premuteUniqueMain(vector<int>&nums, int begin, vector<vector<int>> &res){
        int n = nums.size();
        set<int> begindata;
        if(begin==n){
            res.push_back(nums);
        }
        else{
            for(int i=begin;i<n;i++){
                if(begindata.find(nums[i])!=begindata.end()){
                    continue;
                }
                else{
                    begindata.insert(nums[i]);
                    swap(nums[i],nums[begin]);
                    premuteUniqueMain(nums, begin+1, res);
                    swap(nums[i], nums[begin]);
                }
            }
        }
    }
};

48. 旋转图像

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        size_t len = matrix.size();
        for (int i = 0; i < len; ++i) {
            for (int j = i + 1; j < len; ++j) {
                swap(matrix[i][j], matrix[j][i]);
            }
        }
        size_t len2 = len / 2;
        for (int i = 0; i < len; ++i) {
            for (int j = 0; j < len2; ++j) {
                swap(matrix[i][j], matrix[i][len - 1 - j]);
            }
        }

    }
};

53. 最大子序和

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        if(nums.empty())return 0;
        vector<int> maxnums;
        maxnums.push_back(nums[0]);
        for(int i=1;i<nums.size();i++){
            if(maxnums[i-1]<=0){
                maxnums.push_back(nums[i]);
            }
            else{
                int max = maxnums[i-1]+nums[i];
                maxnums.push_back(max);
            }
        }
        sort(maxnums.begin(), maxnums.end());
        return maxnums[maxnums.size()-1];;
        
    }
};

61. 旋转链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(head==NULL||head->next==NULL) return head;
        ListNode* fast = head;
        ListNode* slow = head;
        int count=1;
        while(slow->next){
            slow = slow->next;
            count++;
        }
        slow->next = head;
        count = count-k%count;
        while(count--){
            fast = fast->next;
            slow=slow->next;
        }
        slow->next = NULL;
        return fast;
        
    }
};

66. 加一

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int deep = digits.size()-1;
        dfsplusone(digits, deep);
        return digits;
    }
    void dfsplusone(vector<int>& digits, int deep){
        if(digits[deep]==9){
            if(deep==0){
                digits[deep]=0;
                digits.insert(digits.begin(), 1);
            }
            else{
                digits[deep]=0;
                dfsplusone(digits, deep-1);
            }
        }
        else{
            ++digits[deep];
        }
    }
};

69. x 的平方根

class Solution {
public:
    int mySqrt(int x) {
        if(x==0) return 0;
        int res=1, pre=0;
        while(abs(res-pre)>1e-6){
            pre=res;
            res=(res+x/res)/2;
        }
        return int(res);
        
    }
};

73. 矩阵置零

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        if(matrix.empty()||matrix[0].empty()) return;
        int m = matrix.size();//row
        int n = matrix[0].size();//col
        bool rowzero = false;
        bool colzero = false;
        for(int i = 0;i<m;i++){
            if(matrix[i][0]==0) rowzero=true;
        }
        for(int i = 0;i<n;i++){
            if(matrix[0][i]==0) colzero=true;
        }
        for(int i=1;i<m;i++){
            for(int j=1;j<n;j++){
                if(matrix[i][j]==0){
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }       
            }
        }
        for(int i=1;i<m;i++){
            for(int j=1;j<n;j++){
                if(matrix[i][0]==0||matrix[0][j]==0){
                    matrix[i][j]=0;
                }
    
            }
        }
        if(rowzero){
            for(int i=0;i<m;i++) matrix[i][0]=0;
        }
        if(colzero){
            for(int i=0;i<n;i++) matrix[0][i]=0;
        }
        
        
    }
};

77. 组合

class Solution {
public:
    vector<vector<int>> combine(int n, int k) {
        vector<vector<int>> res;
        vector<int> temp;
        dfs(temp, n,k, res);
        return res;
    }
    void dfs(vector<int>&temp, int n, int k, vector<vector<int>> & res){
        if(temp.size()==k) res.push_back(temp);
        else{
            for(int i=0;i<n;i++){
                if(!temp.empty()&&i+1<=temp.back())continue;
                temp.push_back(i+1);
                dfs(temp, n, k, res);
                temp.pop_back();
            }
        }
        
    }
};

78. 子集

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>> res(1);
        sort(nums.begin(), nums.end());
        for(int i = 0;i<nums.size();++i){
            int size = res.size();
            for(int j = 0;j < size;++j){
                res.push_back(res[j]);
                res.back().push_back(nums[i]);
            }
        }
        return res;
    }
};

82. 删除排序链表中的重复元素 II

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head==NULL||head->next==NULL) return head;
        ListNode* newhead = new ListNode(0);
        newhead->next = head;
        ListNode* pre = newhead;
        while(pre->next){
            ListNode* cur = pre->next;
            while(cur->next&& cur->val ==cur->next->val){
                cur = cur->next;
            }
            if(cur!=pre->next){
                pre->next=cur->next;
            }
            else{
                pre= pre->next;
            }
        }
        return newhead->next;
    }
};

83. 删除排序链表中的重复元素

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head==NULL ||head->next ==NULL) return head;
        ListNode* p = head;
        while(p->next){
            if(p->val==p->next->val){
                p->next = p->next->next;
            }
            else{
                p = p->next;
            }
        }
        return head;
    }
};

86. 分隔链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        if(head==NULL||head->next==NULL)return head;
        ListNode* small = new ListNode(0);
        ListNode* other = new ListNode(0);
        ListNode* presmall = small;
        ListNode* preother = other;
        while(head){
            if(head->val<x){
                small->next = head;
                small = small->next;
            }
            else{
                other->next = head;
                other = other->next;
            }
            head = head->next;
        }
        small->next = preother->next;
        other->next = NULL;
        return presmall->next;
    }
};

90. 子集 II

class Solution {
public:
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        if(nums.empty()) return {};
        vector<vector<int>> res(1);
        sort(nums.begin(), nums.end());
        int size=1, last = nums[0];
        for(int i=0;i<nums.size();i++){
            if(last!=nums[i]){
                last = nums[i];
                size=res.size();
            }
            int newsize = res.size();
            for(int j=newsize-size;j<newsize;j++){
                res.push_back(res[j]);
                res.back().push_back(nums[i]);
            }
        }
        return res;
    }
};

92. 反转链表 II

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if(head==NULL||head->next==NULL) return head;
        int pos=1;
        ListNode* newhead = new ListNode(0);
        ListNode* p = head;
        newhead->next = head;
        ListNode* pre = newhead;
        while(p&&pos<m)
        {
            pre=p;
            p = p->next;
            ++pos;
        }
        for(int i=0;i<n-m;++i){
            ListNode* tmp = p->next;
            p->next = tmp->next;
            tmp->next = pre->next;
            pre->next = tmp;
        }
        return newhead->next;
    }
};

94. 二叉树的中序遍历

/**
 * 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:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        inorder(root, res);
        return res;
    }
    void inorder(TreeNode* root, vector<int> &res){
        if(root){
            if(root->left) inorder(root->left, res);
            res.push_back(root->val);            
            if(root->right) inorder(root->right, res);
        }
    }
};

96. 不同的二叉搜索树

class Solution {
public:
    int numTrees(int n) {
        if(n==0) return 0;
        if(n==1) return 1;
        int nums[n+1];
        nums[0]=1;
        nums[1]=1;
        for(int i=2;i<=n;i++){
            nums[i]=0;
            for(int j=0;j<i;j++){
                nums[i] += nums[j]*nums[i-1-j];
            }
        }
        return nums[n];
    }
};

100. 相同的树

/**
 * 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 isSameTree(TreeNode* p, TreeNode* q) {
        if(p==NULL&&q==NULL) return true;
        if(p==NULL||q==NULL) return false;
        if(p->val!=q->val) return false;
        
        return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值