├── 102.Binary Tree Level Order Traversal.cpp ├── 215.Kth Largest Element in an Array.cpp ├── 240.Search a 2D Matrix II.cpp ├── 287. Find the Duplicate Number.cpp ├── 300.Longest Increasing Subsequence.cpp ├── 377.Combination Sum IV.cpp ├── 380.Insert Delete GetRandom O(1).cpp ├── 39. Combination Sum.cpp ├── 416.Partition Equal Subset Sum.cpp ├── 437.Path Sum III.cpp ├── 494.Target Sum.cpp ├── 560.Subarray Sum Equals K.cpp ├── 647.Palindromic Substrings.cpp ├── 77.Combinations.cpp ├── 78.Subsets.cpp └── README.md /102.Binary Tree Level Order Traversal.cpp: -------------------------------------------------------------------------------- 1 | 102.Binary Tree Level Order Traversal 2 | /** 3 | * Definition for a binary tree node. 4 | * struct TreeNode { 5 | * int val; 6 | * TreeNode *left; 7 | * TreeNode *right; 8 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | vector> levelOrder(TreeNode* root) { 14 | vector> res; 15 | if(!root) return res; 16 | vector path; 17 | vector vec; 18 | int cur=0,last=1; 19 | vec.push_back(root); 20 | while(curval); 24 | if(vec[cur]->left!=nullptr) vec.push_back(vec[cur]->left); 25 | if(vec[cur]->right!=nullptr) vec.push_back(vec[cur]->right); 26 | cur++; 27 | } 28 | res.push_back(path); 29 | path.clear(); 30 | } 31 | return res; 32 | 33 | 34 | } 35 | }; -------------------------------------------------------------------------------- /215.Kth Largest Element in an Array.cpp: -------------------------------------------------------------------------------- 1 | 215.Kth Largest Element in an Array 2 | class Solution { 3 | public: 4 | int partition(vector&nums,int l,int r){ 5 | int m=l; 6 | int ran=nums[r]; 7 | for(int i=l;i=ran) swap(nums[i],nums[m++]); 9 | } 10 | swap(nums[m],nums[r]); 11 | return m; 12 | } 13 | int findKthLargest(vector& nums, int k) { 14 | int l=0; 15 | int r=nums.size()-1; 16 | int m=partition(nums,l,r); 17 | while(m!=k-1){ 18 | if(m>k-1) { 19 | m=partition(nums,l,m-1); 20 | }else{ 21 | m=partition(nums,m+1,r); 22 | } 23 | } 24 | return nums[m]; 25 | } 26 | }; -------------------------------------------------------------------------------- /240.Search a 2D Matrix II.cpp: -------------------------------------------------------------------------------- 1 | 240. Search a 2D Matrix II 2 | class Solution { 3 | public: 4 | bool searchMatrix(vector>& matrix, int target) { 5 | if(matrix.size()==0) return false; 6 | int j=matrix[0].size()-1; 7 | int i=0; 8 | while(i=0){ 9 | if(matrix[i][j]==target) return true; 10 | if(matrix[i][j]>target){ 11 | j--; 12 | }else{ 13 | i++; 14 | } 15 | } 16 | return false; 17 | } 18 | }; -------------------------------------------------------------------------------- /287. Find the Duplicate Number.cpp: -------------------------------------------------------------------------------- 1 | 287. Find the Duplicate Number 2 | 3 | class Solution { 4 | public: 5 | int findDuplicate(vector& nums) { 6 | int len=nums.size(); 7 | int low=1; 8 | int high=len-1; 9 | int mid=0; 10 | while(lowmid){ 17 | high=mid; 18 | }else{ 19 | low=mid+1; 20 | } 21 | } 22 | return low; 23 | } 24 | }; -------------------------------------------------------------------------------- /300.Longest Increasing Subsequence.cpp: -------------------------------------------------------------------------------- 1 | 300. Longest Increasing Subsequence 2 | class Solution { 3 | public: 4 | int lengthOfLIS(vector& nums) { 5 | //dp[i] represents the lenth of the list end with i 6 | //dp[i]=max(dp[i],dp[j]+1) for all nums[j]<=nums[i] 7 | if(nums.size()==0) return 0; 8 | int res=1; 9 | vector dp(nums.size(),1); 10 | for(int i=1;i& nums, int target) { 5 | vector dp(target+1); 6 | dp[0]=1; 7 | for(int i=1;i<=target;i++){ 8 | for(auto num:nums){ 9 | if(num<=i) dp[i]+=dp[i-num]; 10 | } 11 | } 12 | return dp.back(); 13 | } 14 | }; -------------------------------------------------------------------------------- /380.Insert Delete GetRandom O(1).cpp: -------------------------------------------------------------------------------- 1 | 380. Insert Delete GetRandom O(1) 2 | class RandomizedSet { 3 | public: 4 | /** Initialize your data structure here. */ 5 | RandomizedSet() { 6 | 7 | } 8 | 9 | /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ 10 | bool insert(int val) { 11 | if(mp.find(val)!=mp.end()) return false; 12 | nums.emplace_back(val); 13 | mp[val]=nums.size()-1; 14 | return true; 15 | } 16 | 17 | /** Removes a value from the set. Returns true if the set contained the specified element. */ 18 | bool remove(int val) { 19 | if(mp.find(val)==mp.end()) return false; 20 | int last =nums.back(); 21 | mp[last]=mp[val]; 22 | nums[mp[val]]=last; 23 | nums.pop_back(); 24 | mp.erase(val); 25 | return true; 26 | } 27 | 28 | /** Get a random element from the set. */ 29 | int getRandom() { 30 | return nums[rand()%nums.size()]; 31 | } 32 | 33 | private: 34 | vector nums; 35 | unordered_map mp; 36 | }; 37 | 38 | /** 39 | * Your RandomizedSet object will be instantiated and called as such: 40 | * RandomizedSet obj = new RandomizedSet(); 41 | * bool param_1 = obj.insert(val); 42 | * bool param_2 = obj.remove(val); 43 | * int param_3 = obj.getRandom(); 44 | */ -------------------------------------------------------------------------------- /39. Combination Sum.cpp: -------------------------------------------------------------------------------- 1 | 39. Combination Sum 2 | class Solution { 3 | //backtracking,the begin and sort operation guarantee no duplicate combinations 4 | public: 5 | void core(vector& candidates,vector>&res,vector path,int target,int begin){ 6 | if(target==0) { 7 | res.push_back(path); 8 | return; 9 | } 10 | for(int i=begin;i=0) { 13 | path.push_back(candidates[i]); 14 | core(candidates,res,path,diff,i); 15 | path.pop_back(); 16 | } 17 | } 18 | return; 19 | } 20 | vector> combinationSum(vector& candidates, int target) { 21 | vector> res; 22 | vector path; 23 | sort(candidates.begin(),candidates.end()); 24 | core(candidates,res,path,target,0); 25 | vector> ress; 26 | return res; 27 | } 28 | }; -------------------------------------------------------------------------------- /416.Partition Equal Subset Sum.cpp: -------------------------------------------------------------------------------- 1 | 416. Partition Equal Subset Sum 2 | class Solution { 3 | public: 4 | bool canPartition(vector& nums) { 5 | int sum=accumulate(nums.begin(),nums.end(),0); 6 | if(sum&1) return false; 7 | //dp 8 | int target=sum>>1; 9 | vector dp(target+1); 10 | dp[0]=1; 11 | for(auto num:nums){ 12 | for(int i=target;i>=num;i--) 13 | dp[i]+=dp[i-num]; 14 | } 15 | return dp.back(); 16 | } 17 | }; -------------------------------------------------------------------------------- /437.Path Sum III.cpp: -------------------------------------------------------------------------------- 1 | 437.Path Sum III 2 | /** 3 | * Definition for a binary tree node. 4 | * struct TreeNode { 5 | * int val; 6 | * TreeNode *left; 7 | * TreeNode *right; 8 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | int sumup(TreeNode* root,int pre,int& sum){ 14 | if(root==nullptr) return 0; 15 | int current =pre+root->val; 16 | return (current==sum)+sumup(root->left,current,sum)+sumup(root->right,current,sum); 17 | } 18 | int pathSum(TreeNode* root, int sum) { 19 | if(root==nullptr) return 0; 20 | return sumup(root,0,sum)+pathSum(root->left,sum)+pathSum(root->right,sum); 21 | } 22 | }; -------------------------------------------------------------------------------- /494.Target Sum.cpp: -------------------------------------------------------------------------------- 1 | 494.Target Sum 2 | class Solution { 3 | public: 4 | int findTargetSumWays(vector& nums, int S) { 5 | int sum=accumulate(nums.begin(),nums.end(),0); 6 | if((S+sum)&1) return 0; 7 | int target=(S+sum)/2; 8 | if(target>sum) return 0; 9 | vector res(target+1); 10 | res[0]=1; 11 | for(auto num:nums){ 12 | for(int i=target;i>=num;i--){ 13 | res[i]+=res[i-num]; 14 | } 15 | } 16 | return res.back(); 17 | } 18 | }; -------------------------------------------------------------------------------- /560.Subarray Sum Equals K.cpp: -------------------------------------------------------------------------------- 1 | 560.Subarray Sum Equals K 2 | class Solution { 3 | public: 4 | int subarraySum(vector& nums, int k) { 5 | int res=0,sum=0; 6 | map record; 7 | record[0]=1; 8 | for(int i=0;i> dp(n,vector(n)); 7 | for(int end=0;endend-1||dp[start+1][end-1])){ 12 | dp[start][end]=1; 13 | ++count; 14 | } 15 | } 16 | } 17 | return count; 18 | } 19 | }; -------------------------------------------------------------------------------- /77.Combinations.cpp: -------------------------------------------------------------------------------- 1 | 77.Combinations 2 | class Solution { 3 | public: 4 | void core(int &n,int k,int begin,vector>&res,vector path){ 5 | if(k==0){ 6 | res.push_back(path); 7 | return; 8 | } 9 | for(int i=begin;i<=n;i++){ 10 | path.push_back(i); 11 | k--; 12 | core(n,k,i+1,res,path); 13 | k++; 14 | path.pop_back(); 15 | } 16 | return; 17 | } 18 | vector> combine(int n, int k) { 19 | vector> res; 20 | vectorpath; 21 | //vector visit(n,false); 22 | core(n,k,1,res,path); 23 | return res; 24 | } 25 | }; -------------------------------------------------------------------------------- /78.Subsets.cpp: -------------------------------------------------------------------------------- 1 | 78.Subsets 2 | class Solution { 3 | public: 4 | void core(vector& nums,vector>&res,vector path,int begin){ 5 | res.push_back(path); 6 | if(begin==nums.size()){ 7 | return; 8 | } 9 | for(int i=begin;i> subsets(vector& nums) { 16 | vector> res; 17 | vector path; 18 | core(nums,res,path,0); 19 | return res; 20 | } 21 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MyLeetCode 2 | - Here are the solutions for the most popular problems on leetcode, it is interesting and thinkworthy. 3 | 4 | ## Framework 5 | - LeetCode OJ 6 | 7 | ## Code 8 | - Language C++ 9 | 10 | ## to be continued 11 | > Feel free to contact me if you have any issues or better ideas about anything. 12 | 13 | > by Holy 14 | --------------------------------------------------------------------------------