├── README.md ├── String ├── 344. Reverse String.py ├── 3. Longest Substring Without Repeating Characters.cpp └── 345. Reverse Vowels of a String.py ├── Array ├── 283. Move Zeroes.cpp ├── 922.Sort Array By Parity II.java ├── 35.Search Insert Position.java ├── 27. Remove Element.cpp ├── 9. Palindrome Number ├── 4.Median of Two Sorted Arrays.java └── 26. Remove Duplicates from Sorted Array.cpp ├── shell ├── 195. Tenth Line.txt ├── 193. Valid Phone Numbers.txt ├── 192. Word Frequency.txt └── 194. Transpose File.txt ├── database ├── 175. Combine Two Tables.txt ├── 176. Second Highest Salary.txt ├── 595. Big Countries.txt ├── 596. Classes More Than 5 Students.txt ├── 178.Rank Scores.txt ├── 620. Not Boring Movies.txt ├── 182. Duplicate Emails.txt ├── 180.Consecutive Numbers.txt ├── 197. Rising Temperature.txt ├── 181. Employees Earning More Than Their Managers.txt ├── 196. Delete Duplicate Emails.txt ├── 185. Department Top Three Salaries.txt ├── 183. Customers Who Never Order.txt ├── 626. Exchange Seats.txt ├── 627. Swap Salary.txt ├── 184. Department Highest Salary.txt ├── 177.Nth Highest Salary.txt ├── 262. Trips and Users.txt └── 601. Human Traffic of Stadium.txt ├── Data Structure ├── queue │ ├── 279.Perfect Squares.cpp │ └── 347. Top K Frequent Elements.cpp ├── Linked list │ ├── 206.Reverse Linked List.cpp │ ├── 2. Add Two Numbers.java │ ├── 203.Remove Linked List Elements.cpp │ ├── 237.Delete Node in a Linked List.cpp │ ├── 24.Swap Nodes in Pairs.cpp │ └── 19.Remove Nth Node From End of List.cpp ├── Tree │ ├── 102.Binary Tree Level Order Traversal.cpp │ └── 144. Binary Tree Preorder Traversal.cpp ├── Set │ └── 3. Longest Substring Without Repeating Characters.cpp └── Stack │ └── 20.Valid Parentheses.cpp ├── Two Pointers双指针(对撞指针) ├── 167. Two Sum II - Input array is sorted.java ├── 125. Valid Palindrome.py ├── 88. Merge Sorted Array.cpp ├── 75. Sort Colors.py ├── 75. Sort Colors.cpp ├── 80. Remove Duplicates from Sorted Array II (2).java ├── 80. Remove Duplicates from Sorted Array II.cpp ├── 11. Container With Most Water.java └── 209. Minimum Size Subarray Sum.cpp ├── Basic logic ├── Pow(x, n).java └── Reverse Integer.java ├── Algorithm ├── 215. Kth Largest Element in an Array.cpp └── 215. Kth Largest Element in an Array.java ├── 确定有穷状态自动机(DFA) ├── 215. Kth Largest Element in an Array.cpp └── 215. Kth Largest Element in an Array.java ├── BinarySearch ├── 1. Two Sum.java └── 374.GuessNumberHigherorLower.java ├── LICENSE └── classification.md /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode-MyCodesCollection 2 | 我的LeetCode编程题做题代码 3 | 4 | 5 | 坚持做题,锻炼思维,代码会同步上传到我的这个repo 6 | -------------------------------------------------------------------------------- /String/344. Reverse String.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseString(self, s): 3 | return s[::-1] -------------------------------------------------------------------------------- /Array/283. Move Zeroes.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/29DCH/LeetCode-MyCodesCollection/HEAD/Array/283. Move Zeroes.cpp -------------------------------------------------------------------------------- /shell/195. Tenth Line.txt: -------------------------------------------------------------------------------- 1 | awk 'NR==10' file.txt 2 | 3 | sed -n '10p' file.txt 4 | 5 | tail -n +10 file.txt | head -n 1 6 | -------------------------------------------------------------------------------- /database/175. Combine Two Tables.txt: -------------------------------------------------------------------------------- 1 | select a.FirstName,a.LastName,b.City,b.State from Person a LEFT JOIN Address b on a.PersonId=b.PersonId -------------------------------------------------------------------------------- /database/176. Second Highest Salary.txt: -------------------------------------------------------------------------------- 1 | select MAX(Salary) as SecondHighestSalary from Employee where Salary < (select MAX(Salary) from Employee) -------------------------------------------------------------------------------- /Data Structure/queue/279.Perfect Squares.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/29DCH/LeetCode-MyCodesCollection/HEAD/Data Structure/queue/279.Perfect Squares.cpp -------------------------------------------------------------------------------- /database/595. Big Countries.txt: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select name,population,area 3 | from World 4 | where area>3000000 or population>25000000 -------------------------------------------------------------------------------- /shell/193. Valid Phone Numbers.txt: -------------------------------------------------------------------------------- 1 | sed -n -r '/^([0-9]{3}-|([0-9]{3}) )[0-9]{3}-[0-9]{4}$/p' file.txt 2 | 3 | grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt 4 | -------------------------------------------------------------------------------- /database/596. Classes More Than 5 Students.txt: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select class 3 | from courses 4 | group by class 5 | having count(distinct(student))>=5 -------------------------------------------------------------------------------- /database/178.Rank Scores.txt: -------------------------------------------------------------------------------- 1 | SELECT S.Score, COUNT(DISTINCT T.Score) AS Rank 2 | FROM Scores S 3 | JOIN Scores T ON S.Score <= T.Score 4 | GROUP BY S.Id 5 | ORDER BY S.Score DESC 6 | -------------------------------------------------------------------------------- /database/620. Not Boring Movies.txt: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select * 3 | from cinema 4 | where mod(id,2) = 1 and description <> 'boring' 5 | order by rating desc -------------------------------------------------------------------------------- /database/182. Duplicate Emails.txt: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | 3 | select distinct p1.Email 4 | 5 | from Person p1, Person p2 6 | where p1.Email=p2.Email and p1.idw1.Temperature -------------------------------------------------------------------------------- /Basic logic/Pow(x, n).java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double myPow(double x, int n) { 3 | if(x>=0) 4 | return Math.pow(x,n); 5 | else 6 | return 1/Math.pow(x,-n); 7 | } 8 | } -------------------------------------------------------------------------------- /database/181. Employees Earning More Than Their Managers.txt: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | select e1.Name as Employee 3 | from Employee e1, Employee e2 4 | where e1.ManagerId=e2.Id and e1.Salary>e2.Salary -------------------------------------------------------------------------------- /shell/192. Word Frequency.txt: -------------------------------------------------------------------------------- 1 | awk '{for(i=1;i<=NF;++i){++m[$i]}}END{for(k in m){print k,m[k]}}' words.txt | sort -nr -k 2 2 | 3 | cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -rn | awk '{print $2, $1}' 4 | -------------------------------------------------------------------------------- /Algorithm/215. Kth Largest Element in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findKthLargest(vector& nums, int k) { 4 | sort(nums.begin() , nums.end() ) ; 5 | return nums[nums.size() - k] ; 6 | } 7 | }; -------------------------------------------------------------------------------- /Two Pointers双指针(对撞指针)/125. Valid Palindrome.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, s): 3 | s = s.lower() 4 | s = ''.join(e for e in s if e.isalnum()) 5 | return s == s[::-1] 6 | //re.sub('[^A-Za-z0-9]+', '', s) -------------------------------------------------------------------------------- /确定有穷状态自动机(DFA)/215. Kth Largest Element in an Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findKthLargest(vector& nums, int k) { 4 | sort(nums.begin() , nums.end() ) ; 5 | return nums[nums.size() - k] ; 6 | } 7 | }; -------------------------------------------------------------------------------- /database/196. Delete Duplicate Emails.txt: -------------------------------------------------------------------------------- 1 | delete p1 2 | from Person p1, Person p2 3 | where p1.Email=p2.Email and p1.Id>p2.Id 4 | 5 | delete from 6 | Person 7 | where 8 | Id not in (select Id from (select min(Id) as Id from Person group by Email) p); -------------------------------------------------------------------------------- /shell/194. Transpose File.txt: -------------------------------------------------------------------------------- 1 | awk '{ 2 | for (i = 1; i <= NF; ++i) { 3 | if (NR == 1) s[i] = $i; 4 | else s[i] = s[i] " " $i; 5 | } 6 | } END { 7 | for (i = 1; s[i] != ""; ++i) { 8 | print s[i]; 9 | } 10 | }' file.txt 11 | -------------------------------------------------------------------------------- /database/185. Department Top Three Salaries.txt: -------------------------------------------------------------------------------- 1 | select d.Name as Department, e.Name as Employee, Salary 2 | from Employee e inner join Department d on e.DepartmentId = d.Id 3 | where 3>(select count(distinct e1.Salary) from Employee e1 where e1.Salary>e.Salary and e1.DepartmentId = e.DepartmentId) -------------------------------------------------------------------------------- /database/183. Customers Who Never Order.txt: -------------------------------------------------------------------------------- 1 | SELECT Name AS Customers 2 | FROM Customers WHERE Id NOT IN 3 | (SELECT CustomerId FROM Orders); 4 | 5 | 6 | SELECT Name AS Customers 7 | FROM Customers AS c 8 | LEFT JOIN Orders AS o 9 | ON c.Id = o.CustomerId 10 | WHERE o.Id IS NULL; 11 | -------------------------------------------------------------------------------- /database/626. Exchange Seats.txt: -------------------------------------------------------------------------------- 1 | select case when id = (select max(id) from seat) and mod(id, 2) = 1 then id 2 | when id < (select max(id) from seat) and mod(id, 2) = 1 then id + 1 3 | when mod(id, 2) = 0 then id - 1 4 | end as id,student 5 | from seat 6 | order by id -------------------------------------------------------------------------------- /database/627. Swap Salary.txt: -------------------------------------------------------------------------------- 1 | Update salary 2 | SET sex = CASE sex WHEN 'f' THEN 'm' ELSE 'f' END; 3 | 4 | update salary set sex=IF(sex='m','f', 'm') 5 | 6 | UPDATE salary 7 | SET sex = CHAR(ASCII('f') ^ ASCII('m') ^ ASCII(sex)) 8 | 9 | UPDATE salary 10 | SET sex = CHAR(ASCII(sex) ^ 11) -------------------------------------------------------------------------------- /database/184. Department Highest Salary.txt: -------------------------------------------------------------------------------- 1 | select d.Name as Department, e.Name as Employee, e.Salary 2 | from Department d,Employee e, (select MAX(Salary) as Salary, DepartmentId as DepartmentId from Employee GROUP BY DepartmentId) h 3 | where e.Salary = h.Salary and e.DepartmentId = h.DepartmentId and e.DepartmentId = d.Id; -------------------------------------------------------------------------------- /database/177.Nth Highest Salary.txt: -------------------------------------------------------------------------------- 1 | CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT 2 | BEGIN 3 | declare M INT; 4 | set M=N-1; 5 | RETURN ( 6 | # Write your MySQL query statement below. 7 | select distinct Salary 8 | from Employee order by Salary desc 9 | limit M,1 10 | ); 11 | END 12 | -------------------------------------------------------------------------------- /Two Pointers双指针(对撞指针)/88. Merge Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector &nums1, int m, vector &nums2, int n) { 4 | int i = m - 1, j = n - 1, p = m + n - 1; 5 | while (j >= 0) { 6 | nums1[p--] = (i >= 0 && nums1[i] > nums2[j]) ? nums1[i--] : nums2[j--]; 7 | } 8 | } 9 | }; -------------------------------------------------------------------------------- /BinarySearch/1. Two Sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | for(int i=0;inext; 9 | cur->next = pre; 10 | pre = cur; 11 | cur = next; 12 | } 13 | return pre; 14 | } 15 | }; -------------------------------------------------------------------------------- /Two Pointers双指针(对撞指针)/75. Sort Colors.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortColors(self, nums): 3 | zero = -1 4 | i = 0 5 | two = len(nums) 6 | while i < two: 7 | if nums[i] == 1: 8 | i += 1 9 | elif nums[i] == 2: 10 | two -= 1 11 | nums[two], nums[i] = nums[i], nums[two] 12 | else: 13 | zero += 1 14 | nums[zero], nums[i] = nums[i], nums[zero] 15 | i += 1 -------------------------------------------------------------------------------- /Array/922.Sort Array By Parity II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] sortArrayByParityII(int[] A) { 3 | int[] a=new int[10010]; 4 | int[] b=new int[10010]; 5 | int j=0,k=0; 6 | for(int i=0;inums[nums.length-1]) 10 | return nums.length; 11 | if(0<=i&&inums[i]&&target 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int removeElement(vector& nums, int val) { 7 | int k=0; 8 | for( int i = 0 ; i < nums.size() ; i ++ ) { 9 | if ( nums[i] != val) 10 | nums[k ++] = nums[i]; 11 | } 12 | return k; 13 | } 14 | }; 15 | 16 | int main() 17 | { 18 | int arr[] = {0, 1, 0, 3, 12}; 19 | vector vec(arr, arr + sizeof(arr)/sizeof(int) ); 20 | cout << Solution().removeElement( vec , 0 ) << endl; 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Basic logic/Reverse Integer.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reverse(int x) { 3 | int n=x>0?x:-x; 4 | int a=0; 5 | long b=0; 6 | while(n>0) 7 | { 8 | a=n%10; 9 | b=b*10+a; 10 | n/=10; 11 | } 12 | if(x>0&&b>Integer.MAX_VALUE) 13 | return 0; 14 | else if(x>=0&&b<=Integer.MAX_VALUE) 15 | return (int)b; 16 | else if(x<0&&(-b)>=Integer.MIN_VALUE) 17 | return -(int)b; 18 | else if(x<0&&(-b)& nums) { 4 | int i = -1,j = -1,k = -1; 5 | int m; 6 | for(m = 0; m < nums.size();m++){ 7 | if(nums[m] == 0){ 8 | nums[++k] = 2; 9 | nums[++j] = 1; 10 | nums[++i] = 0; 11 | } 12 | else if(nums[m] == 1){ 13 | nums[++k] = 2; 14 | nums[++j] = 1; 15 | } 16 | else { 17 | nums[++k] = 2; 18 | } 19 | } 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Array/9. Palindrome Number: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | if(x<0) 5 | return false; 6 | int temp[100]={0},i=0; 7 | while(x) 8 | { 9 | temp[i++]=x%10; 10 | x/=10; 11 | } 12 | for(int j=0;j nums[i - 2]) { 6 | nums[i++] = num; 7 | } 8 | } 9 | return i; 10 | } 11 | } 12 | 13 | 14 | public class Solution { 15 | public int removeDuplicates(int[] nums) { 16 | if (nums == null || nums.length == 0) return 0; 17 | int limit = 2, count = 1, lo = 1; 18 | for (int i = 1; i < nums.length; i++) { 19 | count = (nums[i] == nums[i - 1]) ? count + 1 : 1 ; 20 | if (count <= limit) nums[lo++] = nums[i]; 21 | } 22 | return lo; 23 | } 24 | } -------------------------------------------------------------------------------- /Array/4.Median of Two Sorted Arrays.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public double findMedianSortedArrays(int[] nums1, int[] nums2) { 4 | int nums1L = nums1.length; 5 | int nums2L = nums2.length; 6 | double result = 0; 7 | int i = 0, j = 0, k = 0; 8 | int[] num = new int[nums1L + nums2L]; 9 | while(i < nums1L && j < nums2L){ 10 | if(nums1[i] < nums2[j]) num[k++] = nums1[i++]; 11 | else num[k++] = nums2[j++]; 12 | } 13 | while(i == nums1L && j < nums2L){ 14 | num[k++] = nums2[j++]; 15 | } 16 | while(i < nums1L && j == nums2L){ 17 | num[k++] = nums1[i++]; 18 | } 19 | if(num.length % 2 == 0){ 20 | result = ((double)(num[num.length / 2] + num[num.length / 2 -1]))/2; 21 | } 22 | else{ 23 | result = (double)(num[num.length / 2]); 24 | } 25 | return result; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Data Structure/Tree/102.Binary Tree Level Order Traversal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> levelOrder(TreeNode* root) { 4 | vector> res; 5 | if(root==NULL) 6 | return res; 7 | queue > q; 8 | q.push(make_pair(root,0)); 9 | while(!q.empty()) 10 | { 11 | TreeNode* node = q.front().first; 12 | int level = q.front().second; 13 | q.pop(); 14 | if(level == res.size()) 15 | res.push_back(vector()); 16 | res[level].push_back(node->val); 17 | if(node->left) 18 | q.push(make_pair(node->left,level+1) ); 19 | if(node->right) 20 | q.push(make_pair(node->right,level+1) ); 21 | } 22 | return res; 23 | } 24 | }; -------------------------------------------------------------------------------- /Two Pointers双指针(对撞指针)/80. Remove Duplicates from Sorted Array II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int removeDuplicates(vector& nums) { 7 | int k=0; 8 | if (nums.size() == 0) return 0; 9 | nums[k ++] = nums[0]; 10 | for( int i = 1 ; i < nums.size(); i ++ ) 11 | if( !(nums[i] == nums[i - 1] && nums[i] == nums[i + 1])) 12 | nums[k ++] = nums[i] ; 13 | return k ; 14 | } 15 | }; 16 | 17 | int main() 18 | { 19 | int arr[] = {0,0,0,0}; 20 | vector vec(arr, arr + sizeof(arr)/sizeof(int) ); 21 | cout << Solution().removeDuplicates( vec ) << endl; 22 | for( int i = 0 ; i < vec.size() - 1 ; i ++ ) 23 | cout << vec[i] << " "; 24 | cout << vec [ vec.size() - 1 ] << endl; 25 | return 0; 26 | } 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /String/3. Longest Substring Without Repeating Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | set t; 5 | int res = 0, left = 0, right = 0; 6 | while (right < s.size()) { 7 | if (t.find(s[right]) == t.end()) { 8 | t.insert(s[right++]); 9 | res = max(res, (int)t.size()); 10 | } else { 11 | t.erase(s[left++]); 12 | } 13 | } 14 | return res; 15 | } 16 | }; 17 | 18 | class Solution { 19 | public: 20 | int lengthOfLongestSubstring(string s) { 21 | vector m(256, -1); 22 | int res = 0, left = -1; 23 | for (int i = 0; i < s.size(); ++i) { 24 | left = max(left, m[s[i]]); 25 | m[s[i]] = i; 26 | res = max(res, i - left); 27 | } 28 | return res; 29 | } 30 | }; -------------------------------------------------------------------------------- /Data Structure/Set/3. Longest Substring Without Repeating Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | set t; 5 | int res = 0, left = 0, right = 0; 6 | while (right < s.size()) { 7 | if (t.find(s[right]) == t.end()) { 8 | t.insert(s[right++]); 9 | res = max(res, (int)t.size()); 10 | } else { 11 | t.erase(s[left++]); 12 | } 13 | } 14 | return res; 15 | } 16 | }; 17 | 18 | class Solution { 19 | public: 20 | int lengthOfLongestSubstring(string s) { 21 | vector m(256, -1); 22 | int res = 0, left = -1; 23 | for (int i = 0; i < s.size(); ++i) { 24 | left = max(left, m[s[i]]); 25 | m[s[i]] = i; 26 | res = max(res, i - left); 27 | } 28 | return res; 29 | } 30 | }; -------------------------------------------------------------------------------- /Data Structure/Tree/144. Binary Tree Preorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | //递归 2 | 3 | class Solution { 4 | public: 5 | vector res; 6 | vector preorderTraversal(TreeNode* root) { 7 | if(root!=NULL) 8 | { 9 | res.push_back(root->val); 10 | preorderTraversal(root->left); 11 | preorderTraversal(root->right); 12 | } 13 | return res; 14 | } 15 | }; 16 | 17 | //迭代 18 | 19 | class Solution { 20 | public: 21 | vector preorderTraversal(TreeNode *root) { 22 | vector res; 23 | stack tempStack; 24 | while (!tempStack.empty() || root != NULL) { 25 | if (root != NULL) { 26 | res.push_back(root->val); 27 | tempStack.push(root); 28 | root = root->left; 29 | } 30 | else { 31 | root = tempStack.top(); 32 | tempStack.pop(); 33 | root = root->right; 34 | } 35 | } 36 | return res; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /Data Structure/queue/347. Top K Frequent Elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector topKFrequent(vector& nums, int k) 5 | { 6 | unordered_map freq; 7 | for(int i=0; i,vector>, greater> > pq; 12 | for(unordered_map::iterator iter = freq.begin(); iter!=freq.end(); iter++) 13 | { 14 | if(pq.size()==k) 15 | { 16 | if(iter->second>pq.top().first) 17 | { 18 | pq.pop(); 19 | pq.push(make_pair(iter->second,iter->first)); 20 | } 21 | } 22 | else 23 | pq.push(make_pair(iter->second,iter->first)); 24 | } 25 | vector res; 26 | while(!pq.empty()) 27 | { 28 | res.push_back(pq.top().second); 29 | pq.pop(); 30 | } 31 | return res; 32 | } 33 | }; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 刘继强 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Two Pointers双指针(对撞指针)/11. Container With Most Water.java: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public int MaxArea(int[] h) 4 | { 5 | int l = 0, r = h.Length - 1; 6 | int maxArea = 0; 7 | while (l < r && l >= 0 && r <= h.Length - 1) 8 | { 9 | maxArea = Math.Max(maxArea, Math.Min(h[l], h[r]) * (r - l)); 10 | if (h[l] > h[r]) 11 | { 12 | r--; 13 | } 14 | else 15 | { 16 | l++; 17 | } 18 | } 19 | return maxArea; 20 | } 21 | } 22 | 23 | 24 | class Solution { 25 | public: 26 | int maxArea(vector& h) { 27 | int l = 0, r = h.size() - 1; 28 | int maxArea = 0; 29 | while (l < r && l >= 0 && r <= h.size() - 1) 30 | { 31 | maxArea = max(maxArea,min(h[l],h[r])*(r-l)); 32 | if (h[l] > h[r]) 33 | { 34 | r--; 35 | } 36 | else 37 | { 38 | l++; 39 | } 40 | } 41 | return maxArea; 42 | } 43 | }; -------------------------------------------------------------------------------- /database/601. Human Traffic of Stadium.txt: -------------------------------------------------------------------------------- 1 | select s.id,s.date,s.people from stadium s where people >= 100 and 2 | ( 3 | ( 4 | (select people from stadium where id = s.id + 1) >= 100 5 | and 6 | (select people from stadium where id = s.id - 1) >= 100 7 | ) 8 | or 9 | ( 10 | (select people from stadium where id = s.id - 1) >= 100 11 | and 12 | (select people from stadium where id = s.id - 2) >= 100 13 | ) 14 | or 15 | ( 16 | (select people from stadium where id = s.id + 1) >= 100 17 | and 18 | (select people from stadium where id = s.id + 2) >= 100 19 | ) 20 | ); 21 | 22 | 23 | select * from stadium s where s.people >= 100 and 24 | ( 25 | ( 26 | s.id+1 in (select id from stadium where people >= 100) 27 | and 28 | s.id+2 in (select id from stadium where people >= 100) 29 | ) 30 | or 31 | ( 32 | s.id-1 in (select id from stadium where people >= 100) 33 | and 34 | s.id+1 in (select id from stadium where people >= 100) 35 | ) 36 | or 37 | ( 38 | s.id-1 in (select id from stadium where people >= 100) 39 | and 40 | s.id-2 in (select id from stadium where people >= 100) 41 | ) 42 | ); 43 | -------------------------------------------------------------------------------- /Array/26. Remove Duplicates from Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /**class Solution { 5 | public: 6 | int removeDuplicates(vector& nums) { 7 | int k=0; 8 | set S; 9 | for( int i = 0 ; i < nums.size(); i ++ ) 10 | S.insert( nums[i] ); 11 | for( set::iterator it=S.begin() ; it!=S.end() ; it++) 12 | nums[k ++] = *it; 13 | return S.size(); 14 | } 15 | }; 16 | */ 17 | class Solution { 18 | public: 19 | int removeDuplicates(vector& nums) { 20 | int k=0; 21 | if (nums.size() == 0) return 0; 22 | nums[k ++] = nums[0]; 23 | for( int i = 1 ; i < nums.size(); i ++ ) 24 | if( nums[i] != nums[i - 1]) 25 | nums[k ++] = nums[i] ; 26 | return k ; 27 | } 28 | }; 29 | 30 | int main() 31 | { 32 | int arr[] = {0,0,1,2,2,3}; 33 | vector vec(arr, arr + sizeof(arr)/sizeof(int) ); 34 | cout << Solution().removeDuplicates( vec ) << endl; 35 | for( int i = 0 ; i < vec.size() - 1 ; i ++ ) 36 | cout << vec[i] << " "; 37 | cout << vec [ vec.size() - 1 ] << endl; 38 | return 0; 39 | } 40 | 41 | 42 | -------------------------------------------------------------------------------- /Data Structure/Linked list/2. Add Two Numbers.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { val = x; } 7 | * } 8 | */ 9 | class Solution { 10 | public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 11 | if (l1 == null || l2 == null) { 12 | return null; 13 | } 14 | ListNode temp = new ListNode(0); 15 | ListNode result = temp; 16 | 17 | int value1 = 0; 18 | int value2 = 0; 19 | while (l1 != null && l2 != null) { 20 | value2 = (l1.val + l2.val + value1) % 10; 21 | value1 = (l1.val + l2.val + value1) / 10; 22 | temp.next = new ListNode(value2); 23 | l1 = l1.next; 24 | l2 = l2.next; 25 | temp = temp.next; 26 | if (l1 == null && l2 == null) { 27 | break; 28 | } 29 | if (l1 == null) { 30 | l1 = new ListNode(0); 31 | } 32 | if (l2 == null) { 33 | l2 = new ListNode(0); 34 | } 35 | } 36 | if (value1 != 0) { 37 | temp.next = new ListNode(value1); 38 | } 39 | return result.next; 40 | } 41 | } -------------------------------------------------------------------------------- /String/345. Reverse Vowels of a String.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def reverseVowels(self, s): 3 | res = list(s) 4 | vowels = [] 5 | for i in range(len(res)): 6 | if res[i] in ['a', 'o', 'e', 'i', 'u', 'A', 'O', 'E', 'I', 'U']: 7 | vowels.append((i, res[i])) 8 | for j in range(len(vowels)//2): 9 | res[vowels[j][0]] = vowels[len(vowels)-j-1][1] 10 | res[vowels[len(vowels)-j-1][0]] = vowels[j][1] 11 | return ''.join(res) 12 | 13 | 14 | 15 | class Solution(object): 16 | def reverseVowels(self, s): 17 | vowels = re.findall('(?i)[aeiouAEIOU]', s) 18 | return re.sub('(?i)[aeiouAEIOU]', lambda m: vowels.pop(), s) 19 | 20 | 21 | class Solution(object): 22 | def reverseVowels(self, s): 23 | """ 24 | :type s: str 25 | :rtype: str 26 | """ 27 | vowels = {'a': True, 'o': True, 'e': True, 'i': True, 'u': True, 'A': True, 'O': True, 'E': True, 'I': True, 'U': True} 28 | res = list(s) 29 | pos = [] 30 | for i in xrange(len(res)): 31 | if res[i] in vowels: 32 | pos.append((i, res[i])) 33 | for j in xrange(len(pos)/2): 34 | res[pos[j][0]] = pos[len(pos)-j-1][1] 35 | res[pos[len(pos)-j-1][0]] = pos[j][1] 36 | return ''.join(res) -------------------------------------------------------------------------------- /Algorithm/215. Kth Largest Element in an Array.java: -------------------------------------------------------------------------------- 1 | //quickselect 2 | public class Solution { 3 | public int findKthLargest(int[] nums, int k) { 4 | 5 | shuffle(nums); 6 | k = nums.length - k; 7 | int lo = 0; 8 | int hi = nums.length - 1; 9 | while (lo < hi) { 10 | final int j = partition(nums, lo, hi); 11 | if(j < k) { 12 | lo = j + 1; 13 | } else if (j > k) { 14 | hi = j - 1; 15 | } else { 16 | break; 17 | } 18 | } 19 | return nums[k]; 20 | } 21 | 22 | private void shuffle(int a[]) { 23 | 24 | final Random random = new Random(); 25 | for(int ind = 1; ind < a.length; ind++) { 26 | final int r = random.nextInt(ind + 1); 27 | exch(a, ind, r); 28 | } 29 | } 30 | 31 | private int partition(int[] a, int lo, int hi) { 32 | 33 | int i = lo; 34 | int j = hi + 1; 35 | while(true) { 36 | while(i < hi && less(a[++i], a[lo])); 37 | while(j > lo && less(a[lo], a[--j])); 38 | if(i >= j) { 39 | break; 40 | } 41 | exch(a, i, j); 42 | } 43 | exch(a, lo, j); 44 | return j; 45 | } 46 | 47 | private void exch(int[] a, int i, int j) { 48 | final int tmp = a[i]; 49 | a[i] = a[j]; 50 | a[j] = tmp; 51 | } 52 | 53 | private boolean less(int v, int w) { 54 | return v < w; 55 | } 56 | } -------------------------------------------------------------------------------- /确定有穷状态自动机(DFA)/215. Kth Largest Element in an Array.java: -------------------------------------------------------------------------------- 1 | //quickselect 2 | public class Solution { 3 | public int findKthLargest(int[] nums, int k) { 4 | 5 | shuffle(nums); 6 | k = nums.length - k; 7 | int lo = 0; 8 | int hi = nums.length - 1; 9 | while (lo < hi) { 10 | final int j = partition(nums, lo, hi); 11 | if(j < k) { 12 | lo = j + 1; 13 | } else if (j > k) { 14 | hi = j - 1; 15 | } else { 16 | break; 17 | } 18 | } 19 | return nums[k]; 20 | } 21 | 22 | private void shuffle(int a[]) { 23 | 24 | final Random random = new Random(); 25 | for(int ind = 1; ind < a.length; ind++) { 26 | final int r = random.nextInt(ind + 1); 27 | exch(a, ind, r); 28 | } 29 | } 30 | 31 | private int partition(int[] a, int lo, int hi) { 32 | 33 | int i = lo; 34 | int j = hi + 1; 35 | while(true) { 36 | while(i < hi && less(a[++i], a[lo])); 37 | while(j > lo && less(a[lo], a[--j])); 38 | if(i >= j) { 39 | break; 40 | } 41 | exch(a, i, j); 42 | } 43 | exch(a, lo, j); 44 | return j; 45 | } 46 | 47 | private void exch(int[] a, int i, int j) { 48 | final int tmp = a[i]; 49 | a[i] = a[j]; 50 | a[j] = tmp; 51 | } 52 | 53 | private boolean less(int v, int w) { 54 | return v < w; 55 | } 56 | } -------------------------------------------------------------------------------- /Data Structure/Linked list/203.Remove Linked List Elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | ListNode* removeElements(ListNode* head, int val) 5 | { 6 | ListNode* dummyhead = new ListNode(0); 7 | dummyhead->next = head; 8 | ListNode* curNode = dummyhead; 9 | while(curNode->next!=NULL) 10 | { 11 | if(curNode->next->val==val) 12 | { 13 | ListNode* delNode = curNode->next; 14 | curNode->next = delNode->next; 15 | delete delNode; 16 | } 17 | else 18 | curNode=curNode->next; 19 | } 20 | ListNode* retNode = dummyhead->next; 21 | delete dummyhead; 22 | return retNode; 23 | } 24 | }; 25 | 26 | class Solution 27 | { 28 | public: 29 | ListNode* removeElements(ListNode* head, int val) 30 | { 31 | while(head != NULL&&head->val == val ) 32 | { 33 | ListNode* delNode = head; 34 | head = delNode->next; 35 | delete delNode; 36 | } 37 | if(head == NULL) 38 | return NULL; 39 | ListNode* curNode = head; 40 | while(curNode->next!=NULL) 41 | { 42 | if(curNode->next->val==val) 43 | { 44 | ListNode* delNode = curNode->next; 45 | curNode->next = delNode->next; 46 | delete delNode; 47 | } 48 | else 49 | curNode=curNode->next; 50 | } 51 | return head; 52 | } 53 | }; -------------------------------------------------------------------------------- /Two Pointers双指针(对撞指针)/209. Minimum Size Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSubArrayLen(int s, vector& nums) { 4 | int l=0,r=-1; 5 | int sum=0; 6 | int res=nums.size()+1; 7 | while(l=s) 14 | res=min(res,r-l+1); 15 | } 16 | if(res==nums.size()+1) 17 | return 0; 18 | return res; 19 | } 20 | }; 21 | 22 | class Solution { 23 | public: 24 | int minSubArrayLen(int s, vector& nums) { 25 | int res = INT_MAX, left = 0, sum = 0; 26 | for (int i = 0; i < nums.size(); ++i) { 27 | sum += nums[i]; 28 | while (left <= i && sum >= s) { 29 | res = min(res, i - left + 1); 30 | sum -= nums[left++]; 31 | } 32 | } 33 | return res == INT_MAX ? 0 : res; 34 | } 35 | }; 36 | 37 | 38 | class Solution { 39 | public: 40 | int minSubArrayLen(int s, vector& nums) { 41 | int res = INT_MAX, n = nums.size(); 42 | vector sums(n + 1, 0); 43 | for (int i = 1; i < n + 1; ++i) sums[i] = sums[i - 1] + nums[i - 1]; 44 | for (int i = 0; i < n; ++i) { 45 | int left = i + 1, right = n, t = sums[i] + s; 46 | while (left <= right) { 47 | int mid = left + (right - left) / 2; 48 | if (sums[mid] < t) left = mid + 1; 49 | else right = mid - 1; 50 | } 51 | if (left == n + 1) break; 52 | res = min(res, left - i); 53 | } 54 | return res == INT_MAX ? 0 : res; 55 | } 56 | }; -------------------------------------------------------------------------------- /Data Structure/Stack/20.Valid Parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool isValid(string s) 5 | { 6 | stack stack; 7 | for(int i=0; i 42 | #include 43 | #include 44 | typedef long long LL; 45 | using namespace std; 46 | class Solution 47 | { 48 | public: 49 | bool isValid(string s) 50 | { 51 | stack stack; 52 | for(int i=0; i>str) 88 | { 89 | if(Solution().isValid(str)) 90 | cout<<"合法字符串!"<next == NULL) 9 | { 10 | delete node; 11 | node = NULL; 12 | return; 13 | } 14 | node->val = node->next->val; 15 | ListNode* delNode = node->next; 16 | node->next = delNode->next; 17 | delete delNode; 18 | } 19 | }; 20 | 21 | /** 22 | #include 23 | using namespace std; 24 | 25 | struct ListNode 26 | { 27 | int val; 28 | ListNode *next; 29 | ListNode(int x) : val(x), next(NULL) {} 30 | }; 31 | 32 | ListNode* createLinkedList(int arr[], int n) 33 | { 34 | if(n == 0) 35 | return NULL; 36 | ListNode* head = new ListNode(arr[0]); 37 | ListNode* curNode = head; 38 | for(int i=1; inext = new ListNode(arr[i]); 41 | curNode = curNode->next; 42 | } 43 | return head; 44 | } 45 | 46 | void printLinkedList(ListNode* head) 47 | { 48 | ListNode* curNode = head; 49 | while(curNode!=NULL) 50 | { 51 | cout<val<<" -> "; 52 | curNode = curNode->next; 53 | } 54 | cout<<"NULL"<next; 64 | delete delNode; 65 | } 66 | } 67 | 68 | class Solution 69 | { 70 | public: 71 | void deleteNode(ListNode* node) 72 | { 73 | if(node == NULL) 74 | return; 75 | if(node->next == NULL) 76 | { 77 | delete node; 78 | node = NULL; 79 | return; 80 | } 81 | node->val = node->next->val; 82 | ListNode* delNode = node->next; 83 | node->next = delNode->next; 84 | delete delNode; 85 | } 86 | }; 87 | 88 | int main() 89 | { 90 | int arr[]= {1,2,3,4,5}; 91 | int n = sizeof(arr)/sizeof(int); 92 | ListNode* head = createLinkedList(arr,n); 93 | printLinkedList(head); 94 | Solution().deleteNode(head->next->next); 95 | printLinkedList(head); 96 | deleteLinkedList(head); 97 | return 0; 98 | } 99 | */ 100 | -------------------------------------------------------------------------------- /Data Structure/Linked list/24.Swap Nodes in Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | ListNode* swapPairs(ListNode* head) 5 | { 6 | ListNode* dummyhead = new ListNode(0); 7 | dummyhead->next = head; 8 | ListNode* p = dummyhead; 9 | while(p->next && p->next->next) 10 | { 11 | ListNode* node1 = p->next; 12 | ListNode* node2 = node1->next; 13 | ListNode* next = node2->next; 14 | node2->next = node1; 15 | node1->next = next; 16 | p->next = node2; 17 | p=node1; 18 | } 19 | ListNode* retNode = dummyhead->next; 20 | delete dummyhead; 21 | return retNode; 22 | } 23 | }; 24 | 25 | 26 | /** 27 | #include 28 | using namespace std; 29 | 30 | struct ListNode 31 | { 32 | int val; 33 | ListNode *next; 34 | ListNode(int x) : val(x), next(NULL) {} 35 | }; 36 | 37 | ListNode* createLinkedList(int arr[], int n) 38 | { 39 | if(n == 0) 40 | return NULL; 41 | ListNode* head = new ListNode(arr[0]); 42 | ListNode* curNode = head; 43 | for(int i=1; inext = new ListNode(arr[i]); 46 | curNode = curNode->next; 47 | } 48 | return head; 49 | } 50 | 51 | void printLinkedList(ListNode* head) 52 | { 53 | ListNode* curNode = head; 54 | while(curNode!=NULL) 55 | { 56 | cout<val<<" -> "; 57 | curNode = curNode->next; 58 | } 59 | cout<<"NULL"<next; 69 | delete delNode; 70 | } 71 | } 72 | 73 | class Solution 74 | { 75 | public: 76 | ListNode* swapPairs(ListNode* head) 77 | { 78 | ListNode* dummyhead = new ListNode(0); 79 | dummyhead->next = head; 80 | ListNode* p = dummyhead; 81 | while(p->next && p->next->next) 82 | { 83 | ListNode* node1 = p->next; 84 | ListNode* node2 = node1->next; 85 | ListNode* next = node2->next; 86 | node2->next = node1; 87 | node1->next = next; 88 | p->next = node2; 89 | p=node1; 90 | } 91 | ListNode* retNode = dummyhead->next; 92 | delete dummyhead; 93 | return retNode; 94 | } 95 | }; 96 | int main() 97 | { 98 | int arr[]= {1,2,3,4,5}; 99 | int n = sizeof(arr)/sizeof(int); 100 | ListNode* head = createLinkedList(arr,n); 101 | printLinkedList(head); 102 | ListNode* head1 = Solution().swapPairs(head); 103 | printLinkedList(head1); 104 | deleteLinkedList(head1); 105 | return 0; 106 | } 107 | */ 108 | -------------------------------------------------------------------------------- /Data Structure/Linked list/19.Remove Nth Node From End of List.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | ListNode* removeNthFromEnd(ListNode* head, int n) 5 | { 6 | assert(n>=0); 7 | ListNode* dummyhead = new ListNode(0); 8 | dummyhead->next = head; 9 | ListNode* p=dummyhead; 10 | ListNode* q=dummyhead; 11 | for(int i=0; inext; 15 | } 16 | while(q!=NULL) 17 | { 18 | p=p->next; 19 | q=q->next; 20 | } 21 | ListNode* delNode = p->next; 22 | p->next=delNode->next; 23 | delete delNode; 24 | ListNode* retNode = dummyhead->next; 25 | delete dummyhead; 26 | return retNode; 27 | } 28 | }; 29 | 30 | /** 31 | #include 32 | using namespace std; 33 | 34 | struct ListNode 35 | { 36 | int val; 37 | ListNode *next; 38 | ListNode(int x) : val(x), next(NULL) {} 39 | }; 40 | 41 | ListNode* createLinkedList(int arr[], int n) 42 | { 43 | if(n == 0) 44 | return NULL; 45 | ListNode* head = new ListNode(arr[0]); 46 | ListNode* curNode = head; 47 | for(int i=1; inext = new ListNode(arr[i]); 50 | curNode = curNode->next; 51 | } 52 | return head; 53 | } 54 | 55 | void printLinkedList(ListNode* head) 56 | { 57 | ListNode* curNode = head; 58 | while(curNode!=NULL) 59 | { 60 | cout<val<<" -> "; 61 | curNode = curNode->next; 62 | } 63 | cout<<"NULL"<next; 73 | delete delNode; 74 | } 75 | } 76 | 77 | class Solution 78 | { 79 | public: 80 | ListNode* removeNthFromEnd(ListNode* head, int n) 81 | { 82 | assert(n>=0); 83 | ListNode* dummyhead = new ListNode(0); 84 | dummyhead->next = head; 85 | ListNode* p=dummyhead; 86 | ListNode* q=dummyhead; 87 | for(int i=0; inext; 91 | } 92 | while(q!=NULL) 93 | { 94 | p=p->next; 95 | q=q->next; 96 | } 97 | ListNode* delNode = p->next; 98 | p->next=delNode->next; 99 | delete delNode; 100 | ListNode* retNode = dummyhead->next; 101 | delete dummyhead; 102 | return retNode; 103 | } 104 | }; 105 | 106 | int main() 107 | { 108 | int arr[]= {1,2,3,4,5}; 109 | int n = sizeof(arr)/sizeof(int); 110 | ListNode* head = createLinkedList(arr,n); 111 | printLinkedList(head); 112 | ListNode* head1 = Solution().removeNthFromEnd(head,2); 113 | printLinkedList(head1); 114 | deleteLinkedList(head1); 115 | return 0; 116 | } 117 | */ -------------------------------------------------------------------------------- /classification.md: -------------------------------------------------------------------------------- 1 | # [Array](Array/) 2 | 3 | 026. [Remove Duplicates from Sorted Array](Array/26_RemoveDuplicatesFromSortedArray.py) 4 | 027. [Remove Element](Array/27_RemoveElement.py) 5 | 031. [Next Permutation](Array/31_NextPermutation.py) 6 | 041. [First Missing Positive](Array/41_FirstMissingPositive.py) 7 | 054. [Spiral Matrix](Array/54_SpiralMatrix.py) 8 | 056. [Merge Intervals](Array/56_MergeIntervals.py) 9 | 057. [Insert Interval](Array/57_InsertInterval.py) 10 | 059. [Spiral Matrix II](Array/59_SpiralMatrixII.py) 11 | 073. [Set Matrix Zeroes](Array/73_SetMatrixZeroes.py) 12 | 118. [Pascal's Triangle](Array/118_PascalTriangle.py) 13 | 119. [Pascal's Triangle II](Array/119_PascalTriangleII.py) 14 | 164. [Maximum Gap](Array/164_MaximumGap.py) 15 | 189. [Rotate Array](Array/189_RotateArray.py) 16 | 228. [Summary Ranges](Array/228_SummaryRanges.py) 17 | 283. [Move Zeroes](Array/283_MoveZeroes.py) 18 | 289. [Game of Life](Array/289_GameOfLife.py) 19 | 20 | # [String](String/) 21 | 22 | 006. [ZigZag Conversion](String/06_ZigZagConversion.py) 23 | 008. [String to Integer](String/08_StringtoInteger.py) 24 | 014. [Longest Common Prefix](String/14_LongestCommonPrefix.py) 25 | 017. [Letter Combinations of a Phone Number](String/17_LetterCombinationsPN.py) 26 | 028. [Implement strStr()](String/28_ImplementstrStr.py) 27 | 038. [Count and Say](String/38_CountAndSay.py) 28 | 058. [Length of Last Word](String/58_LengthOfLastWord.py) 29 | 067. [Add Binary](String/67_AddBinary.py) 30 | 068. [Text Justification](String/68_TextJustification.py) 31 | 151. [Reverse Words in a String](String/151_ReverseWordsInString.py) 32 | 165. [Compare Version Numbers](String/165_CompareVersionNumbers.py) 33 | 344. [Reverse String](String/344_ReverseString.py) 34 | 35 | # [Linked List](LinkedList/) 36 | 37 | 002. [Add Two Numbers](LinkedList/02.AddTwoNumbers.py) 38 | 021. [Merge Two Sorted Lists](LinkedList/21_MergeTwoSortedLists.py) 39 | 024. [Swap Nodes in Pairs](LinkedList/24_SwapNodesInPairs.py) 40 | 025. [Reverse Nodes in k-Group](LinkedList/25_ReverseNodesIn-k-Group.py) 41 | 061. [Rotate List](LinkedList/61_RotateList.py) 42 | 076. [Minimum Window Substring](LinkedList/76_MinimumWindowSubstring.py) 43 | 082. [Remove Duplicates from Sorted List II](LinkedList/82_RemoveDuplicatesFromSortedListII.py) 44 | 083. [Remove Duplicates from Sorted List](LinkedList/83_RemoveDuplicatesFromSortedList.py) 45 | 086. [Partition List](LinkedList/86_PartitionList.py) 46 | 092. [Reverse Linked List II](LinkedList/92_ReverseLinkedListII.py) 47 | 138. [Copy List with Random Pointer](LinkedList/138_CopyListWithRandomPointer.py) 48 | 143. [Reorder List](LinkedList/143_ReorderList.py) 49 | 147. [Insertion Sort List](LinkedList/147_InsertionSortList.py) 50 | 148. [Sort List](LinkedList/148_SortList.py) 51 | 160. [Intersection of Two Linked Lists](LinkedList/160_IntersectionOfTwoLinkedLists.py) 52 | 203. [Remove Linked List Elements](LinkedList/203_RemoveLinkedListElements.py) 53 | 206. [Reverse Linked List](LinkedList/206_ReverseLinkedList.py) 54 | 234. [Palindrome Linked List](LinkedList/234_PalindromeLinkedList.py) 55 | 237. [Delete Node in a Linked List](LinkedList/237_DeleteNodeInLinkedList.py) 56 | 328. [Odd Even Linked List](LinkedList/328_OddEvenLinkedList.py) 57 | 58 | # [Tree](Tree/) 59 | 60 | 094. [Binary Tree Inorder Traversa](Tree/94_BinaryTreeInorderTraversal.py) 61 | 095. [Unique Binary Search Trees II](Tree/95_UniqueBinarySearchTreesII.py) 62 | 096. [Unique Binary Search Trees](Tree/96_UniqueBinarySearchTrees.py) 63 | 099. [Recover Binary Search Tree](Tree/99_RecoverBinarySearchTree.py) 64 | 100. [Same Tree](Tree/100_SameTree.py) 65 | 105. [Construct Binary Tree from Preorder and Inorder Traversal](Tree/105_ConstructBinaryTreePreorderInorder.py) 66 | 106. [Construct Binary Tree from Inorder and Postorder Traversal](Tree/106_ConstructBinaryTreeInorderPostorder.py) 67 | 108. [Convert Sorted Array to Binary Search Tree](Tree/108_ConvertSortedArrayToBinarySearchTree.py) 68 | 109. [Convert Sorted List to Binary Search Tree](Tree/109_ConvertSortedListToBinarySearchTree.py) 69 | 110. [Balanced Binary Tree](Tree/110_BalancedBinaryTree.py) 70 | 111. [Minimum Depth of Binary Tree](Tree/111_MinimumDepthofBinaryTree.py) 71 | 112. [Path Sum](Tree/112_PathSum.py) 72 | 113. [Path Sum II](Tree/113_PathSumII.py) 73 | 114. [Flatten Binary Tree to Linked List](Tree/114_FlattenBinaryTreeToLinkedList.py) 74 | 116. [Populating Next Right Pointers in Each Node](Tree/116_PopulatingNextRightPointersInEachNode.py) 75 | 117. [Populating Next Right Pointers in Each Node II](Tree/117_PopulatingNextRightPointersInEachNodeII.py) 76 | 124. [Binary Tree Maximum Path Sum](Tree/124_BinaryTreeMaximumPathSum.py) 77 | 144. [Binary Tree Preorder Traversal](Tree/144_BinaryTreePreorderTraversal.py) 78 | 145. [Binary Tree Postorder Traversal](Tree/145_BinaryTreePostorderTraversal.py) 79 | 173. [Binary Search Tree Iterator](Tree/173_BinarySearchTreeIterator.py) 80 | 208. [Implement Trie (Prefix Tree)](Tree/208_ImplementTrie.py) 81 | 211. [Add and Search Word](211_AddandSearchWord.py) 82 | 226. [Invert Binary Tree](Tree/226_InvertBinaryTree.py) 83 | 235. [Lowest Common Ancestor of a Binary Search Tree](Tree/235_LowestCommonAncestorOfBinarySearchTree.py) 84 | 236. [Lowest Common Ancestor of a Binary Tree](Tree/236_LowestCommonAncestorOfBinaryTree.py) 85 | 297. [Serialize and Deserialize Binary Tree](Tree/297_SerializeAndDeserializeBinaryTree.py) 86 | 331. [Verify Preorder Serialization of a Binary Tree](Tree/331_VerifyPreorderSerializationOfBinaryTree.py) 87 | 88 | # [Hash Table](HashTable/) 89 | 90 | 001. [Two Sum](HashTable/01_TwoSum.py) 91 | 003. [Longest Substring Without Repeating Characters](HashTable/03_LongestSubstringWithoutRepeatingCharacters.py) 92 | 018. `4Sum` 93 | 036. [Valid Sudoku](HashTable/36_ValidSudoku.py) 94 | 049. [Group Anagrams](HashTable/49_GroupAnagrams.py) 95 | 128. [Longest Consecutive Sequence](HashTable/128_LongestConsecutiveSequence.py) 96 | 146. [LRU Cache](HashTable/146_LRUCache_pythonic.py) 97 | 149. [Max Points on a Line](HashTable/149_MaxPointsOnLine.py) 98 | 187. [Repeated DNA Sequences](HashTable/187_RepeatedDNASequences.py) 99 | 205. [Isomorphic Strings](HashTable/205_IsomorphicStrings.py) 100 | 217. [Contains Duplicate](HashTable/217_ContainsDuplicate.py) 101 | 219. [Contains Duplicate II](working/219_ContainsDuplicateII.py) 102 | 242. [Valid Anagram](HashTable/242_ValidAnagram.py) 103 | 257. [Binary Tree Paths](DepthFirstSearch/257_BinaryTreePaths.py) 104 | 274. [H-Index](HashTable/274_H-Index.py) 105 | 290. [Word Pattern](HashTable/290_WordPattern.py) 106 | 299. [Bulls and Cows](HashTable/299_BullsAndCows.py) 107 | 349. [Intersection of Two Arrays](HashTable/349_IntersectionOfTwoArrays.py) 108 | 350. [Intersection of Two Arrays II](HashTable/350_IntersectionOfTwoArraysII.py) 109 | 110 | # [Heap](Heap/) 111 | 023. [Merge k Sorted Lists](Heap/23_MergeKSortedLists.py) 112 | 295. [Find Median from Data Stream](Heap/295_FindMedianFromDataStream.py) 113 | 347. [Top K Frequent Elements](Heap/347_TopKFrequentElements.py) 114 | 115 | # [Binary Search](BinarySearch/) 116 | 117 | 004. [Median of Two Sorted Arrays](BinarySearch/4_MedianOfTwoSortedArrays.py) 118 | 033. [Search in Rotated Sorted Array](BinarySearch/33_SearchInRotatedSortedArray.py) 119 | 034. [Search for a Range](BinarySearch/34_SearchForRange.py) 120 | 069. [Sqrt(x)](BinarySearch/69_Sqrt_x.py) 121 | 074. [Search a 2D Matrix](BinarySearch/74_Search2DMatrix.py) 122 | 081. [Search in Rotated Sorted Array II](BinarySearch/81_SearchInRotatedSortedArrayII.py) 123 | 153. [Find Minimum in Rotated Sorted Array](BinarySearch/153_FindMinimumInRotatedSortedArray.py) 124 | 154. [Find Minimum in Rotated Sorted Array II](BinarySearch/154_FindMinimumInRotatedSortedArrayII.py) 125 | 162. [Find Peak Element](BinarySearch/162_FindPeakElement.py) 126 | 222. [Count Complete Tree Nodes](BinarySearch/222_CountCompleteTreeNodes.py) 127 | 230. [Kth Smallest Element in a BST](working/230_KthSmallestElementInBST.py) 128 | 275. [H-Index II](BinarySearch/275_H-IndexII.py) 129 | 278. [First Bad Version](BinarySearch/278_FirstBadVersion.py) 130 | 367. [Valid Perfect Square](BinarySearch/367_ValidPerfectSquare.py) 131 | 132 | # [Depth-first Search](DepthFirstSearch/) 133 | 134 | 098. [Validate Binary Search Tree](DepthFirstSearch/98_ValidateBinarySearchTree.py) 135 | 126. [Word Ladder II](DepthFirstSearch/126_WordLadderII.py) 136 | 129. [Sum Root to Leaf Numbers](DepthFirstSearch/129_SumRootToLeafNumbers.py) 137 | 140. [Word Break II](DepthFirstSearch/140_WordBreakII.py) 138 | 200. [Number of Islands](DepthFirstSearch/200_NumberofIslands.py) 139 | 301. [Remove Invalid Parentheses](DepthFirstSearch/301_RemoveInvalidParentheses.py) 140 | 306. [Additive Number](DepthFirstSearch/306_AdditiveNumber.py) 141 | 142 | # [Breadth-first Search](BreadthFirstSearch/) 143 | 144 | 102. [Binary Tree Level Order Traversal](BreadthFirstSearch/102_BinaryTreeLevelOrderTraversal.py) 145 | 103. [Binary Tree Zigzag Level OrderTraversal](BreadthFirstSearch/103_BinaryTreeZigzagLevelOrderTraversal.py) 146 | 104. [Maximum Depth Of BinaryTree](BreadthFirstSearch/104_MaximumDepthOfBinaryTree.py) 147 | 107. [Binary Tree Level Order Traversal II](BreadthFirstSearch/107_BinaryTreeLevelOrderTraversalII.py) 148 | 126. [Word Ladder II](BreadthFirstSearch/126_WordLadderII.py) 149 | 127. [Word Ladder](BreadthFirstSearch/127_WordLadder.py) 150 | 130. [Surrounded Regions](BreadthFirstSearch/130_SurroundedRegions.py) 151 | 199. [Binary Tree Right Side View](BreadthFirstSearch/199_BinaryTreeRightSideView.py) 152 | 310. [Minimum Height Trees](BreadthFirstSearch/310_MinimumHeightTrees.py) 153 | 322. [Coin Change](BreadthFirstSearch/322_CoinChange.py) 154 | 155 | # [Backtracking](Backtracking/) 156 | 157 | 022. [Generate Parentheses](Backtracking/23_GenerateParentheses.py) 158 | 039. [Combination Sum](Backtracking/39_CombinationSum.py) 159 | 046. [Permutations](Backtracking/46_Permutations.py) 160 | 047. [Permutations II](Backtracking/47_PermutationsII.py) 161 | 051. [N-Queens](Backtracking/51_NQueens.py) 162 | 052. [N-Queens II](Backtracking/52_NQueensII.py) 163 | 079. [Word Search](Backtracking/79_WordSearch.py) 164 | 090. [Subsets II](Backtracking/90_SubsetsII.py) 165 | 093. [Restore IP Addresses](Backtracking/93_RestoreIPAddresses.py) 166 | 131. [Palindrome Partitioning](Backtracking/131_PalindromePartitioning.py) 167 | 216. [Combination Sum III](Backtracking/216_CombinationSumIII.py) 168 | 169 | # [Recursion](Recursion/) 170 | 171 | 060. Permutation Sequence 172 | 077. [Combinations](Recursion/77_Combinations.py) 173 | 089. [Gray Code](Recursion/89_GrayCode.py) 174 | 101. [Symmetric Tree](Recursion/101_SymmetricTree.py) 175 | 176 | 177 | # [Dynamic Programming](DynamicProgramming/) 178 | 179 | 010. [Regular Expression Matching](DynamicProgramming/10_RegularExpressionMatching.py) 180 | 032. [Longest Valid Parentheses](DynamicProgramming/32_LongestValidParentheses.py) 181 | 044. [Wildcard Matching](DynamicProgramming/44_WildcardMatching.py) 182 | 053. [Maximum Subarray](DynamicProgramming/53_MaximumSubarray.py) 183 | 062. [Unique Paths](DynamicProgramming/62_UniquePaths.py) 184 | 063. [Unique Paths II](DynamicProgramming/63_UniquePathsII.py) 185 | 064. [Minimum Path Sum](DynamicProgramming/64_MinimumPathSum.cpp) 186 | 070. [Climbing Stairs](DynamicProgramming/70_ClimbingStairs.py) 187 | 072. [Edit Distance](DynamicProgramming/72_EditDistance.py) 188 | 087. [Scramble String](DynamicProgramming/87_ScrambleString.py) 189 | 091. [Decode Ways](DynamicProgramming/91_DecodeWays.py) 190 | 097. [Interleaving String](DynamicProgramming/97_InterleavingString.py) 191 | 115. [Distinct Subsequences](DynamicProgramming/115_DistinctSubsequences.py) 192 | 120. [Triangle](DynamicProgramming/120_Triangle.py) 193 | 121. [Best Time to Buy and Sell Stock](DynamicProgramming/121_BestTimeToBuyAndSellStock.py) 194 | 123. [Best Time to Buy and Sell Stock III](DynamicProgramming/123_BestTimeToBuyAndSellStockIII.py) 195 | 132. [Palindrome Partitioning II](DynamicProgramming/132_PalindromePartitioningII.py) 196 | 135. [Candy](DynamicProgramming/135_Candy.py) 197 | 139. [Word Break](DynamicProgramming/139_WordBreak.py) 198 | 152. [Maximum Product Subarray](working/152_MaximumProductSubarray.py) 199 | 174. [Dungeon Game](DynamicProgramming/174_DungeonGame.py) 200 | 188. [Best Time to Buy and Sell Stock IV](DynamicProgramming/188_BestTimeBuySellStockIV.py) 201 | 198. [House Robber](DynamicProgramming/198_HouseRobber.py) 202 | 213. [House Robber II](DynamicProgramming/213_HouseRobberII.py) 203 | 221. [Maximal Square](DynamicProgramming/221_MaximalSquare.py) 204 | 264. [Ugly Number II](DynamicProgramming/264_UglyNumberII.py) 205 | 279. [Perfect Squares](DynamicProgramming/279_PerfectSquares.py) 206 | 303. [Range Sum Query - Immutable](DynamicProgramming/303_RangeSumQueryImmutable.py) 207 | 304. [Range Sum Query 2D - Immutable](DynamicProgramming/304_RangeSumQuery2DImmutable.py) 208 | 309. [Best Time to Buy and Sell Stock with Cooldown](DynamicProgramming/309_BestTimeBuySellStockWithCooldown.py) 209 | 313. [Super Ugly Number](DynamicProgramming/313_SuperUglyNumber.py) 210 | 337. [House Robber III](DynamicProgramming/337_HouseRobberIII.py) 211 | 368. [Largest Divisible Subset](DynamicProgramming/368_LargestDivisibleSubset.py) 212 | 357. [Count Numbers with Unique Digits](DynamicProgramming/357_CountNumbersWithUniqueDigits.py) 213 | 214 | # [Greedy](Greedy/) 215 | 216 | 045. [Jump Game II](Greedy/45_JumpGameII.py) 217 | 055. [Jump Game](Greedy/55_JumpGame.py) 218 | 122. [Best Time to Buy and Sell Stock II](Greedy/122_BestTimeToBuyAndSellStockII.py) 219 | 134. [Gas Station](Greedy/134_GasStation.py) 220 | 316. [Remove Duplicate Letters](Greedy/316_RemoveDuplicateLetters.py) 221 | 330. [Patching Array](Greedy/330_PatchingArray.py) 222 | 223 | # [Stack](Stack/) 224 | 225 | 020. [Valid Parentheses](Stack/20_ValidParentheses.py) 226 | 032. [Longest Valid Parentheses](Stack/32_LongestValidParentheses.py) 227 | 071. [Simplify Path](Stack/71_SimplifyPath.py) 228 | 084. [Largest Rectangle in Histogram](Stack/84_LargestRectangleInHistogram.py) 229 | 085. [Maximal Rectangle](Stack/85_MaximalRectangle.py) 230 | 094. [Binary Tree Inorder Traversal](Stack/94_BinaryTreeInorderTraversal.py) 231 | 150. [Evaluate Reverse Polish Notation](Stack/150_EvaluateReversePolishNotation.py) 232 | 155. [Min Stack](Stack/155_MinStack.py) 233 | 224. [Basic Calculator](Stack/224_BasicCalculator.py) 234 | 225. [Implement Stack using Queues](Stack/225_ImplementStackusingQueues.py) 235 | 227. [Basic Calculator II](Stack/227_BasicCalculatorII.py) 236 | 232. [Implement Queue using Stacks](Stack/232_ImplementQueueUsingStacks.py) 237 | 316. [Remove Duplicate Letters](Stack/316_RemoveDuplicateLetters.py) 238 | 341. [Flatten Nested List Iterator](Stack/341_FlattenNestedListIterator.py) 239 | 240 | # [Two Pointers](TwoPointers/) 241 | 242 | 011. [Container With Most Water](TwoPointers/11_ContainerWithMostWater.py) 243 | 015. [3Sum](TwoPointers/15_3Sum.py) 244 | 016. [3Sum Closest](TwoPointers/16_3SumClosest.py) 245 | 018. [4Sum](TwoPointers/18_4Sum.py) 246 | 019. [Remove Nth Node From End of List](TwoPointers/19_RemoveNthNodeFromEndOfList.py) 247 | 042. [Trapping Rain Water](TwoPointers/42_TrappingRainWater.py) 248 | 075. [Sort Colors](TwoPointers/75_SortColors.py) 249 | 080. [Remove Duplicates from Sorted Array II](TwoPointers/80_RemoveDuplicatesArrayII.py) 250 | 088. [Merge Sorted Array](TwoPointers/88_MergeSortedArray.py) 251 | 125. [Valid Palindrome](TwoPointers/125_ValidPalindrome.py) 252 | 141. [Linked List Cycle](TwoPointers/141_LinkedListCycle.py) 253 | 142. [Linked List Cycle II](TwoPointers/142_LinkedListCycleII.py) 254 | 209. [Minimum Size Subarray Sum](TwoPointers/209_MinimumSizeSubarraySum.py) 255 | 283. [Move Zeroes](TwoPointers/283_MoveZeroes.py) 256 | 287. [Find the Duplicate Number](TwoPointers/287_FindTheDuplicateNumber.py) 257 | 345. [Reverse Vowels of a String](TwoPointers/345_ReverseVowelsOfString.py) 258 | 259 | 260 | # [Math](Math/) 261 | 262 | 007. [Reverse Integer](Math/07_ReverseInteger.py) 263 | 009. [Palindrome Number](Math/09_PalindromeNumber.py) 264 | 012. [Integer to Roman](Math/12_IntegertoRoman.py) 265 | 013. [Roman to Integer](Math/13_RomantoInteger.py) 266 | 048. [Rotate Image](Math/48_RotateImage.py) 267 | 043. [Multiply Strings](Math/43_MultiplyStrings.py) 268 | 050. [Pow(x, n)](Math/50_Pow.py) 269 | 060. [Permutation Sequence](Math/60_PermutationSequence.py) 270 | 066. [Plus One](Math/66_PlusOne.py) 271 | 069. Sqrt(x) 272 | 166. [Fraction to Recurring Decimal](Math/166_FractionToRecurringDecimal.py) 273 | 168. [Excel Sheet Column Title](Math/168_ExcelSheetColumnTitle.py) 274 | 171. [Excel Sheet Column Number](Math/171_ExcelSheetColumnNumber.py) 275 | 172. [Factorial Trailing Zeroes](Math/172_FactorialTrailingZeroes.py) 276 | 179. [Largest Number](Math/179_LargestNumber.py) 277 | 202. [Happy Number](Math/202_HappyNumber.py) 278 | 204. [Count Primes](Math/204_CountPrimes.py) 279 | 223. [Rectangle Area](Math/223_RectangleArea.py) 280 | 233. [Number of Digit One](Math/233_NumberOfDigitOne.py) 281 | 238. [Product of Array Except Self](Math/238_ProductOfArrayExceptSelf.py) 282 | 258. [Add Digits](Math/258_AddDigits.py) 283 | 263. [Ugly Number](Math/263_UglyNumber.py) 284 | 273. [Integer to English Words](Math/273_IntegerToEnglishWords.py) 285 | 292. [Nim Game](Math/292_NimGame.py) 286 | 326. [Power of Three](Math/326_PowerOfThree.py) 287 | 319. [Bulb Switcher](Math/319_BulbSwitcher.py) 288 | 335. [Self Crossing](Math/335_SelfCrossing.py) 289 | 343. [Integer Break](Math/343_IntegerBreak.py) 290 | 291 | # [Bit Manipulation](BitManipulation/) 292 | 293 | 029. [Divide Two Integers](BitManipulation/29_DivideTwoIntegers.py) 294 | 078. [Subsets](BitManipulation/78_Subsets.py) 295 | 136. [Single Number](BitManipulation/136_SingleNumber.py) 296 | 137. [Single Number II](BitManipulation/137_SingleNumberII.py) 297 | 169. [Majority Element](BitManipulation/169_MajorityElement.py) 298 | 190. [Reverse Bits](BitManipulation/190_ReverseBits.py) 299 | 191. [Number of 1 Bits](BitManipulation/191_NumberOf1Bits.py) 300 | 201. [Bitwise AND of Numbers Range](BitManipulation/201_BitwiseANDofNumbersRange.py) 301 | 231. [Power of Two](BitManipulation/231_PowerOfTwo.py) 302 | 260. [Single Number III](BitManipulation/260_SingleNumberIII.py) 303 | 268. [Missing Number](BitManipulation/268_MissingNumber.py) 304 | 318. [Maximum Product of Word Lengths](BitManipulation/318_MaximumProductOfWordLengths.py) 305 | 338. [Counting Bits](BitManipulation/338_CountingBits.py) 306 | 371. [Sum of Two Integers](BitManipulation/371_SumOfTwoIntegers.py) 307 | 342. [Power of Four](BitManipulation/342_PowerOfFour.py) 308 | 309 | # [Graph](Graph/) 310 | 311 | 133. [Clone Graph](Graph/133_CloneGraph.py) 312 | 207. [Course Schedule](Graph/207_CourseSchedule.py) 313 | 210. [Course Schedule II](Graph/210_CourseScheduleII.py) 314 | 315 | # Combination 316 | 317 | 030. [Substring with Concatenation of All Words](Combination/30_SubstringWithConcatenationOfAllWords.py) 318 | 037. [Sudoku Solver](Combination/37_SudokuSolver.py) 319 | 140. [Word Break II](Combination/140_WordBreakII.py) 320 | 146. [LRU Cache](Combination/146_LRUCache.py) 321 | 300. [Longest Increasing Subsequence](Combination/300_LongestIncreasingSubsequence.py) 322 | 324. [Wiggle Sort II](Combination/324_WiggleSortII.py) 323 | 329. [Longest Increasing Path in a Matrix](Combination/329_LongestIncreasingPathInMatrix.py) 324 | 355. [Design Twitter](Combination/355_DesignTwitter.py) 325 | 326 | # DFA 327 | 328 | 065. [Valid Number](DFA/65_ValidNumber.py) 329 | 330 | # [Divide and Conquer](DivideConquer/) 331 | 332 | 215. [Kth Largest Element in an Array](DivideConquer/215_KthLargestElementArray.py) 333 | 240. [Search a 2D Matrix II](DivideConquer/240_Search2DMatrixII.py) 334 | 241. [Different Ways to Add Parentheses](DivideConquer/241_DifferentWaysToAddParentheses.py) 335 | 336 | # Others 337 | 338 | 220. [Contains Duplicate III](220_ContainsDuplicateIII.py) 339 | 229. [Majority Element II](Others/229_MajorityElementII.py) 340 | 239. [Sliding Window Maximum](Others/239_SlidingWindowMaximum.py) 341 | 284. [Peeking Iterator](Others/284_PeekingIterator.py) 342 | 307. [Range Sum Query - Mutable](Others/307_RangeSumQueryMutable.py) 343 | --------------------------------------------------------------------------------