├── .clang-format ├── 00001. Two Sum.cpp ├── 00002. Add Two Numbers.cpp ├── 00003. Longset Substring Without Repeating Characters.cpp ├── 00004. Median of Two Sorted Arrays.cpp ├── 00005. Longest Palindromic Substring.cpp ├── 00006. ZigZag Conversion.cpp ├── 00007. Reverse Integer.cpp ├── 00008. String to Integer(atoi).cpp ├── 00009. Palindrome Number.cpp ├── 00010. Regular Expression Matching.cpp ├── 00011. Container With Most Water.java ├── 00012. Integer to Roman.cpp ├── 00013. Roman to Integer.cpp ├── 00014. Longest Common Prefix.cpp ├── 00015. 3Sum.cpp ├── 00016. 3Sum Closest.cpp ├── 00017. Letter Combinations of a Phone Number.cpp ├── 00018. 4Sum.cpp ├── 00019. Remove Nth Node From End of List.cpp ├── 00020. Vaild Parentrheses.cpp ├── 00021. Merge Two Sorted Lists.cpp ├── 00022. Generate Parentheses.cpp ├── 00023. Merge k Sorted Lists.java ├── 00024. Swap Nodes in Pairs.java ├── 00025. Reverse Nodes in k-Group.cpp ├── 00026. Remove Duplicates from Sorted Array.c ├── 00027. Remove Element.c ├── 00028. Implement strStr().c ├── 00029. Divide Two Integers.c ├── 00030. Substring with Concatenation of All Words.cpp ├── 00031. Next Permutation.cpp ├── 00032. Longest Valid Parentheses.cpp ├── 00033. Search in Rotated Sorted Array.c ├── 00034. Search for a Range.cpp ├── 00035. Search Insert Position.cpp ├── 00036. Valid Sudoku.cpp ├── 00037. Sudoku Solver.cpp ├── 00038. Count and Say.c ├── 00039. Combination Sum.cpp ├── 00040. Combination Sum II.cpp ├── 00041. First Missing Positive.c ├── 00042. Trapping Rain Water.cpp ├── 00043. Multiply Strings.cpp ├── 00044. Wildcrad Matching.c ├── 00045. Jump Game II.cpp ├── 00046. Permutations.cpp ├── 00047. Permutations II.cpp ├── 00048. Rotate Image.cpp ├── 00049. Group Anagrams.cpp ├── 00050. Pow(x,n).cpp ├── 00051. N-Queens.cpp ├── 00052. N-Queens II.cpp ├── 00053. Maximum Subarray.c ├── 00054. Spiral Matric.cpp ├── 00055. Jump Game.java ├── 00056. Merge Intervals.cpp ├── 00057. Insert Interval.cpp ├── 00058. Length of Last Word.c ├── 00059. Spiral Matrix II.java ├── 00060. Permutation Sequence.cpp ├── 00061. Rotate List.cpp ├── 00062. Unique Paths.cpp ├── 00063. Unique Paths II.cpp ├── 00064. Minimum Path Sum.java ├── 00065. Vaild Number.c ├── 00066. Plus One.cpp ├── 00067. Add Binary.java ├── 00068. Text Justification.cpp ├── 00069. Sqrt(x).c ├── 00070. Climbing Stairs.java ├── 00071. Simplify Path.cpp ├── 00072. Edit Distance.cpp ├── 00073. Set Matrix Zeroes.cpp ├── 00074. Search a 2D Matrix.c ├── 00075. Sort Colors.cpp ├── 00076. Minimum Window Substring.cpp ├── 00077. Combinations.cpp ├── 00078. Subsets.cpp ├── 00079. Word Search.cpp ├── 00080. Remove Duplicates from Sorted Array II.cpp ├── 00081. Search in Rotated Sorted Array II.c ├── 00082. Remove Duplicates from Sorted List II.c ├── 00083. Remove Duplicates from Sorted List.cpp ├── 00084. Largest Rectangle in Histogram.cpp ├── 00085. Maximal Rectangle.cpp ├── 00086. Partition List.cpp ├── 00088. Merge Sorted Array.cpp ├── 00089. Gray code.cpp ├── 00090. Subsets II.cpp ├── 00091. Decode Ways.cpp ├── 00092. Reverse Linked List II.cpp ├── 00093. Restore IP Addresses.cpp ├── 00094. Binary Tree Inorder Traversal.java ├── 00096. Unique Binary Search Trees.cpp ├── 00097. Interleaving String.cpp ├── 00098. Validate Binary Search Tree .cpp ├── 00100. Same Tree.java ├── 00101. Symmetric Tree.c ├── 00102. Binary Tree Level Order Traversal.java ├── 00103. Binary Tree Zigzag Level Order Traversal.java ├── 00104. Maximum Depth of Binary Tree.cpp ├── 00105. Construct Binary Tree from Preorder nad inorder Traversal.cpp ├── 00106. Construct Binary Tree from Inorder and Postorder Traversal.cpp ├── 00107. Binary Tree Level Order Traversal II.java ├── 00108. Convert Sorted Array to Binary Search Tree.cpp ├── 00109. Convert Sorted List to Binary Search Tree.cpp ├── 00110. Balanced Binary Tree.cpp ├── 00111. Minimum Depth of Binary Tree.c ├── 00112. Path Sum.cpp ├── 00113. Path Sum II.cpp ├── 00114. Flatten Binary Tree to Linked List.cpp ├── 00116. Populating Next Right Pointers in Each Node.cpp ├── 00117. Populating Next Right Pointers in Each Node II.cpp ├── 00118. Pascal's Triangle.cpp ├── 00119. Pascal's Triangle II.cpp ├── 00120. Triangle.cpp ├── 00121. Best Time to Buy and Sell Stock.cpp ├── 00122. Best Time to Buy and Sell Stock II.java ├── 00123. Best Time to Buy and Sell Stock III.cpp ├── 00124. Binary Tree maximym Path Sum.c ├── 00125. Valid Palindrome.cpp ├── 00126. Word Ladder II.java ├── 00127. Word Ladder.java ├── 001277. Count Square Submatrices with All Ones.cpp ├── 00128. Longest Consecutive Sequence.cpp ├── 00129. Sum Root to Leaf Numbers.cpp ├── 00130. Surrounded Regions.cpp ├── 00131. Palindrome Partitioning.cpp ├── 00133. Clone Graph.cpp ├── 00134. Gas Station.c ├── 00135. Candy.java ├── 00136. Single Number.cpp ├── 00138. Copy List with Random Pointer.cpp ├── 00139. Word Break.cpp ├── 00140. Word Break II.cpp ├── 00141. Linked List Cycle.cpp ├── 00142. Linked List Cycle II.cpp ├── 00143. Reorder List.cpp ├── 00144. Binary Tree Preorder Traversal.cpp ├── 00145. Binary Tree Postorder Traversal.cpp ├── 00146. LRU Cache.cpp ├── 00147. Insertion Sort List.cpp ├── 00148. Sort List.cpp ├── 00149. Max Points on a Line.cpp ├── 00150. Evaluate Reverse Polish Notation.cpp ├── 00151. Reverse Words in a String.cpp ├── 00152. Maximum Product Subarray.cpp ├── 00153. Find Minimum in Rotated Sorted Array.cpp ├── 00154. Find Minimum in Rotated Sorted Array II.cpp ├── 00155. Min Stack.cpp ├── 00160. Intersection of Two Linked Lists.cpp ├── 00162. Find Peak Element.cpp ├── 00164. Maximum Gap.cpp ├── 00165. Compare Version Numbers.swift ├── 00166. Fraction to Recurring Decimal.cpp ├── 00167. Two Sum II.swift ├── 00168. Excel Sheet Column Title.cpp ├── 00169. Majority Element.cpp ├── 00171. Excel Sheet Column Number.cpp ├── 00172. Factorial Trailing Zeroes.cpp ├── 00174. Dungeon Game.cpp ├── 00179. Largest Number.cpp ├── 00187. Repeated DNA Sequences.cpp ├── 00189. Rotate Array.c ├── 00190. Reverse Bits.cpp ├── 00191. Number of 1 Bits.cpp ├── 00198. House Robber.cpp ├── 00199. Binary Tree Right Side View.cpp ├── 00200. Number of Islands.cpp ├── 00202. Happy Number.cpp ├── 00204. Count Primes.cpp ├── 00205. Isomorphic Strings.cpp ├── 00206. Reverse Linked List.cpp ├── 00207. Course Schedule.cpp ├── 00208. Implement Trie(Prefix Tree).cpp ├── 00209. Minimum Size Subarray Sum.cpp ├── 00210. Course Schedule II.cpp ├── 00211. Add and Search Word - Data structure design.cpp ├── 00212. Word Search II.cpp ├── 00213. House Robber II.cpp ├── 00215. Kth Largest Element in an Array.cpp ├── 00216. Combination Sum III.cpp ├── 00217. Contains Duplicate.cpp ├── 00219. Contains Duplicate II.cpp ├── 00221. Maximal Square.cpp ├── 00222. Count Complete Tree Nodes.cpp ├── 00223. Rectangle Area.cpp ├── 00224. Basic Calculator.cpp ├── 00225. Implement Stack using Queues.cpp ├── 00226. Invert Binary Tree.cpp ├── 00227. Basic Calculator II.cpp ├── 00228. Summary Ranges.cpp ├── 00230. Kth Smallest Element in a BST.cpp ├── 00231. Power of Two.cpp ├── 00232. Implement Queue using Stacks.cpp ├── 00234. Palindrome Linked List.cpp ├── 00235. Lowest Common Ancestor of a Binary Search Tree.cpp ├── 00236. Lowest Common Ancestor of a Binary Tree.cpp ├── 00237. Delete Node in a Linked List.cpp ├── 00238. Product of Array Except Self.cpp ├── 00239. Sliding Window Maximum.cpp ├── 00240. Search a 2D Matrix II.cpp ├── 00242. Valid Anagram.cpp ├── 00257. Binary Tree Paths.cpp ├── 00258. Add Digits.cpp ├── 00260. Single Number III.swift ├── 00263. Ugly Number.cpp ├── 00264. Ugly Number II.cpp ├── 00268. Missing Number.cpp ├── 00274. H-Index.cpp ├── 00275. H-Index II.cpp ├── 00278. First Bad Version.c ├── 00279. Perfect Squares.cpp ├── 00283. Move Zeroes.cpp ├── 00287. Find the Duplicate Number.cpp ├── 00289. Game of Life.cpp ├── 00290. Word Pattern.cpp ├── 00292. Nim Game.cpp ├── 00295. Find Median from Data Stream.cpp ├── 00299. Bulls and Cows.cpp ├── 00300. Longest Increasing Subsequence.cpp ├── 00303. Range Sum Query - Immutable.cpp ├── 00304. Range Sum Query 2D - Immutable.cpp ├── 00307. Range Sum Query - Mutable.cpp ├── 00310. Minimum Height Trees.cpp ├── 00313. Super Ugly Number.cpp ├── 00315. Count of Smaller Numbers After Self.cpp ├── 00316. Remove Duplicate Letters.cpp ├── 00322. Coin Change.cpp ├── 00324. Wiggle Sort II.cpp ├── 00326. Power of Three.cpp ├── 00328. Odd Even Linked List.cpp ├── 00329. Longest Increasing Path in a Matrix.cpp ├── 00332. Reconstruct Itinerart.cpp ├── 00334. Increasing Triplet Subsequence.cpp ├── 00338. Counting Bits.cpp ├── 00341. Flatten Nested List Iterator.cpp ├── 00342. Power of Four.cpp ├── 00343. Integer Break.cpp ├── 00344. Reverse String.cpp ├── 00345. Reverse Vowels of a String.cpp ├── 00347. Top k Frequent Elements.cpp ├── 00349. Intersection of Two Arrays.cpp ├── 00350. Intersection of Two Arrays II.cpp ├── 00352. Data Stream as Disjoint Intervals.cpp ├── 00354. Russian Doll Envelopes.cpp ├── 00355. Design Twitter.cpp ├── 00367. Valid Perfect Square.cpp ├── 00371. Sum of Two Integers.cpp ├── 00373. Find K Pairs with Smallest Sums.cpp ├── 00374. Guess Number Higher or Lower.cpp ├── 00378. Kth Smallest Element in a Sorted Matrix.cpp ├── 00380. Insert Delete GetRandom O(1).cpp ├── 00381. Insert Delete GetRandom O(1) - Duplicates allowed.cpp ├── 00383. Ransom Note.cpp ├── 00384. Shuffle an Array.cpp ├── 00387. First Unique Character in a String.js ├── 00389. Find the Difference.cpp ├── 00393. UTF-8 Validation.cpp ├── 00395. Longest Substring with At Least K Repeating Characters.cpp ├── 00398. Random Pick Index.cpp ├── 00399. Evaluate Division.cpp ├── 00401. Binary Watch.cpp ├── 00402. Remove K Digits.cpp ├── 00404. Sum of Left Leaves.cpp ├── 00406. Queue Reconstruction by Height.cpp ├── 00409. Longest Palindrome.cpp ├── 00410. Split Array Largest Sum.cpp ├── 00412. Fizz Buzz.cpp ├── 00413. Arithmetic Slices.cpp ├── 00414. Third Maximum Number.cpp ├── 00416. Partition Equal Subset Sum.cpp ├── 00419. Battleships in a Board.cpp ├── 00423. Reconstruct Original Digits from English.cpp ├── 00437. Path Sum III.cpp ├── 00438. Find All Anagrams in a String.cpp ├── 00442. Find All Duplicates in an Array.cpp ├── 00447. Number of Boomerangs.cpp ├── 00448. Find All Numbers Disappeared in an Array.cpp ├── 00449. Serialize and Deserialize BST.cpp ├── 00451. Sort Characters By Frequency.cpp ├── 00453. Minimum Moves to Equal Array Elements.cpp ├── 00454. 4Sum II.cpp ├── 00455. Assign Cookies.cpp ├── 00456. 132 Pattern.cpp ├── 00459. Repeated Substring Pattern.cpp ├── 00460. LFU Cache.cpp ├── 00461. Hamming Distance.cpp ├── 00462. Minimum Moves to Equal Array Elements II.cpp ├── 00463. Island Perimeter.cpp ├── 00468. Validate IP Address.swift ├── 00473. Matchsticks to Square.cpp ├── 00474. Ones and Zeros.cpp ├── 00476. Number Complement.cpp ├── 00477. Total Hamming Distance.cpp ├── 00478. Generate Random Point in a Circle.cpp ├── 00480. Sliding Window Median.cpp ├── 00485. Max Consecutive Ones.cpp ├── 00488. Zuma Game.cpp ├── 00492. Construct the Rectangle.cpp ├── 00495. Teemo Attacking.cpp ├── 00496. Next Greater Element I.cpp ├── 00500. Keyboard Row.cpp ├── 00503. Next Greater Element II.cpp ├── 00504. Base 7.cpp ├── 00506. Relative Ranks.cpp ├── 00508. Most Frequent Subtree Sum.cpp ├── 00513. Find Bottom Left Tree Value.cpp ├── 00515. Find Largest Value in Each Tree Row.cpp ├── 00520. Detect Capital.cpp ├── 00521. Longest Uncommon Subsequence I.c ├── 00522. Longest Uncommon Subsequence II.cpp ├── 00524. Longest Word in Dictionary through Deleting.cpp ├── 00526. Beautiful Arrangement.cpp ├── 00529. Minesweeper.cpp ├── 00530. Minimum Absolute Difference in BST.cpp ├── 00532. K-diff Pairs in an Array.cpp ├── 00535. Encode and Decode TinyURL.cpp ├── 00537. Complex Number Multiplication.cpp ├── 00540. Single Element in a Sorted Array.cpp ├── 00543. Diameter of Binary Tree.cpp ├── 00551. Student Attendance Record I.cpp ├── 00554. Brick Wall.cpp ├── 00557. Reverse Words in a String III.cpp ├── 00560. Subarray Sum Equals K.cpp ├── 00561. Array Partition I.cpp ├── 00565. Array Nesting.cpp ├── 00566. Reshape the Matrix.cpp ├── 00572. SubTree of Another Tree.cpp ├── 00575. Distribute Candies.cpp ├── 00581. Shortest Unsorted Contnuous Subarray.cpp ├── 00582. Kill Process.cpp ├── 00583. Delete Operation for Two Strings.cpp ├── 00587. Erect the Fence.cpp ├── 00588. Design In-Memory File System.cpp ├── 00592. Fraction Addition and Subtraction.cpp ├── 00593. Valid Square.cpp ├── 00594. Longest Harmonious Subsequence.cpp ├── 00599. Minimum Index Sum of Two Lists.cpp ├── 00611. Valid Triangle Number.cpp ├── 00617. Merge Two Binary Trees.cpp ├── 00621. Task Scheduler.cpp ├── 00630. Course Schedule III.cpp ├── 00637. Average of Levels in Binary Tree.cpp ├── 00638. Shopping Offers.cpp ├── 00648. Replace Words.cpp ├── 00650. 2 Keys Keyboard.cpp ├── 00653. Two Sum IV - Input is a BST.cpp ├── 00657. Judge Route Circle.cpp ├── 00659. Split Array into Consecutive Subsequences.cpp ├── 00670. Maximum Swap.cpp ├── 00673. Number of Longest Increasing Subsequence.cpp ├── 00676. Implement Magic Dictionary.cpp ├── 00677. Map Sum Pairs.cpp ├── 00691. Top K Frequent Words.cpp ├── 00698. Partition to K Equal Sum Subsets.cpp ├── 00710. Random Pick with Blacklist.cpp ├── 00738. Monotone Increasing Digits.cpp ├── 00740. Delete and Earn.cpp ├── 00745. Prefix and Suffix Search.cpp ├── 00756. Pyramid Transition Matrix.cpp ├── 00763. Partition Labels.cpp ├── 00764. Largest Plus Sign.cpp ├── 00766. Toeplitz Matrix.cpp ├── 00767. Reorganize String.cpp ├── 00768. Max Chunks To Make Sorted II.cpp ├── 00769. Max Chunks To Make Sorted.cpp ├── 00783. Minimum Distance Between BST Nodes.cpp ├── 00785. Is Graph Bipartite.cpp ├── 00792. Number of Matching Subsequences.cpp ├── 00796. Rotate String.cpp ├── 00797. All Paths From Source to Target.cpp ├── 00799. Champagne Tower.cpp ├── 00820. Short Encoding of Words.cpp ├── 00827. Making A Large Island.cpp ├── 00829. Consecutive Numbers Sum.cpp ├── 00840. Magic Squares In Grid.cpp ├── 00841. Keys and Rooms.cpp ├── 00842. Split Array into Fibonacci Sequence.cpp ├── 00843. Guess the Word.cpp ├── 00844. Backspace String Compare.cpp ├── 00845. Longest Mountain in Array.cpp ├── 00846. Hand of Straights.cpp ├── 00847. Shortest Path Visiting All Nodes.cpp ├── 00848. Shifting Letters.cpp ├── 00849. Maximize Distance to Closest Person.cpp ├── 00855. Exam Room.cpp ├── 00864. Shortest Path to Get All Keys.cpp ├── 00866. Prime Palindrome.cpp ├── 00867. Transpose Matrix.cpp ├── 00869. Reordered Power of 2.cpp ├── 00870. Advantage Shuffle.cpp ├── 00871. Minimum Number of Refueling Stops.cpp ├── 00873. Length of Longest Fibonacci Subsequenc.cpp ├── 00875. Koko Eating Bananas.cpp ├── 00876. Middle of the Linked List.cpp ├── 00877. Stone Game.cpp ├── 00881. Boats to Save People.cpp ├── 00881. Boats to Save People.py ├── 00883. Projection Area of 3D Shapes.cpp ├── 00884. Uncommon Words from Two Sentences.py ├── 00886. Possible Bipartition.py ├── 00888. Fair Candy Swap.py ├── 00890. Find and Replace Pattern.cpp ├── 00890. Find and Replace Pattern.py ├── 00892. Surface Area of 3D Shapes.cpp ├── 00893. Groups of Special-Equivalent Strings.cpp ├── 00894. All Possible Full Binary Trees.cpp ├── 00895. Maximum Frequency Stack.cpp ├── 00907. Sum of Subarray Minimums.cpp ├── 00911. Online Election.cpp ├── 00915. Partition Array into Disjoint Intervals.cpp ├── 00916. Word Subsets.cpp ├── 00918. Maximum Sum Circular Subarray.cpp ├── 00919. Complete Binary Tree Inserter.cpp ├── 00922. Sort Array By Parity II.cpp ├── 00923. 3Sum With Multiplicity.cpp ├── 00924. Minimize Malware Spread.cpp ├── 00926. Flip String to Monotone Increasing.cpp ├── 00928. Minimize Malware Spread II.cpp ├── 00930. Binary Subarrays With Sum.cpp ├── 00931. Minimum Falling Path Sum.cpp ├── 00935. Knight Dialer.cpp ├── 00937. Reorder Log Files.py ├── 00938. Range Sum of BST.py ├── 00939. Minimum Area Rectangle.cpp ├── 00959. Regions Cut By Slashes.cpp ├── 00963. Minimum Area Rectangle II.cpp ├── 00969. Pancake Sorting.cpp ├── 00973. K Closest Points to Origin.cpp ├── 00974. Subarray Sums Divisible by K.cpp ├── 00975. Odd Even Jump.cpp ├── 00976. Largest Perimeter Triangle.cpp ├── 00987. Vertical Order Traversal of a Binary Tree.swift ├── 01014. Capacity To Ship Packages Within D Days.cpp ├── 01024. Video Stiching.cpp ├── 01044. Longest Duplicate Substring.cpp ├── 01074. Number Of Submatrices That Sum to Target.cpp ├── 01283. Find the Smallest Divisor Given a Threshold.cpp ├── 01284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix.cpp ├── 01293. Shortest Path in a Grid with Obstacles Elimiantion.cpp ├── 01307. Verbal Arithmetic Puzzle.cpp ├── 01320. Minimum Distance to Type a Word Using Two Fingers.cpp ├── 01340. Minimum Difficulty of a Job Schedule.cpp ├── 01349. Maximum Students Taking Exam.cpp ├── 01363. Largest Multiple of Three.cpp ├── 01377. Frog Position After T Seconds.cpp ├── 01477. Find Two Non-overlapping Sub-arrays Each With Target Sum.cpp ├── Dynamic Programming ├── DP.md └── Dynamic Programming Patterns.md ├── POJ └── POJ#1941.cpp ├── README.md ├── String Search └── Leetcode 1044.md ├── makefile └── update.sh /00001. Two Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector &numbers, int target) { 4 | vector ans; 5 | deal(numbers); 6 | sort(V.begin(),V.end(),cmp); 7 | // for(int i=0;i<(int)V.size();i++) 8 | // { 9 | // cout< target) e--; 17 | else if(sum < target) s++; 18 | else 19 | { 20 | s = V[s].first; 21 | e = V[e].first; 22 | break; 23 | } 24 | } 25 | ans.push_back(min(s,e)); 26 | ans.push_back(max(s,e)); 27 | return ans; 28 | } 29 | void deal(vector &numbers) 30 | { 31 | V.clear(); 32 | for(int i=0;i<(int)numbers.size();i++) 33 | V.push_back(make_pair(i+1,numbers[i])); 34 | } 35 | static bool cmp(const pair& fir,const pair& sec) 36 | { 37 | return fir.second < sec.second; 38 | } 39 | private: 40 | vector > V; 41 | }; 42 | 43 | -------------------------------------------------------------------------------- /00003. Longset Substring Without Repeating Characters.cpp: -------------------------------------------------------------------------------- 1 | int vis[1024]; 2 | int lengthOfLongestSubstring(char *s) { 3 | int len = (int)strlen(s); 4 | if(len <= 1) return len; 5 | int ans = 1,i = 0,j=1; 6 | memset(vis, 0, sizeof(vis)); 7 | vis[s[i]] = 1; 8 | while (j ans) ans = j-i+1; 11 | vis[s[j]] = 1; 12 | j++; 13 | } 14 | else{ 15 | vis[s[i]] = 0; 16 | i++; 17 | } 18 | } 19 | return ans; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /00004. Median of Two Sorted Arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double findMedianSortedArrays(int A[], int m, int B[], int n) { 4 | int tot = m + n; 5 | if(tot & 0x1) 6 | return findKthNum(A, m, B, n, tot/2+1); 7 | else 8 | return (findKthNum(A, m, B, n, tot/2) + findKthNum(A, m, B, n, tot/2 + 1))*1.0 / 2; 9 | } 10 | 11 | int findKthNum(int a[],int m, int b[], int n, int k){ 12 | if(m > n) return findKthNum(b, n, a, m, k); 13 | if(m == 0) return b[k-1]; 14 | if(k == 1) return min(a[0],b[0]); 15 | 16 | int pa = min(k/2,m), pb = k - pa; 17 | if(a[pa - 1] < b[pb-1]){ 18 | return findKthNum(a+pa, m-pa, b, n, k-pa); 19 | } 20 | else if(a[pa - 1] > b[pb-1]){ 21 | return findKthNum(a, m, b+pb, n-pb, k-pb); 22 | } 23 | return a[pa-1]; 24 | } 25 | }; 26 | 27 | -------------------------------------------------------------------------------- /00005. Longest Palindromic Substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestPalindrome(string s) { 4 | if(s.length() < 2) return s; 5 | int anslen = 0,ansidx = 0; 6 | for(int i=0;i<(int)s.size();){ 7 | int start = i; 8 | while(s[start] == s[i+1]) i++; 9 | int end = i; 10 | 11 | while(start > -1 && end < (int)s.size() && s[start] == s[end]){ 12 | start--,end++; 13 | } 14 | if(end - start >= 3 && end-start-1 > anslen) ansidx = start+1,anslen = end - start - 1; 15 | i++; 16 | } 17 | return s.substr(ansidx,anslen); 18 | } 19 | }; 20 | 21 | -------------------------------------------------------------------------------- /00006. ZigZag Conversion.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | string convert(string s, int nRows) { 5 | if(nRows == 1) return s; 6 | int len = s.length(); 7 | 8 | const int dir[2][2] = {{1,0},{-1,1}}; 9 | char maze[nRows][len]; 10 | memset(maze,'$',sizeof(maze)); 11 | 12 | int i,j,k,dirk; 13 | i = -1; 14 | j = k = dirk = 0; 15 | while(k < len) 16 | { 17 | int x = i+dir[dirk][0]; 18 | int y = j+dir[dirk][1]; 19 | if(x>-1&&x-1&&y=star;i--){ 12 | j = str[i]-'0'; 13 | tmp = tmp*10+j; 14 | if(j != tmp%10) return 0; 15 | } 16 | return tmp*ans; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /00008. String to Integer(atoi).cpp: -------------------------------------------------------------------------------- 1 | const int minLen = 11; 2 | const int maxLen = 10; 3 | const char strMin[] ="-2147483648"; 4 | const char strMax[] ="2147483647"; 5 | char tmp[1024]; 6 | int myAtoi(char *str) { 7 | int flag = 1,star = 0; 8 | int ans = 0; 9 | int len = (int)strlen(str); 10 | while(star < len && str[star] == ' ') star++; 11 | if(str[star] == '-' || str[star] == '+'){ 12 | if(str[star] == '-'){ 13 | flag = -1; 14 | } 15 | else{ 16 | flag = 1; 17 | } 18 | } 19 | int end = star + (str[star]=='-'||str[star]=='+' ? 1 : 0); 20 | while(end < len && str[end]>='0' && str[end]<='9') end++; 21 | if(str[star] == '+') star++; 22 | memset(tmp, 0, sizeof(tmp)); 23 | strncpy(tmp, str+star, end - star); 24 | if(star-1 >-1 && str[star-1] == '+') star--; 25 | int tmpLen = (int)strlen(tmp); 26 | if(flag == -1){ 27 | if(tmpLen>minLen || (tmpLen == minLen && (strcmp(tmp, strMin) > 0))){ 28 | return INT_MIN; 29 | } 30 | } 31 | else{ 32 | if(tmpLen>maxLen || (tmpLen == maxLen && (strcmp(tmp, strMax) > 0))){ 33 | return INT_MAX; 34 | } 35 | } 36 | if(str[star] == '-' || str[star] == '+') star++; 37 | for(int i = star,j;i=0 && j<=9){ 40 | ans = (ans<<3) + (ans<<1); 41 | if(flag == -1) ans -= j; 42 | else ans += j; 43 | } 44 | else 45 | break; 46 | } 47 | return ans; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /00009. Palindrome Number.cpp: -------------------------------------------------------------------------------- 1 | char str[1024]; 2 | int reverse(int x) { 3 | int ans = 1,star = 0,tmp = 0; 4 | int len; 5 | sprintf(str,"%d",x); 6 | len = (int)strlen(str); 7 | if(str[0] == '-'){ 8 | star = 1; 9 | ans = -1; 10 | } 11 | for(int i = len-1,j;i>=star;i--){ 12 | j = str[i]-'0'; 13 | tmp = tmp*10+j; 14 | if(j != tmp%10) return 0; 15 | } 16 | return tmp*ans; 17 | } 18 | 19 | bool isPalindrome(int x) { 20 | if(x < 0) return false; 21 | int tmp = reverse(x); 22 | return tmp == x; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /00010. Regular Expression Matching.cpp: -------------------------------------------------------------------------------- 1 | bool isMatch(char* s, char* p) { 2 | if(*p == 0) return *s == 0; 3 | 4 | if(*(p+1) != '*'){ 5 | if(*s && ((*s == *p) || (*p == '.'))){ 6 | return isMatch(s+1,p+1); 7 | } 8 | return false; 9 | } 10 | else{ 11 | while(*s && ((*s==*p) || (*p == '.'))){ 12 | if(isMatch(s, p+2)) return true; 13 | s++; 14 | } 15 | return isMatch(s, p+2); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /00011. Container With Most Water.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | public int maxArea(int[] height) { 4 | int ans = 0; 5 | int left = 0; 6 | int right = height.length-1; 7 | 8 | while(left < right) 9 | { 10 | ans = Math.max(ans, Math.min(height[left], height[right])*(right-left)); 11 | 12 | if(height[left] < height[right]) left++; 13 | else right--; 14 | } 15 | 16 | return ans; 17 | } 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /00012. Integer to Roman.cpp: -------------------------------------------------------------------------------- 1 | //Accepted 57 ms cpp 2 | const char RomanNumbers[] = "IVXLCDM"; 3 | class Solution { 4 | public: 5 | string intToRoman(int num) { 6 | string ans=""; 7 | char tmp[1024]={'\0'}; 8 | sprintf(tmp,"%d",num); 9 | int len = (int)strlen(tmp); 10 | for(int i=0;i0;i--){ 32 | str+=RomanNumbers[index]; 33 | } 34 | } 35 | else{ 36 | str+=RomanNumbers[index]; 37 | str+=RomanNumbers[index+2]; 38 | } 39 | } 40 | return str; 41 | } 42 | }; 43 | 44 | 45 | -------------------------------------------------------------------------------- /00013. Roman to Integer.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 19 ms c 3 | 4 | const char RomanNumbers[] = "IVXLCDM"; 5 | int findRomanIndex(const char ch){ 6 | for(int i=0;i<7;i++) 7 | { 8 | if(ch == RomanNumbers[i]) 9 | return i; 10 | } 11 | return 0; 12 | } 13 | int romanToInt(char *s) { 14 | int number = 0; 15 | int len = (int)strlen(s); 16 | int fir = -1,sec = -1,tmp,index; 17 | for(int i=0;i= sec){ 31 | number += fir; 32 | fir = sec; 33 | sec = -1; 34 | } 35 | else{ 36 | number -= fir; 37 | fir = sec; 38 | sec = -1; 39 | } 40 | } 41 | } 42 | number += fir; 43 | return number; 44 | } 45 | 46 | 47 | -------------------------------------------------------------------------------- /00014. Longest Common Prefix.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 12 ms cpp 3 | class Solution { 4 | public: 5 | string longestCommonPrefix(vector &strs) { 6 | if (strs.empty()) { 7 | return ""; 8 | } 9 | if(strs.size() == 1){ 10 | return strs[0]; 11 | } 12 | sort(strs.begin(), strs.end(), [&](string a,string b){ 13 | return a.length() < b.length(); 14 | }); 15 | int n = (int)strs[0].size(); 16 | int m = (int)strs.size(); 17 | int p = 0; 18 | bool flag = true; 19 | while(flag){ 20 | for(int i=0;i > threeSum(vector &num) { 6 | vector > ans; 7 | if(num.size() < 3) return ans; 8 | sort(num.begin(),num.end()); 9 | int len = num.size(); 10 | int fir,sec,sum; 11 | for(int i=0;i 0) sec--; 22 | else if(fir != i+1 && num[fir] == num[fir-1]) fir++; 23 | else if(sec != len-1 && num[sec] == num[sec+1]) sec--; 24 | else 25 | { 26 | vector tmp(3); 27 | tmp[0] = num[i],tmp[1] = num[fir],tmp[2] = num[sec]; 28 | ans.push_back(tmp); 29 | fir++,sec--; 30 | } 31 | } 32 | } 33 | return ans; 34 | } 35 | }; 36 | 37 | 38 | -------------------------------------------------------------------------------- /00016. 3Sum Closest.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 88 ms cpp 3 | class Solution { 4 | public: 5 | int Abs(int a) 6 | { 7 | return a>0?a:-a; 8 | } 9 | int threeSumClosest(vector &num, int target) { 10 | sort(num.begin(),num.end()); 11 | int n = num.size()-2; 12 | int ans = -0x3fff3fff,tmp = 0; 13 | for(int i=0;i target) left--; 22 | else if(tmp < target) right++; 23 | else 24 | return target; 25 | } 26 | } 27 | return ans; 28 | } 29 | }; 30 | 31 | 32 | -------------------------------------------------------------------------------- /00017. Letter Combinations of a Phone Number.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 2 ms cpp 3 | char mp[10][5] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 4 | int mpIndex[10] = {0,0,3,3,3,3,3,4,3,4}; 5 | class Solution { 6 | public: 7 | vector letterCombinations(string digits) { 8 | vector ans; 9 | 10 | size_t m = digits.length(); 11 | if(m == 0) return ans; 12 | queue Q; 13 | Q.push(""); 14 | for(int i=0;i > fourSum(vector &num, int target) 6 | { 7 | vector > ans; 8 | if(num.size() < 4) return ans; 9 | sort(num.begin(),num.end()); 10 | int len = num.size(),sum,fir,sec; 11 | for(int i=0;i target) sec--; 24 | else if(fir != j+1 && num[fir] == num[fir-1]) fir++; 25 | else if(sec != len-1 && num[sec] == num[sec+1]) sec--; 26 | else 27 | { 28 | vector tmp(4); 29 | tmp[0] = num[i],tmp[1] = num[j]; 30 | tmp[2] = num[fir],tmp[3] = num[sec]; 31 | ans.push_back(tmp); 32 | fir++,sec--; 33 | } 34 | } 35 | } 36 | } 37 | return ans; 38 | } 39 | }; 40 | 41 | -------------------------------------------------------------------------------- /00019. Remove Nth Node From End of List.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 16 ms cpp 3 | class Solution { 4 | public: 5 | ListNode *removeNthFromEnd(ListNode *head, int n) { 6 | stack S; 7 | ListNode *node = head; 8 | while(node){ 9 | S.push(node); 10 | node = node->next; 11 | } 12 | ListNode * rear = NULL; 13 | int reIndex = 0; 14 | while(!S.empty()){ 15 | if(n == reIndex){ 16 | break; 17 | } 18 | else{ 19 | rear = S.top(); 20 | S.pop(); 21 | reIndex++; 22 | } 23 | } 24 | 25 | if(n == reIndex){ 26 | if(rear){ 27 | if(!S.empty()){ 28 | node = S.top(); 29 | node->next = rear->next; 30 | } 31 | else{ 32 | head = rear->next; 33 | } 34 | } 35 | else{ 36 | if(!S.empty()){ 37 | node = S.top(); 38 | node->next = NULL; 39 | } 40 | else{ 41 | return NULL; 42 | } 43 | } 44 | } 45 | return head; 46 | } 47 | 48 | }; 49 | 50 | -------------------------------------------------------------------------------- /00020. Vaild Parentrheses.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 2 ms cpp 3 | class Solution { 4 | public: 5 | bool isValid(string s) { 6 | stack S; 7 | for_each(s.begin(), s.end(), [&](char ch){ 8 | if(!S.empty() && ((S.top()=='('&& ch==')') || (S.top()=='['&&ch==']') || (S.top() == '{' && ch == '}'))){ 9 | S.pop(); 10 | } 11 | else{ 12 | S.push(ch); 13 | } 14 | }); 15 | return S.empty(); 16 | } 17 | }; 18 | 19 | -------------------------------------------------------------------------------- /00021. Merge Two Sorted Lists.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 21 ms cpp 3 | class Solution { 4 | public: 5 | ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { 6 | if(!l1 || !l2){ 7 | if(!l1 && !l2) return NULL; 8 | 9 | return l1 ? l1 : l2; 10 | } 11 | 12 | ListNode *head; 13 | if(l1->val < l2->val){ 14 | head = l1; 15 | l1 = l1->next; 16 | } 17 | else{ 18 | head = l2; 19 | l2 = l2->next; 20 | } 21 | ListNode *tmp = head; 22 | while(l1 && l2){ 23 | if(l1->val < l2->val){ 24 | tmp->next = l1; 25 | l1 = l1->next; 26 | } 27 | else{ 28 | tmp->next = l2; 29 | l2 = l2->next; 30 | } 31 | tmp = tmp->next; 32 | } 33 | 34 | if(l1) tmp->next = l1; 35 | if(l2) tmp->next = l2; 36 | return head; 37 | } 38 | }; 39 | 40 | 41 | -------------------------------------------------------------------------------- /00022. Generate Parentheses.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 4 ms cpp 3 | struct node 4 | { 5 | string state; 6 | int value; 7 | int leftCnt; 8 | int rightCnt; 9 | node(string _s="",int _v=0,int _l=0,int _r=0):state(_s),value(_v),leftCnt(_l),rightCnt(_r){} 10 | }; 11 | class Solution { 12 | public: 13 | vector generateParenthesis(int n) { 14 | len = 2*n-1; 15 | bfs(n); 16 | return ans; 17 | } 18 | void bfs(int n){ 19 | queue Q; 20 | node now,after; 21 | now.state +="("; 22 | now.value = 1; 23 | now.leftCnt = 1; 24 | Q.push(now); 25 | while(!Q.empty()){ 26 | now = Q.front(); 27 | Q.pop(); 28 | if(now.state.length() == 2*n && now.value == 0){ 29 | ans.push_back(now.state); 30 | continue; 31 | } 32 | if(now.value - 1 >= 0 && now.rightCnt < n){ 33 | after.state = now.state + ')'; 34 | after.value = now.value - 1; 35 | after.leftCnt = now.leftCnt; 36 | after.rightCnt = now.rightCnt + 1; 37 | Q.push(after); 38 | } 39 | if(now.value + 1 >= 0 && now.leftCnt < n){ 40 | after.state = now.state + '('; 41 | after.value = now.value + 1; 42 | after.rightCnt = now.rightCnt; 43 | after.leftCnt = now.leftCnt+1; 44 | Q.push(after); 45 | } 46 | } 47 | } 48 | Solution() 49 | { 50 | ans.clear(); 51 | } 52 | private: 53 | vector ans; 54 | int len; 55 | }; 56 | 57 | -------------------------------------------------------------------------------- /00023. Merge k Sorted Lists.java: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 400 ms java 3 | public class Solution { 4 | public ListNode mergeKLists(ArrayList lists) { 5 | ListNode head, tmp, p; 6 | Q = new PriorityQueue(1024, new cmp()); 7 | Q.clear(); 8 | for (int i = 0; i < lists.size(); i++) 9 | { 10 | tmp = lists.get(i); 11 | if(tmp != null) 12 | Q.offer(tmp); 13 | } 14 | tmp = Q.peek(); 15 | Q.poll(); 16 | if (tmp != null && tmp.next != null) 17 | Q.offer(tmp.next); 18 | head = tmp; 19 | while (!Q.isEmpty()) { 20 | p = Q.peek(); 21 | Q.poll(); 22 | if (p.next != null) 23 | Q.offer(p.next); 24 | tmp.next = p; 25 | tmp = p; 26 | } 27 | return head; 28 | } 29 | 30 | class cmp implements Comparator { 31 | public int compare(ListNode a, ListNode b) { 32 | return a.val - b.val; 33 | } 34 | } 35 | 36 | private PriorityQueue Q; 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /00024. Swap Nodes in Pairs.java: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 364 ms java 3 | public class Solution { 4 | public ListNode swapPairs(ListNode head) { 5 | if(head == null || head.next == null) return head; 6 | ListNode fir,sec,tmp; 7 | fir = head; 8 | sec = head.next; 9 | head = sec; 10 | fir.next = sec.next; 11 | sec.next = fir; 12 | tmp = fir; 13 | fir = fir.next; 14 | if(fir != null) 15 | sec = fir.next; 16 | else 17 | return head; 18 | while(fir != null && sec != null) 19 | { 20 | fir.next = sec.next; 21 | tmp.next = sec; 22 | sec.next = fir; 23 | tmp = fir; 24 | fir = fir.next; 25 | if(fir != null) 26 | sec = fir.next; 27 | } 28 | return head; 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /00025. Reverse Nodes in k-Group.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 35 ms cpp 3 | class Solution { 4 | public: 5 | ListNode *reverseKGroup(ListNode *head, int k) { 6 | if(!head) return NULL; 7 | 8 | stack nodeStack; 9 | vector reverseNodes; 10 | while(head){ 11 | nodeStack.push(head); 12 | head = head->next; 13 | nodeStack.top()->next = NULL; 14 | if(nodeStack.size() == k){ 15 | while (!nodeStack.empty()) { 16 | reverseNodes.push_back(nodeStack.top()); 17 | nodeStack.pop(); 18 | } 19 | } 20 | } 21 | 22 | vector anotherNodes; 23 | while(!nodeStack.empty()){ 24 | anotherNodes.push_back(nodeStack.top()); 25 | nodeStack.pop(); 26 | } 27 | 28 | for_each(anotherNodes.rbegin(), anotherNodes.rend(), [&](ListNode *node){ 29 | reverseNodes.push_back(node); 30 | }); 31 | 32 | head = reverseNodes[0]; 33 | ListNode *tmp = head; 34 | for(int i=1;inext = reverseNodes[i]; 37 | tmp = reverseNodes[i]; 38 | } 39 | 40 | return head; 41 | } 42 | }; 43 | 44 | -------------------------------------------------------------------------------- /00026. Remove Duplicates from Sorted Array.c: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 17 ms c 3 | int removeDuplicates(int A[], int n) { 4 | int *B = (int*)malloc(n*sizeof(int)); 5 | int start = 0; 6 | for(int i=0;i= disL){ 18 | if(divL >= disTmpL){ 19 | divL -= disTmpL; 20 | ans += cnt; 21 | } 22 | cnt >>= 1; 23 | disTmpL >>= 1; 24 | } 25 | 26 | return (flag ? -ans : ans); 27 | } 28 | -------------------------------------------------------------------------------- /00030. Substring with Concatenation of All Words.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 611 ms cpp 3 | class Solution { 4 | public: 5 | vector findSubstring(string s, vector& words) { 6 | vector ans; 7 | if(words.empty() || s.length() < words.size() * words.front().length()) return ans; 8 | size_t wordLen = words.front().length(); 9 | map dict; 10 | for_each(words.begin(), words.end(), [&](const string tmp){ 11 | dict[tmp]++; 12 | }); 13 | for(size_t i = 0;i<=s.length() - wordLen*words.size();i++){ 14 | size_t j = 0; 15 | map tmpDict; 16 | for(j = 0;j& nums) { 6 | next_permutation(nums.begin(), nums.end(), less() ); 7 | } 8 | 9 | }; 10 | 11 | -------------------------------------------------------------------------------- /00032. Longest Valid Parentheses.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 8 ms cpp 3 | class Solution { 4 | public: 5 | int longestValidParentheses(string s) { 6 | int ans = 0; 7 | int len = (int)s.length(); 8 | int cnt = 0; 9 | int start = 0,end = len - 1; 10 | for(int i=0;i-1;i--){ 22 | if(s[i] == ')') cnt++; 23 | else cnt --; 24 | 25 | if(cnt == 0) ans = max(ans,end - i + 1); 26 | else if(cnt < 0){ 27 | cnt = 0; 28 | end = i-1; 29 | } 30 | } 31 | return ans; 32 | } 33 | }; 34 | 35 | -------------------------------------------------------------------------------- /00033. Search in Rotated Sorted Array.c: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 0 ms c 3 | int search(int* nums, int numsSize, int target) { 4 | int i=0; 5 | for(i =0;i searchRange(int A[], int n, int target) { 6 | // IMPORTANT: Please reset any member data you declared, as 7 | // the same Solution instance will be reused for each test case. 8 | vector ans; 9 | int p = 0 , q = n-1; 10 | int mid; 11 | while(p <= q) 12 | { 13 | mid = ((q-p)>>1)+p; 14 | if(A[mid] < target) p = mid+1; 15 | else if(A[mid] > target) q = mid-1; 16 | else break; 17 | } 18 | 19 | if(A[mid]!=target) 20 | { 21 | ans.push_back(-1); 22 | ans.push_back(-1); 23 | return ans; 24 | } 25 | p = mid; 26 | while(p > 0 && A[p-1] == target) p--; 27 | q = mid; 28 | while(q < n-1 && A[q+1] == target) q++; 29 | ans.push_back(p); 30 | ans.push_back(q); 31 | return ans; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /00035. Search Insert Position.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 16 ms cpp 3 | class Solution { 4 | public: 5 | int searchInsert(int A[], int n, int target) { 6 | if(target <= A[0]) return 0; 7 | else if(target > A[n-1]) return n; 8 | 9 | int right = 0,left = n; 10 | int mid; 11 | while(right < left) 12 | { 13 | mid = ((left-right)>>1) + right; 14 | if(target > A[mid]) right = mid; 15 | else if(target < A[mid]) left = mid; 16 | 17 | if((target<=A[mid] && target>A[mid-1])) return mid; 18 | } 19 | return -1; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /00036. Valid Sudoku.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 56 ms cpp 3 | class Solution { 4 | public: 5 | Solution() 6 | { 7 | memset(row,false,sizeof(row)); 8 | memset(col,false,sizeof(col)); 9 | memset(block,false,sizeof(block)); 10 | } 11 | bool isValidSudoku(vector > &board) { 12 | int num; 13 | for(unsigned i=0;i > combinationSum(vector &candidates, int target) { 6 | vector > ans; 7 | 8 | vector tmp; 9 | 10 | sort(candidates.begin(),candidates.end()); 11 | 12 | dfs(ans,candidates,tmp,target); 13 | 14 | return ans; 15 | } 16 | 17 | void dfs(vector >& A,vector& C,vector& T,int target) 18 | { 19 | if(target == 0) 20 | { 21 | vector tmp(T); 22 | A.push_back(tmp); 23 | return ; 24 | } 25 | for(int i=0;i= C[i] &&(T.size() == 0 || C[i]>=T.back())) 28 | { 29 | T.push_back(C[i]); 30 | dfs(A,C,T,target-C[i]); 31 | T.pop_back(); 32 | } 33 | // else return ; 34 | } 35 | } 36 | }; 37 | 38 | -------------------------------------------------------------------------------- /00040. Combination Sum II.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 92 ms cpp 3 | class Solution { 4 | public: 5 | vector > combinationSum2(vector &num, int target) { 6 | vector > ans; 7 | vector tmp; 8 | S.clear(); 9 | sort(num.begin(),num.end()); 10 | memset(vis,false,sizeof(vis)); 11 | dfs(ans,num,tmp,0,0,0,target); 12 | return ans; 13 | } 14 | void dfs(vector >& ans,vector& num,vector& tmp,int star,int step,int sum,int target) 15 | { 16 | if(sum == target) 17 | { 18 | if(!S.count(tmp)) 19 | { 20 | ans.push_back(tmp); 21 | S.insert(tmp); 22 | } 23 | return ; 24 | } 25 | for(int i=star;i<(int)num.size();i++) 26 | { 27 | if(!vis[i] && sum+num[i] <= target) 28 | { 29 | vis[i] = true; 30 | tmp.push_back(num[i]); 31 | dfs(ans,num,tmp,i+1,step+1,sum+num[i],target); 32 | // if(i+1 < (int)num.size() && num[i] == num[i+1] ) i++; 33 | vis[i] = false; 34 | tmp.erase(--tmp.end()); 35 | } 36 | else return ; 37 | } 38 | } 39 | private: 40 | bool vis[10240]; 41 | set > S; 42 | }; 43 | 44 | -------------------------------------------------------------------------------- /00041. First Missing Positive.c: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 2 ms c 3 | void myswap(int *a, int *b){ 4 | int tmp = *a; 5 | *a = *b; 6 | *b = tmp; 7 | } 8 | void output(int *nums,int n){ 9 | for(int i=0;i0 && nums[i]<=numsSize && nums[nums[i]-1] != nums[i]){ 20 | myswap(&(nums[nums[i]-1]), &(nums[i])); 21 | } 22 | // output(nums, numsSize); 23 | } 24 | // output(nums,numsSize); 25 | for(int i=0;i A[i]) 27 | ans += (tmp - A[i]); 28 | } 29 | return ans; 30 | } 31 | 32 | void output(int *leftMax,int *rightMax,int n) 33 | { 34 | printf("this is leftMax:\n"); 35 | for(int i=0;i= n-1) break; 14 | 15 | if( i <= oldReach) newReach = max(newReach,A[i]+i); 16 | else 17 | { 18 | if(oldReach >= newReach) return -1; 19 | oldReach = newReach , steps++,i--; 20 | } 21 | } 22 | return steps; 23 | } 24 | }; 25 | 26 | -------------------------------------------------------------------------------- /00046. Permutations.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 12 ms cpp 3 | class Solution { 4 | public: 5 | vector> permute(vector& nums) { 6 | vector> ans; 7 | sort(nums.begin(), nums.end()); 8 | do{ 9 | ans.push_back(nums); 10 | }while (next_permutation(nums.begin(), nums.end())); 11 | 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /00047. Permutations II.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 28 ms cpp 3 | class Solution { 4 | public: 5 | vector> permuteUnique(vector& nums) { 6 | vector> ans; 7 | sort(nums.begin(), nums.end()); 8 | do{ 9 | ans.push_back(nums); 10 | }while (next_permutation(nums.begin(), nums.end())); 11 | 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /00048. Rotate Image.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 4 ms cpp 3 | class Solution { 4 | public: 5 | void rotate(vector>& matrix) { 6 | int n = (int)matrix.size(); 7 | for(int i=0;i>& nums){ 22 | for_each(nums.begin(), nums.end(), [&](const vector &items){ 23 | for_each(items.begin(), items.end(), [&](const int &item){ 24 | cout< anagrams(vector &strs) { 6 | vector ans; 7 | map M; 8 | for(size_t i = 0;i < strs.size();i++) 9 | { 10 | string str = strs[i]; 11 | sort(str.begin(),str.end()); 12 | 13 | if(M.find(str) == M.end()) 14 | M[str] = i; 15 | else 16 | { 17 | if(M[str] != -1) 18 | ans.push_back(strs[M[str]]); 19 | ans.push_back(strs[i]); 20 | M[str] = -1; 21 | } 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /00050. Pow(x,n).cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 44 ms cpp 3 | class Solution { 4 | public: 5 | double pow(double x, int n) { 6 | if(n >= 0) return myPow(x,n); 7 | else return 1/myPow(x,-n); 8 | } 9 | 10 | double myPow(double x,int n) 11 | { 12 | if(n == 0) return 1; 13 | if(n == 1) return x; 14 | double tmp = myPow(x,n/2); 15 | if(n&1) return tmp*tmp*x; 16 | return tmp*tmp; 17 | } 18 | }; 19 | 20 | -------------------------------------------------------------------------------- /00051. N-Queens.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 16 ms cpp 3 | class Solution { 4 | private: 5 | vector _Qcol; 6 | vector > _ans; 7 | int _n; 8 | public: 9 | vector > solveNQueens(int n) { 10 | _Qcol = vector(n,0); 11 | _n = n; 12 | 13 | dfs(0); 14 | 15 | return _ans; 16 | } 17 | 18 | void dfs(int step) 19 | { 20 | if(step == _n) 21 | { 22 | vector tmp; 23 | for(int i=0;i<_n;i++) 24 | { 25 | string str = string(_n,'.'); 26 | str[_Qcol[i]] = 'Q'; 27 | tmp.push_back(str); 28 | } 29 | _ans.push_back(tmp); 30 | } 31 | for(int i=0;i<_n;i++) 32 | { 33 | if(judge(step,i)) 34 | { 35 | _Qcol[step] = i; 36 | dfs(step+1); 37 | _Qcol[step] = 0; 38 | 39 | } 40 | } 41 | } 42 | 43 | bool judge(const int x,const int y) 44 | { 45 | for(int i=0;i _Qcol; 7 | int _ans; 8 | int _n; 9 | public: 10 | int totalNQueens(int n) { 11 | _n = n; 12 | _ans = 0; 13 | _Qcol = vector(n,0); 14 | dfs(0); 15 | return _ans; 16 | } 17 | void dfs(int step) 18 | { 19 | if(step == _n) 20 | { 21 | _ans++; 22 | return ; 23 | } 24 | for(int i=0;i<_n;i++) 25 | { 26 | if(judge(step,i)) 27 | { 28 | _Qcol[step] = i; 29 | dfs(step+1); 30 | _Qcol[step] = 0; 31 | 32 | } 33 | } 34 | } 35 | 36 | bool judge(const int x,const int y) 37 | { 38 | for(int i=0;inums[i]?nums[i]+dp[i-1]:nums[i]; 9 | if(dp[i] > ans) ans = dp[i]; 10 | } 11 | free(dp); 12 | return ans; 13 | } 14 | -------------------------------------------------------------------------------- /00054. Spiral Matric.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 4 ms cpp 3 | class Solution { 4 | private: 5 | unsigned _n,_m; 6 | char _star; 7 | const int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; 8 | public: 9 | vector spiralOrder(vector > &matrix) { 10 | vector ans; 11 | _n = (int)matrix.size(); 12 | if(_n < 1) return ans; 13 | _m = (int)matrix[0].size(); 14 | _star = 0; 15 | 16 | 17 | int count = _n*_m; 18 | int x,y,xx,yy; 19 | x = y = 0; 20 | while(count--) 21 | { 22 | ans.push_back(matrix[x][y]); 23 | matrix[x][y] = -778477; 24 | xx = x + dir[_star][0]; 25 | yy = y + dir[_star][1]; 26 | if(judge(xx, yy) && matrix[xx][yy] != -778477) 27 | x = xx , y = yy; 28 | else 29 | { 30 | _star = (_star+1)%4; 31 | x = x + dir[_star][0]; 32 | y = y + dir[_star][1]; 33 | } 34 | } 35 | 36 | return ans; 37 | } 38 | bool judge(const int x,const int y) 39 | { 40 | return (x>-1&&x<_n&&y>-1&&y<_m); 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /00055. Jump Game.java: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 376 ms java 3 | public class Solution { 4 | public boolean canJump(int[] A) { 5 | int limit = A.length-1; 6 | for(int i=A.length-2;i>-1;i--) 7 | if(i+A[i] >= limit) limit = i; 8 | if(limit == 0) return true; 9 | 10 | return false; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /00056. Merge Intervals.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 19 ms cpp 3 | class Solution { 4 | public: 5 | vector merge(vector &intervals) { 6 | if(intervals.size() < 1) return vector(); 7 | sort(intervals.begin(), intervals.end(), [&](const Interval& a,const Interval& b){ 8 | if(a.start == b.start){ 9 | return a.end < b.end; 10 | } 11 | return a.start < b.start; 12 | }); 13 | vector ans; 14 | Interval interval(intervals[0].start,intervals[0].end); 15 | for(int i=1;i -1 && s[j] == ' ') j--; 9 | if(j < i) return 0; 10 | while(j>=i && s[j]!=' ') j--,ans++; 11 | 12 | return ans; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /00059. Spiral Matrix II.java: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 332 ms java 3 | public class Solution { 4 | public int[][] generateMatrix(int n) { 5 | int [][]maze = new int[n][n]; 6 | final int dir[][] = {{0,1},{1,0},{0,-1},{-1,0}}; 7 | int _i,_j,count,length,k,i,j; 8 | length = n*n; 9 | count = 1; 10 | k = _i = _j = 0; 11 | while(count <= length) 12 | { 13 | maze[_i][_j] = count++; 14 | i = _i+dir[k][0]; 15 | j = _j+dir[k][1]; 16 | if(i<0||i>=n||j<0||j>=n||maze[i][j]!=0) 17 | { 18 | k = (k+1)%4; 19 | i = _i+dir[k][0]; 20 | j = _j+dir[k][1]; 21 | } 22 | _i = i; 23 | _j = j; 24 | } 25 | return maze; 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /00060. Permutation Sequence.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 0 ms cpp 3 | 4 | class Solution { 5 | public: 6 | string getPermutation(int n, int k) { 7 | 8 | string str = ""; 9 | vector numbers; 10 | vector ans; 11 | for(int i=1;i<=n;i++) numbers.push_back(i); 12 | 13 | if(k == jiecheng(n)){ 14 | for(int i=n;i>0;i--) str+=('0'+i); 15 | return str; 16 | } 17 | 18 | for(int i=1;inext)) return head; 9 | vector V; 10 | ListNode *tmp = head; 11 | while(tmp){ 12 | V.push_back(tmp); 13 | tmp = tmp->next; 14 | V.back()->next = NULL; 15 | } 16 | if(k%V.size() == 0) k = (int)V.size(); 17 | else k%=V.size(); 18 | head = V[V.size() - k]; 19 | ListNode *node = head; 20 | for(int i = (int)V.size() - k + 1; i < V.size(); i++){ 21 | node->next = V[i]; 22 | node = node->next; 23 | } 24 | 25 | for(int i=0;i<(int)V.size() - k;i++){ 26 | node->next = V[i]; 27 | node = node->next; 28 | } 29 | return head; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /00062. Unique Paths.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 4 ms cpp 3 | class Solution { 4 | private: 5 | int _maze[101][101]; 6 | public: 7 | int uniquePaths(int m, int n) { 8 | memset(_maze, 0, sizeof(_maze)); 9 | 10 | for(int i=0;i > &obstacleGrid) { 8 | memset(_maze, 0, sizeof(_maze)); 9 | 10 | int n = (int)obstacleGrid.size(); 11 | int m = (int)obstacleGrid[0].size(); 12 | 13 | for(int i=0;i plusOne(vector& digits) { 6 | vector ans; 7 | 8 | std::reverse(digits.begin(), digits.end()); 9 | 10 | int tmp = 1; 11 | 12 | for_each(digits.begin(), digits.end(), [&](int& num){ 13 | num += tmp; 14 | tmp = num/10; 15 | num %= 10; 16 | ans.push_back(num); 17 | }); 18 | 19 | if(tmp) ans.push_back(1); 20 | std::reverse(ans.begin(), ans.end()); 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /00067. Add Binary.java: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 376 ms java 3 | public class Solution { 4 | public String addBinary(String a, String b) { 5 | StringBuffer sb = new StringBuffer(); 6 | if(a.length() > b.length()) 7 | { 8 | String tmp = a; 9 | a = b; 10 | b = tmp; 11 | } 12 | 13 | // System.out.println("a : " + a + "\nb : " + b); 14 | star = 1; 15 | base = 0; 16 | carry = 0; 17 | while(star <= a.length()) 18 | { 19 | base = (a.charAt(a.length()-star)-'0') + (b.charAt(b.length()-star)-'0'); 20 | base += carry; 21 | carry = base / 2; 22 | base = base % 2; 23 | sb.append(base); 24 | star++; 25 | } 26 | 27 | while(star <= b.length()) 28 | { 29 | base = carry + (b.charAt(b.length() - star)-'0'); 30 | carry = base / 2; 31 | base = base % 2; 32 | sb.append(base); 33 | star++; 34 | } 35 | if(carry == 1) 36 | sb.append(carry); 37 | sb.reverse(); 38 | return sb.toString(); 39 | } 40 | private int base,carry,star; 41 | } 42 | -------------------------------------------------------------------------------- /00069. Sqrt(x).c: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 4 ms c 3 | int mySqrt(int x) { 4 | 5 | 6 | float f = x; 7 | int valInt = *(int *)&f; 8 | valInt = (1 << 29) + (valInt >> 1) - (1 << 2) + 2; // guess the star number 9 | f = *(float *)&valInt; 10 | 11 | double val = f; 12 | val = 0.5*val + 0.5*x / val; 13 | val = 0.5*val + 0.5*x / val; 14 | val = 0.5*val + 0.5*x / val; 15 | val = 0.5*val + 0.5*x / val; 16 | 17 | return val; 18 | } 19 | -------------------------------------------------------------------------------- /00070. Climbing Stairs.java: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 308 ms java 3 | public class Solution { 4 | public int climbStairs(int n) { 5 | int fir = 1; 6 | int sec = 2; 7 | if(n == 1) return 1; 8 | if(n == 2) return 2; 9 | 10 | int count = 3; 11 | int ans = fir+sec; 12 | while(count < n) 13 | { 14 | count++; 15 | fir = sec; 16 | sec = ans; 17 | ans = fir+sec; 18 | } 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /00072. Edit Distance.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 25 ms cpp 3 | class Solution { 4 | public: 5 | int minDistance(string word1, string word2) { 6 | int n = word1.size(); 7 | int m = word2.size(); 8 | if(0 == n || 0 == m) return max(n,m); 9 | 10 | int dp[n+1][m+1]; 11 | for(int i=0;i<=n;i++) dp[i][0] = i; 12 | for(int i=0;i<=m;i++) dp[0][i] = i; 13 | 14 | for(int i=1;i<=n;i++) 15 | for(int j=1;j<=m;j++){ 16 | int cost = word1[i-1] == word2[j-1] ? 0 : 1; 17 | dp[i][j] = min(min(dp[i-1][j] + 1, dp[i][j-1] + 1), dp[i-1][j-1] + cost); 18 | } 19 | return dp[n][m]; 20 | } 21 | }; 22 | 23 | -------------------------------------------------------------------------------- /00073. Set Matrix Zeroes.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 99 ms cpp 3 | class Solution { 4 | public: 5 | void setZeroes(vector > &matrix) { 6 | if(matrix.size() < 1) return; 7 | vector row(matrix.size(),false); 8 | vector col(matrix[0].size(),false); 9 | 10 | for(int i=0;i>1); 9 | int row = mid/matrixColSize; 10 | int col = mid%matrixColSize; 11 | if(matrix[row][col] == target) return true; 12 | else if(matrix[row][col] > target){ 13 | right = mid - 1; 14 | } 15 | else{ 16 | left = mid+1; 17 | } 18 | } 19 | 20 | return false; 21 | } 22 | -------------------------------------------------------------------------------- /00075. Sort Colors.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 12 ms cpp 3 | class Solution { 4 | public: 5 | Solution() 6 | { 7 | _red = _white = _blue = 0; 8 | } 9 | void sortColors(int A[], int n) { 10 | for(int i=0;i s.length()) return ans; 8 | 9 | vector countTable(256,0); 10 | 11 | for_each(t.begin(), t.end(), [&](const char ch){ 12 | countTable[ch]++; 13 | }); 14 | 15 | vector foundTable(256,0); 16 | int count(0),pos(0); 17 | unsigned long maxLen(s.length()); 18 | for(size_t i = 0;i 0){ 20 | foundTable[s[i]] ++; 21 | if(foundTable[s[i]] <= countTable[s[i]]) count++; 22 | if(count == t.length()){ 23 | for(int j=pos;j<=i;j++){ 24 | if(countTable[s[j]] > 0){ 25 | foundTable[s[j]] --; 26 | if(foundTable[s[j]] < countTable[s[j]]){ 27 | pos = j; 28 | break; 29 | } 30 | } 31 | } 32 | // cout< > combine(int n, int k) { 6 | vector > ans; 7 | 8 | vector tmp; 9 | 10 | // bool *vis = (bool*)malloc(sizeof(bool)*(n+1)); 11 | // 12 | // memset(vis,false,sizeof(vis)); 13 | 14 | dfs(ans,tmp,1,n,0,k); 15 | 16 | return ans; 17 | } 18 | 19 | void dfs(vector >& A,vector& T,int star,int n,int step,int k) 20 | { 21 | if(step == k) 22 | { 23 | vector tmp(T); 24 | A.push_back(tmp); 25 | return ; 26 | } 27 | 28 | for(int i=star;i<=n;i++) 29 | { 30 | if((T.size()==0 ||(i>T.back()))) 31 | { 32 | T.push_back(i); 33 | dfs(A,T,star+1,n,step+1,k); 34 | T.pop_back(); 35 | } 36 | } 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /00078. Subsets.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 44 ms cpp 3 | class Solution { 4 | private: 5 | vector > _ans; 6 | vector _vis; 7 | vector _S; 8 | int _n; 9 | public: 10 | vector > subsets(vector &S) { 11 | _ans.clear(); 12 | _ans.push_back(vector()); 13 | 14 | _vis = vector(S.size() + 1,false); 15 | _n = (int)S.size(); 16 | _S = S; 17 | sort(_S.begin(), _S.end()); 18 | 19 | vector tmp; 20 | for(unsigned i=1;i<=S.size();i++) 21 | { 22 | tmp.clear(); 23 | dfs(0,i,tmp); 24 | } 25 | return _ans; 26 | } 27 | void dfs(int step,int n,vector& tmp) 28 | { 29 | if(step == n) 30 | { 31 | _ans.push_back(tmp); 32 | return; 33 | } 34 | 35 | for(int i=step;i<_n;i++) 36 | { 37 | if(!_vis[i]) 38 | { 39 | if(step && tmp[step-1] > _S[i]) continue; 40 | tmp.push_back(_S[i]); 41 | _vis[i] = true; 42 | dfs(step+1, n,tmp); 43 | _vis[i] = false; 44 | tmp.pop_back(); 45 | } 46 | } 47 | } 48 | }; 49 | 50 | -------------------------------------------------------------------------------- /00079. Word Search.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 96 ms cpp 3 | class Solution { 4 | public: 5 | bool exist(vector > &board, string word) 6 | { 7 | n = board.size(); 8 | m = board[0].size(); 9 | if(n*m < word.length()) return false; 10 | memset(vis,false,sizeof(vis)); 11 | for(int i=0;i > &board, string word,int x,int y,int step) 26 | { 27 | if(step == word.length()) return true; 28 | int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; 29 | for(int i=0;i<4;i++) 30 | { 31 | int xx = x+dir[i][0]; 32 | int yy = y+dir[i][1]; 33 | if(xx>-1&&xx-1&&yy& nums) { 6 | if(nums.size() < 3) return (int)nums.size(); 7 | 8 | vector::iterator iter = nums.begin()+1; 9 | int count = 1; 10 | int num = *(nums.begin()); 11 | 12 | for(;iter < nums.end() ; iter++){ 13 | if(num == *iter){ 14 | count++; 15 | if(count == 3){ 16 | nums.erase(iter); 17 | iter--; 18 | count--; 19 | } 20 | } 21 | else{ 22 | num = *iter; 23 | count = 1; 24 | } 25 | } 26 | return (int)nums.size(); 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /00081. Search in Rotated Sorted Array II.c: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 4 ms c 3 | bool search(int* nums, int numsSize, int target) { 4 | int i=0; 5 | for(i =0;inext) return head; 8 | ListNode *tmp = head; 9 | 10 | int count = 1; 11 | int val = head->val; 12 | bool flag = false; 13 | while (tmp->next) { 14 | if(tmp->next->val == val) count++; 15 | else{ 16 | if(count == 1) { 17 | head = tmp; 18 | flag = true; 19 | break; 20 | } 21 | else{ 22 | count = 1; 23 | val = tmp->next->val; 24 | } 25 | } 26 | tmp = tmp->next; 27 | } 28 | if(count == 1) head = tmp,flag = true; 29 | if(!flag) return NULL; 30 | 31 | ListNode *fir = head->next; 32 | ListNode *sec = NULL; 33 | if(fir && fir->next) sec = fir->next; 34 | val = head->val; 35 | 36 | tmp->next = NULL; 37 | 38 | while(fir && sec){ 39 | if(fir->val != val && fir->val != sec->val){ 40 | tmp->next = new ListNode(fir->val); 41 | tmp = tmp->next; 42 | } 43 | 44 | val = fir->val; 45 | fir = sec; 46 | sec = fir->next; 47 | } 48 | 49 | if(fir && fir->val != val){ 50 | tmp->next = fir; 51 | } 52 | 53 | return head; 54 | } 55 | }; 56 | 57 | -------------------------------------------------------------------------------- /00083. Remove Duplicates from Sorted List.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 80 ms cpp 3 | class Solution { 4 | public: 5 | ListNode *deleteDuplicates(ListNode *head) { 6 | if(!head) return NULL; 7 | ListNode *s,*p; 8 | s = head; 9 | p = s->next; 10 | while(p) 11 | { 12 | if(p->val == s->val) 13 | { 14 | s->next = p->next; 15 | free(p); 16 | p = s->next; 17 | } 18 | else 19 | { 20 | s = p; 21 | p = p->next; 22 | } 23 | } 24 | return head; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /00084. Largest Rectangle in Histogram.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 24 ms cpp 3 | 4 | class Solution { 5 | public: 6 | int largestRectangleArea(vector& height) { 7 | int ans = 0; 8 | height.push_back(0); 9 | int n = (int)height.size(); 10 | stack s; 11 | int ranger = 0; 12 | while(ranger < n){ 13 | if(s.empty() || height[ranger] >= height[s.top()]) s.push(ranger++); 14 | else{ 15 | int index = s.top(); 16 | s.pop(); 17 | ans = max(ans, height[index] * (s.empty() ? ranger : ranger - s.top() - 1)); 18 | } 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /00085. Maximal Rectangle.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 28 ms cpp 3 | class Solution { 4 | public: 5 | int largestRectangleArea(vector& height) { 6 | int ans = 0; 7 | height.push_back(0); 8 | int n = (int)height.size(); 9 | stack s; 10 | int ranger = 0; 11 | while(ranger < n){ 12 | if(s.empty() || height[ranger] >= height[s.top()]) s.push(ranger++); 13 | else{ 14 | int index = s.top(); 15 | s.pop(); 16 | ans = max(ans, height[index] * (s.empty() ? ranger : ranger - s.top() - 1)); 17 | } 18 | } 19 | return ans; 20 | } 21 | 22 | int maximalRectangle(vector>& matrix) { 23 | if(!matrix.size()) return 0; 24 | size_t n = matrix.size(),m = matrix[0].size(); 25 | vector heights(m,0); 26 | // init 27 | for(int i=0;i b ? a : b; 47 | } 48 | }; 49 | 50 | -------------------------------------------------------------------------------- /00086. Partition List.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 8 ms cpp 3 | class Solution { 4 | public: 5 | ListNode* partition(ListNode* head, int x) { 6 | vector vals; 7 | 8 | ListNode *tmp = head; 9 | while(tmp){ 10 | vals.push_back(tmp->val); 11 | tmp = tmp->next; 12 | } 13 | 14 | 15 | vector smallVals; 16 | vector bigVals; 17 | 18 | for_each(vals.begin(), vals.end(), [&](const int num){ 19 | if(num >= x){ 20 | bigVals.push_back(num); 21 | } 22 | else{ 23 | smallVals.push_back(num); 24 | } 25 | }); 26 | 27 | 28 | for_each(bigVals.begin(), bigVals.end(), [&](const int num){ 29 | smallVals.push_back(num); 30 | }); 31 | 32 | int len = (int)smallVals.size(); 33 | if(len >= 1) head->val = smallVals[0]; 34 | tmp = head; 35 | for(int i=1;inext = node; 38 | tmp = node; 39 | } 40 | return head; 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /00088. Merge Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 40 ms cpp 3 | class Solution { 4 | public: 5 | void merge(int A[], int m, int B[], int n) { 6 | for(int i=0;i-1 && A[j] > key) 12 | { 13 | A[index] = A[j]; 14 | index = j; 15 | j--; 16 | } 17 | A[j+1] = key; 18 | m++; 19 | } 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /00089. Gray code.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | [Gray Code](https://leetcode.com/problems/gray-code/) 4 | 5 | 6 | 7 | 这里是[Gray code](https://en.wikipedia.org/wiki/Gray_code)维基百科的定义 8 | 9 | https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Binary-reflected_Gray_code_construction.svg/250px-Binary-reflected_Gray_code_construction.svg.png 10 | 11 | 其实解题思路,看上面那张图就够了。模拟实现就ok了。 12 | 13 | 我是用string来模拟,主要是方便插入前面的0和1.最后再 处理一下 `Binary String to Integer` 14 | 15 | 16 | > http://stackoverflow.com/questions/23596988/binary-string-to-integer-with-atoi 17 | */ 18 | 19 | 20 | class Solution { 21 | public: 22 | vector grayCode(int n) { 23 | vector ans; 24 | ans.push_back(0); 25 | 26 | if(n >= 1){ 27 | vector tmp; 28 | string str("0"); 29 | string str1("1"); 30 | tmp.push_back(str); 31 | tmp.push_back(str1); 32 | 33 | for(int i=1;i res = tmp; 35 | reverse(res.begin(), res.end()); 36 | 37 | int m = (int)tmp.size(); 38 | for(int i=0;i> subsetsWithDup(vector& nums) { 12 | vector > ans; 13 | sort(nums.begin(), nums.end()); 14 | hashMap.clear(); 15 | 16 | len = (unsigned)nums.size(); 17 | 18 | for(unsigned i = 0; i <= len; i++){ 19 | dfs({},0,0,i,ans,nums); 20 | } 21 | 22 | 23 | return ans; 24 | } 25 | 26 | void dfs(vector tmp,unsigned star,unsigned step,unsigned total, vector> &ans,const vector nums){ 27 | if(step == total && !hashMap.count(tmp)){ 28 | hashMap.insert(tmp); 29 | ans.push_back(tmp); 30 | return; 31 | } 32 | 33 | for(unsigned i = star; i < len; i++){ 34 | tmp.push_back(nums[i]); 35 | dfs(tmp,i+1,step+1,total,ans,nums); 36 | tmp.pop_back(); 37 | } 38 | } 39 | 40 | private: 41 | set> hashMap; 42 | unsigned len; 43 | }; 44 | -------------------------------------------------------------------------------- /00091. Decode Ways.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 4 ms cpp 3 | class Solution { 4 | public: 5 | int numDecodings(string s) { 6 | int count = 0; 7 | if(s.length() == 0) return 0; 8 | if(s[0] == '0') return 0; 9 | 10 | int *dp = (int*)malloc(sizeof(int) * (s.length()+1)); 11 | memset(dp, 0, sizeof(int)*(s.length() + 1)); 12 | dp[0] = 1; 13 | dp[1] = s[0] != '0' ? 1:0; 14 | 15 | for(int i=2;i<=s.length();i++){ 16 | int a = stoi(s.substr(i-1,1)); 17 | int b= stoi(s.substr(i-2,2)); 18 | 19 | if(a >= 1 && a<= 9){ 20 | dp[i] += dp[i-1]; 21 | } 22 | 23 | if(b >= 10 && b <= 26){ 24 | dp[i] += dp[i-2]; 25 | } 26 | } 27 | 28 | 29 | count = dp[s.length()]; 30 | free(dp); 31 | return count; 32 | } 33 | 34 | }; 35 | -------------------------------------------------------------------------------- /00093. Restore IP Addresses.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | [Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/) 3 | 4 | 5 | 暴力枚举 6 | */ 7 | 8 | 9 | class Solution { 10 | public: 11 | vector restoreIpAddresses(string s) { 12 | vector ans; 13 | 14 | dfs(1,4,1,s,{0},ans); 15 | 16 | return ans; 17 | } 18 | 19 | void dfs(unsigned step, unsigned total, unsigned star, const string& str,vector idxs, vector& ans){ 20 | if( (str.size() - star)*1.0 / (total - step + 1) > 3.0) return; 21 | 22 | if(step == total && vaildIPSection(str, idxs[step - 1], (unsigned)str.size())){ 23 | string tmp = str; 24 | for(int i=1;i 1){ 47 | unsigned limit = pow(10,(end-star-1)) - 1; 48 | if(num <= limit) return false; 49 | } 50 | 51 | return num <= 255; 52 | } 53 | }; 54 | 55 | -------------------------------------------------------------------------------- /00094. Binary Tree Inorder Traversal.java: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 344 ms java 3 | public class Solution { 4 | public ArrayList inorderTraversal(TreeNode root) { 5 | ArrayList ans = new ArrayList(); 6 | 7 | midLeftRight(root,ans); 8 | 9 | return ans; 10 | } 11 | 12 | public void midLeftRight(TreeNode root,ArrayList ans) 13 | { 14 | if(root == null) return ; 15 | midLeftRight(root.left, ans); 16 | ans.add(root.val); 17 | midLeftRight(root.right, ans); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /00096. Unique Binary Search Trees.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 96. Unique Binary Search Trees 4 | Total Accepted: 102874 5 | Total Submissions: 260772 6 | Difficulty: Medium 7 | Contributors: Admin 8 | Given n, how many structurally unique BST's (binary search trees) that store values 1...n? 9 | 10 | For example, 11 | Given n = 3, there are a total of 5 unique BST's. 12 | 13 | 1 3 3 2 1 14 | \ / / / \ \ 15 | 3 2 1 1 3 2 16 | / / \ \ 17 | 2 1 2 3 18 | Subscribe to see which companies asked this question */ 19 | 20 | 21 | /* 22 | 23 | 19 / 19 test cases passed. 24 | Status: Accepted 25 | Runtime: 0 ms 26 | Submitted: 0 minutes ago 27 | 28 | 29 | 1. 枚举 根节点 1...n 30 | 2. 该根节点下的 左子树个数 * 右子树个数 31 | */ 32 | 33 | 34 | 35 | class Solution { 36 | public: 37 | int numTrees(int n) { 38 | int ans = 0; 39 | int *dp = (int *)malloc(sizeof(int) * (n+1)); 40 | 41 | memset(dp,0,sizeof(int) * (n+1)); 42 | dp[0] = dp[1] = 1; 43 | 44 | for(int i = 2; i <= n; i++){ 45 | for( int root = 1; root <= i ; root++){ 46 | dp[i] = dp[i] + (dp[root-1] * dp[i-root]); 47 | } 48 | } 49 | 50 | ans = dp[n]; 51 | free(dp); 52 | return ans ; 53 | } 54 | }; 55 | 56 | 57 | int main(){ 58 | Solution solve; 59 | 60 | for(int i=1;i<20;i++){ 61 | printf("%d = %d\n",i,solve.numTrees(i)); 62 | } 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /00098. Validate Binary Search Tree .cpp: -------------------------------------------------------------------------------- 1 | /* 2 | [98. Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) 3 | 4 | 5 | ## 题目大意 6 | 7 | 给定一棵二叉树,判断是否标准平衡二叉树 8 | 9 | 关于平衡树的定义: 10 | https://en.wikipedia.org/wiki/Binary_search_tree 11 | 12 | 13 | 14 | ## 解题思路 15 | 中序遍历 二叉树,得出数组。按照定义该数组应该是 升序的 16 | */ 17 | 18 | 19 | class Solution { 20 | public: 21 | bool isValidBST(TreeNode* root) { 22 | if(!root) return true; 23 | 24 | vector ans; 25 | treeToVector(root,ans); 26 | 27 | bool isVaild = true; 28 | 29 | for(size_t i = 1; i < ans.size();i++){ 30 | if(ans[i] <= ans[i-1]){ 31 | isVaild = false; 32 | break; 33 | } 34 | } 35 | 36 | return isVaild; 37 | } 38 | 39 | private: 40 | void treeToVector(TreeNode *root, vector& ans){ 41 | if(!root) return; 42 | treeToVector(root->left, ans); 43 | ans.push_back(root->val); 44 | treeToVector(root->right, ans); 45 | 46 | } 47 | }; 48 | -------------------------------------------------------------------------------- /00100. Same Tree.java: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 352 ms java 3 | /** 4 | * Definition for binary tree 5 | * public class TreeNode { 6 | * int val; 7 | * TreeNode left; 8 | * TreeNode right; 9 | * TreeNode(int x) { val = x; } 10 | * } 11 | */ 12 | public class Solution { 13 | public boolean isSameTree(TreeNode p, TreeNode q) { 14 | if(p==null && q!=null) return false; 15 | if(p!=null && q==null) return false; 16 | if(p==null && q==null) return true; 17 | if(p.val == q.val) 18 | { 19 | if(isSameTree(p.left,q.left) && isSameTree(p.right,q.right)) return true; 20 | } 21 | return false; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /00101. Symmetric Tree.c: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 4 ms c 3 | 4 | bool compareNode(struct TreeNode *left, struct TreeNode *right){ 5 | if(!left && !right) return true; 6 | if(left && right){ 7 | if(left->val != right->val) return false; 8 | 9 | return compareNode(left->left, right->right) && compareNode(left->right, right->left); 10 | } 11 | 12 | return false; 13 | } 14 | 15 | bool isSymmetric(struct TreeNode* root) { 16 | if(!root) return true; 17 | 18 | return compareNode(root->left,root->right); 19 | } 20 | -------------------------------------------------------------------------------- /00102. Binary Tree Level Order Traversal.java: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 420 ms java 3 | public class Solution { 4 | public ArrayList> levelOrder(TreeNode root) { 5 | ArrayList> ans = new ArrayList>(); 6 | if(root == null) 7 | return ans; 8 | this.level = 0; 9 | dfs_level(root,0); 10 | for(int i=0;i<=level;i++) 11 | { 12 | ans.add(new ArrayList()); 13 | } 14 | dfs(root,ans,0); 15 | return ans; 16 | } 17 | 18 | public void dfs_level(TreeNode rt,int step) 19 | { 20 | if(rt == null) return ; 21 | if(step > this.level) this.level = step; 22 | dfs_level(rt.left,step+1); 23 | dfs_level(rt.right, step+1); 24 | } 25 | 26 | public void dfs(TreeNode rt,ArrayList> ans,int step) 27 | { 28 | if(rt == null) return ; 29 | // for(ArrayList array : ans) 30 | // System.out.println(array); 31 | // System.out.println("\n"); 32 | ans.get(step).add(rt.val); 33 | dfs(rt.left,ans,step+1); 34 | dfs(rt.right,ans,step+1); 35 | } 36 | 37 | private int level; 38 | } 39 | -------------------------------------------------------------------------------- /00103. Binary Tree Zigzag Level Order Traversal.java: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 404 ms java 3 | import java.util.*; 4 | 5 | public class Solution { 6 | public List> zigzagLevelOrder(TreeNode root) { 7 | List> ans = new ArrayList>(); 8 | 9 | if(root == null) return ans; 10 | 11 | this.level = 0; 12 | 13 | dfs_level(root, 0); 14 | 15 | for (int i = 0; i <= this.level; i++) { 16 | ans.add(new ArrayList()); 17 | } 18 | 19 | dfs(root, 0, ans); 20 | 21 | for (int i = 1; i <= level; i += 2) 22 | Collections.reverse(ans.get(i)); 23 | 24 | return ans; 25 | } 26 | 27 | public void dfs_level(TreeNode root, int step) { 28 | if (root == null) 29 | return; 30 | 31 | this.level = Math.max(step, this.level); 32 | 33 | dfs_level(root.left, step + 1); 34 | dfs_level(root.right, step + 1); 35 | } 36 | 37 | public void dfs(TreeNode root, int step, List> ans) { 38 | if (root == null) 39 | return; 40 | 41 | ans.get(step).add(root.val); 42 | 43 | dfs(root.left, step + 1, ans); 44 | dfs(root.right, step + 1, ans); 45 | } 46 | 47 | private int level; 48 | } 49 | -------------------------------------------------------------------------------- /00104. Maximum Depth of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 44 ms cpp 3 | class Solution { 4 | public: 5 | int maxDepth(TreeNode *root) { 6 | return dfs(root,0); 7 | } 8 | 9 | int dfs(TreeNode *node, int val) 10 | { 11 | if(!node) return val; 12 | return max(dfs(node->left,val+1),dfs(node->right,val+1)); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /00105. Construct Binary Tree from Preorder nad inorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 124 ms cpp 3 | class Solution { 4 | public: 5 | TreeNode *buildTree(vector &preorder, vector &inorder) { 6 | if(preorder.size() == 0) return NULL; 7 | 8 | return buildTree2(preorder,0,preorder.size()-1,inorder,0,inorder.size()-1); 9 | } 10 | 11 | TreeNode *buildTree2(vector& preorder,int preBeg,int preEnd,vector& inorder,int inBeg,int inEnd) 12 | { 13 | if(inBeg > inEnd) return NULL; 14 | 15 | int rtVal = preorder[preBeg]; 16 | int index ; 17 | 18 | for(int i=inBeg;i<=inEnd;i++) 19 | { 20 | if(rtVal == inorder[i]) 21 | { 22 | index = i; 23 | break; 24 | } 25 | } 26 | 27 | int len = index - inBeg; 28 | TreeNode *root = new TreeNode(rtVal); 29 | root->left = buildTree2(preorder,preBeg+1,preBeg+len,inorder,inBeg,index-1); 30 | root->right = buildTree2(preorder,preBeg+len+1,preEnd,inorder,index+1,inEnd); 31 | 32 | return root; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /00106. Construct Binary Tree from Inorder and Postorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 116 ms cpp 3 | class Solution { 4 | public: 5 | TreeNode *buildTree(vector &inorder, vector &postorder) { 6 | if(postorder.size() == 0) return NULL; 7 | 8 | return buildTree2(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1); 9 | } 10 | 11 | TreeNode* buildTree2(vector& inorder,int inBeg,int inEnd,vector& postorder,int postBeg,int postEnd) 12 | { 13 | if(postBeg > postEnd) return NULL; 14 | 15 | int rtVal = postorder[postEnd]; 16 | 17 | int index; 18 | for(int i=inBeg;i<=inEnd;i++) 19 | { 20 | if(inorder[i] == rtVal) 21 | { 22 | index = i; 23 | break; 24 | } 25 | } 26 | 27 | int len = index - inBeg; 28 | TreeNode* root = new TreeNode(rtVal); 29 | root->left = buildTree2(inorder,inBeg,index-1,postorder,postBeg,postBeg+len-1); 30 | root->right = buildTree2(inorder,index+1,inEnd,postorder,postBeg+len,postEnd-1); 31 | return root; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /00107. Binary Tree Level Order Traversal II.java: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 444 ms java 3 | public class Solution { 4 | public ArrayList> levelOrderBottom(TreeNode root) { 5 | ArrayList> ans = new ArrayList>(); 6 | if(root == null) 7 | return ans; 8 | this.level = 0; 9 | dfs_level(root,0); 10 | for(int i=0;i<=level;i++) 11 | { 12 | ans.add(new ArrayList()); 13 | } 14 | dfs(root,ans,0); 15 | 16 | ArrayList> res = new ArrayList>(); 17 | int len = ans.size()-1; 18 | for(int i=len;i>-1;i--) 19 | { 20 | res.add(ans.get(i)); 21 | } 22 | return res; 23 | } 24 | 25 | public void dfs_level(TreeNode rt,int step) 26 | { 27 | if(rt == null) return ; 28 | if(step > this.level) this.level = step; 29 | dfs_level(rt.left,step+1); 30 | dfs_level(rt.right, step+1); 31 | } 32 | 33 | public void dfs(TreeNode rt,ArrayList> ans,int step) 34 | { 35 | if(rt == null) return ; 36 | ans.get(step).add(rt.val); 37 | dfs(rt.left,ans,step+1); 38 | dfs(rt.right,ans,step+1); 39 | } 40 | 41 | private int level; 42 | } 43 | -------------------------------------------------------------------------------- /00108. Convert Sorted Array to Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 84 ms cpp 3 | class Solution { 4 | public: 5 | TreeNode *sortedArrayToBST(vector &num) { 6 | if(num.size() == 0) return NULL; 7 | 8 | if(num.size() == 1) 9 | { 10 | TreeNode *root = new TreeNode(num[0]); 11 | return root; 12 | } 13 | 14 | return array2BST(num,0,num.size()-1); 15 | } 16 | 17 | TreeNode* array2BST(vector& num,int i,int j) 18 | { 19 | if(i == j) 20 | { 21 | TreeNode* root = new TreeNode(num[i]); 22 | return root; 23 | } 24 | int mid = i+((j-i)>>1); 25 | TreeNode* root = new TreeNode(num[mid]); 26 | 27 | if(mid != i) 28 | root->left = array2BST(num,i,mid-1); 29 | 30 | root->right = array2BST(num,mid+1,j); 31 | return root; 32 | } 33 | }; 34 | 35 | -------------------------------------------------------------------------------- /00109. Convert Sorted List to Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 100 ms cpp 3 | class Solution { 4 | public: 5 | TreeNode *sortedListToBST(ListNode *head) { 6 | 7 | while(head != NULL) 8 | { 9 | num.push_back(head->val); 10 | head = head->next; 11 | } 12 | 13 | if(num.size() == 0) return NULL; 14 | if(num.size() == 1) return new TreeNode(num[0]); 15 | 16 | return array2BST(num,0,num.size()-1); 17 | 18 | } 19 | 20 | TreeNode* array2BST(vector& num,int i,int j) 21 | { 22 | if(i == j) return new TreeNode(num[i]); 23 | 24 | int mid = i+((j-i)>>1); 25 | TreeNode *root = new TreeNode(num[mid]); 26 | if(mid != i) 27 | root->left = array2BST(num,i,mid-1); 28 | 29 | root->right = array2BST(num,mid+1,j); 30 | 31 | return root; 32 | 33 | } 34 | private: 35 | vector num; 36 | }; 37 | -------------------------------------------------------------------------------- /00110. Balanced Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 496 ms cpp 3 | class Solution { 4 | public: 5 | bool isBalanced(TreeNode *root) { 6 | if(!root) return true; 7 | bool l = isBalanced(root->left); 8 | bool r = isBalanced(root->right); 9 | 10 | int lDeep = getDeep(root->left); 11 | int rDeep = getDeep(root->right); 12 | 13 | return l && r && abs(lDeep-rDeep) < 2; 14 | } 15 | 16 | int getDeep(TreeNode* root) 17 | { 18 | if(!root) return 0; 19 | 20 | int l = getDeep(root->left); 21 | int r = getDeep(root->right); 22 | 23 | return l > r ? l+1 : r+1; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /00111. Minimum Depth of Binary Tree.c: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 4 ms c 3 | void dfs(struct TreeNode* node,int step,int *ans){ 4 | if(!node) return ; 5 | if(!(node->left) && !(node->right)){ 6 | *ans = *ans < step ? *ans : step; 7 | return ; 8 | } 9 | 10 | dfs(node->left,step+1,ans); 11 | dfs(node->right,step+1,ans); 12 | } 13 | 14 | int minDepth(struct TreeNode* root) { 15 | int ans = INT_MAX; 16 | dfs(root,1,&ans); 17 | return ans == INT_MAX ? 0 : ans; 18 | } 19 | -------------------------------------------------------------------------------- /00112. Path Sum.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 14 ms cpp 3 | class Solution { 4 | public: 5 | bool hasPathSum(TreeNode* root, int sum) { 6 | bool flag = false; 7 | 8 | if(root){ 9 | dfs(root,&flag,0,sum); 10 | } 11 | 12 | return flag; 13 | } 14 | 15 | void dfs(TreeNode* root,bool *flag,int tmpSum,int sum){ 16 | if(*flag) return; 17 | 18 | if(!root->left && !root->right){ 19 | if(tmpSum + root->val == sum){ 20 | *flag = true; 21 | return ; 22 | } 23 | } 24 | 25 | if(root->left){ 26 | dfs(root->left,flag,tmpSum + root->val, sum); 27 | } 28 | 29 | if(root->right){ 30 | dfs(root->right,flag,tmpSum + root->val, sum); 31 | } 32 | 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /00113. Path Sum II.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 19 ms cpp 3 | /** 4 | * Definition for a binary tree node. 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector> pathSum(TreeNode* root, int sum) { 15 | vector> ans; 16 | 17 | dfs(root,sum,ans,{}); 18 | 19 | return ans; 20 | } 21 | 22 | void dfs(TreeNode *root,const int sum,vector> &ans,vector tmp){ 23 | if(!root) return; 24 | if(!root->left && !root->right){ 25 | int tmpSum = root->val; 26 | for_each(tmp.begin(), tmp.end(), [&](const int num){ 27 | tmpSum += num; 28 | }); 29 | 30 | if(tmpSum == sum){ 31 | tmp.push_back(root->val); 32 | ans.push_back(tmp); 33 | } 34 | 35 | return; 36 | } 37 | 38 | 39 | if(root->left){ 40 | tmp.push_back(root->val); 41 | dfs(root->left,sum,ans,tmp); 42 | tmp.pop_back(); 43 | } 44 | if(root->right){ 45 | tmp.push_back(root->val); 46 | dfs(root->right,sum,ans,tmp); 47 | tmp.pop_back(); 48 | } 49 | } 50 | }; 51 | -------------------------------------------------------------------------------- /00118. Pascal's Triangle.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 0 ms cpp 3 | class Solution { 4 | public: 5 | vector> generate(int numRows) { 6 | vector> ans; 7 | 8 | if(numRows >= 1) ans.push_back(vector{1}); 9 | 10 | if(numRows >= 2) ans.push_back(vector{1,1}); 11 | 12 | for(int i = 3; i <= numRows; i++){ 13 | vector tmp(i,0); 14 | 15 | for(int j=1;j<=i;j++){ 16 | if(j == 1 || j == i) tmp[j-1] = 1; 17 | else tmp[j-1] = ans[i-2][j-2] + ans[i-2][j-1]; 18 | } 19 | 20 | ans.push_back(tmp); 21 | } 22 | 23 | 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /00119. Pascal's Triangle II.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 28 ms cpp 3 | class Solution { 4 | public: 5 | int gcd(int a,int b) 6 | { 7 | return (b>0)?gcd(b,a%b):a; 8 | } 9 | vector getRow(int rowIndex) { 10 | vector ans(rowIndex+1); 11 | n = rowIndex; 12 | int limit = (rowIndex+2)>>1,tmp,tmp1,tmp2; 13 | ans[0] = 1; 14 | tmp = tmp1 = tmp2 = 1; 15 | for(int i = 1;i tmp1) n = (n/tmp)*(rowIndex-i),tmp2 = tmp; 22 | else n = n*(rowIndex-i)/tmp1,tmp2 = tmp1; 23 | // printf("%I64d %d %d %d\n",n,tmp,tmp1,ans[i]); 24 | } 25 | for(int i = limit,j=(rowIndex&1?limit-1:limit-2);i > &triangle) { 9 | _n = (int)triangle.size(); 10 | _m = (int)triangle[_n-1].size(); 11 | 12 | int star = 2; 13 | for(int i = _n-star;i>-1;i--,star++) 14 | { 15 | for(int j=0;j<_m - star + 1;j++) 16 | { 17 | triangle[i][j] = min(triangle[i][j] + triangle[i+1][j],triangle[i][j] + triangle[i+1][j+1]); 18 | } 19 | } 20 | 21 | return triangle[0][0]; 22 | } 23 | 24 | }; 25 | 26 | -------------------------------------------------------------------------------- /00121. Best Time to Buy and Sell Stock.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 48 ms cpp 3 | class Solution { 4 | public: 5 | int maxProfit(vector &prices) { 6 | int len = prices.size(); 7 | if(len < 1) return 0; 8 | int minx = prices[0]; 9 | int ans = 0; 10 | for(int i=1;i prices[i-1]) ans += prices[i]-prices[i-1]; 13 | } 14 | 15 | return ans; 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /00123. Best Time to Buy and Sell Stock III.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 28 ms cpp 3 | class Solution { 4 | public: 5 | int maxProfit(vector &prices) { 6 | if(prices.size() < 2) return 0; 7 | 8 | vector left(prices.size(),0); 9 | vector right(prices.size(),0); 10 | 11 | int n = prices.size(); 12 | int tmpmin = prices[0]; 13 | for(int i = 1;i < n;i++) 14 | { 15 | tmpmin = min(tmpmin,prices[i]); 16 | left[i] = max(left[i-1],prices[i]-tmpmin); 17 | } 18 | 19 | int tmpmax = prices[n-1]; 20 | for(int i = n-2;i >= 0;i--) 21 | { 22 | tmpmax = max(tmpmax,prices[i]); 23 | right[i] = max(right[i+1],tmpmax-prices[i]); 24 | } 25 | 26 | int ans = 0; 27 | 28 | for(int i = 0;ileft); 22 | int right = dfs(root->right); 23 | left = left>0?left:0; 24 | right = right>0?right:0; 25 | ans = ans>(left+right+root->val)?ans:(left+right+root->val); 26 | 27 | return root->val + (left>right?left:right); 28 | } 29 | -------------------------------------------------------------------------------- /00125. Valid Palindrome.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 60 ms cpp 3 | class Solution { 4 | public: 5 | bool isPalindrome(string s) { 6 | // IMPORTANT: Please reset any member data you declared, as 7 | // the same Solution instance will be reused for each test case. 8 | string after; 9 | bool flag = false; 10 | after.clear(); 11 | int len = s.length(); 12 | for(int i=0;i='0'&&s[i]<='9') after+=s[i]; 14 | else if(s[i]>='a'&&s[i]<='z') after+=s[i]; 15 | else if(s[i]>='A'&&s[i]<='Z') after+=(s[i]-'A'+'a'); 16 | else if(s[i]!=' ') flag = true; 17 | if(!after.size() && !flag) return true; 18 | len = after.length(); 19 | int p = 0,q = len-1; 20 | while(p <= q) 21 | { 22 | if(after[p] == after[q]) p++,q--; 23 | else return false; 24 | } 25 | return true; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /00127. Word Ladder.java: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 1196 ms java 3 | 4 | public class Solution { 5 | public int ladderLength(String start, String end, HashSet dict) { 6 | dict.add(start); 7 | dict.add(end); 8 | Pretreatment(dict); 9 | Queue Q = new LinkedList(); 10 | Q.add(new node(1,start)); 11 | vis[M.get(start)] = true; 12 | while(!Q.isEmpty()) 13 | { 14 | node now = Q.poll(); 15 | for(int i=0;i dict) 35 | { 36 | M = new HashMap(); 37 | n = 1; 38 | for(String str : dict) 39 | { 40 | M.put(str, n); 41 | n++; 42 | } 43 | vis = new boolean[n]; 44 | } 45 | 46 | class node { 47 | public node(int s, String b) { 48 | step = s; 49 | buf = b; 50 | } 51 | 52 | int step; 53 | String buf; 54 | } 55 | 56 | private Map M; 57 | private boolean vis[]; 58 | private int n; 59 | } 60 | 61 | -------------------------------------------------------------------------------- /00128. Longest Consecutive Sequence.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 12 ms cpp 3 | #define MAX(A,B) (A>B?A:B) 4 | #define MIN(A,B) (A& nums) { 9 | int ans = 0; 10 | 11 | sort(nums.begin(), nums.end(), less()); 12 | 13 | nums.erase(unique(nums.begin(), nums.end()),nums.end()); 14 | 15 | int i,j,len; 16 | 17 | i = 0; 18 | j = len = i+1; 19 | 20 | ans = MIN(1,nums.size()); 21 | 22 | while(j < nums.size()){ 23 | if(nums[j] - nums[i] == len){ 24 | j++,len++; 25 | ans = MAX(ans, len); 26 | } 27 | else{ 28 | i = j++; 29 | len = 1; 30 | ans = MAX(ans, len); 31 | } 32 | } 33 | 34 | return ans; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /00129. Sum Root to Leaf Numbers.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 4 ms cpp 3 | class Solution { 4 | public: 5 | int sumNumbers(TreeNode* root) { 6 | int ans = 0; 7 | vector nums; 8 | if(root){ 9 | dfs(root,"0",nums); 10 | } 11 | 12 | for_each(nums.begin(),nums.end(),[&](const string str){ 13 | ans += atoi(str.c_str()); 14 | }); 15 | 16 | return ans; 17 | 18 | } 19 | 20 | void dfs(TreeNode* root,string str,vector& nums){ 21 | 22 | if(!root->left && !root->right){ 23 | nums.push_back(string(str + ( to_string(root->val) ))); 24 | return ; 25 | } 26 | 27 | if(root->left){ 28 | dfs(root->left,str + ( to_string(root->val) ),nums); 29 | } 30 | 31 | if(root->right){ 32 | dfs(root->right,str + ( to_string(root->val) ),nums); 33 | } 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /00134. Gas Station.c: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 3 ms c 3 | int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize) { 4 | int totalGas = 0, totalCosts = 0; 5 | int maxCostIdx = -1, maxCost = -1; 6 | for(int i=0;i maxCost){ 9 | maxCostIdx = i; 10 | maxCost = cost[i]; 11 | } 12 | } 13 | 14 | for(int i=0;i= cost[tmp]) { 25 | tmp--; 26 | if(tmp == -1) tmp = gasSize - 1; 27 | if(tmp == maxCostIdx) break; 28 | 29 | 30 | tmpMaxgas += gas[tmp]; 31 | tmpMaxCost += cost[tmp]; 32 | } 33 | 34 | 35 | return (tmp+1)%gasSize; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /00135. Candy.java: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 376 ms java 3 | public class Solution { 4 | public int candy(int[] ratings) { 5 | int ans = 0; 6 | 7 | if(ratings.length == 1) return 1; 8 | 9 | int []num = new int[ratings.length]; 10 | int len = ratings.length; 11 | int candy = 1; 12 | for(int i=1;i ratings[i-1]) 15 | num[i] = Math.max(candy++,num[i]); 16 | else 17 | candy = 1; 18 | } 19 | 20 | candy = 1; 21 | for(int i = len-2;i>-1;i--) 22 | { 23 | if(ratings[i] > ratings[i+1]) 24 | num[i] = Math.max(candy++,num[i]); 25 | else 26 | candy = 1; 27 | } 28 | 29 | for(int i=0;i& nums) { 6 | int tmp = 0; 7 | 8 | for_each(nums.begin(), nums.end(), [&](const int num){ 9 | tmp = tmp^num; 10 | }); 11 | 12 | return tmp; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /00139. Word Break.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 12 ms cpp 3 | class Solution { 4 | private: 5 | unordered_set _dict; 6 | vector _match; 7 | int _len; 8 | public: 9 | bool wordBreak(string s, unordered_set &dict) { 10 | _dict = dict; 11 | _match = vector(s.length()+1,false); 12 | _len = (int)s.length(); 13 | _match[0] = true; 14 | for(int i=0;i<_len;i++) 15 | { 16 | if(_match[i]) 17 | { 18 | for(int j=1;j+i < _len+1;j++) 19 | { 20 | if(_dict.find(s.substr(i,j)) != _dict.end()) 21 | _match[i+j] = true; 22 | } 23 | } 24 | } 25 | 26 | return _match[s.length()]; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /00140. Word Break II.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 76 ms cpp 3 | class Solution { 4 | private: 5 | unordered_set _dict; 6 | vector _match; 7 | vector _ans; 8 | int _len; 9 | public: 10 | vector wordBreak(string s, unordered_set &dict) { 11 | _dict = dict; 12 | _match = vector(s.length()+1,false); 13 | _ans = vector(); 14 | _len = (int)s.length(); 15 | _match[0] = true; 16 | for(int i=0;i<_len;i++) 17 | { 18 | if(_match[i]) 19 | { 20 | for(int j=1;j+i < _len+1;j++) 21 | { 22 | if(_dict.find(s.substr(i,j)) != _dict.end()) 23 | _match[i+j] = true; 24 | } 25 | } 26 | } 27 | if(_match[s.length()]) 28 | matchTheWord(s, 0,""); 29 | return _ans; 30 | 31 | } 32 | 33 | void matchTheWord(string& s,int index,string str) 34 | { 35 | for(int i=1;index + inext; 11 | if(!s) return false; 12 | q = q->next->next; 13 | 14 | 15 | while(s && q) 16 | { 17 | if(s == q) return true; 18 | s = s->next; 19 | if(q->next == NULL) return false; 20 | q = q->next->next; 21 | } 22 | return false; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /00142. Linked List Cycle II.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 128 ms cpp 3 | class Solution { 4 | public: 5 | bool hasCycle(ListNode *head) { 6 | if(!head) return false; 7 | 8 | ListNode *s = head; 9 | ListNode *q = head; 10 | s = s->next; 11 | if(!s) return false; 12 | q = q->next->next; 13 | 14 | 15 | while(s && q) 16 | { 17 | if(s == q) return true; 18 | s = s->next; 19 | if(q->next == NULL) return false; 20 | q = q->next->next; 21 | } 22 | return false; 23 | } 24 | 25 | ListNode *detectCycle(ListNode *head) { 26 | if(!hasCycle(head)) return NULL; 27 | 28 | set unionSet; 29 | ListNode *tmp = head; 30 | unionSet.insert(tmp); 31 | tmp = tmp->next; 32 | while(!unionSet.count(tmp)) 33 | { 34 | unionSet.insert(tmp); 35 | tmp = tmp->next; 36 | } 37 | return tmp; 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /00143. Reorder List.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 280 ms cpp 3 | class Solution { 4 | private: 5 | vector listNodeVector; 6 | public: 7 | Solution() 8 | { 9 | listNodeVector.clear(); 10 | } 11 | void reorderList(ListNode *head) { 12 | ListNode *tmp = head; 13 | while(tmp) 14 | { 15 | listNodeVector.push_back(tmp); 16 | ListNode* fix = tmp; 17 | tmp = tmp->next; 18 | fix->next = NULL; 19 | } 20 | 21 | // 只有一个节点,无需翻转 22 | if(listNodeVector.size() < 2) return; 23 | 24 | int end = (int)listNodeVector.size() - 1; 25 | int star = 0; 26 | head = listNodeVector[star]; 27 | tmp = head; 28 | while(star < end) 29 | { 30 | tmp->next = listNodeVector[end]; 31 | tmp = listNodeVector[end]; 32 | star++; 33 | if(star == end) break; 34 | tmp->next = listNodeVector[star]; 35 | tmp = listNodeVector[star]; 36 | end--; 37 | if(end == star) break; 38 | } 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /00144. Binary Tree Preorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 8 ms cpp 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector preorderTraversal(TreeNode *root) { 15 | vector ans; 16 | 17 | dfs(root,ans); 18 | 19 | return ans; 20 | } 21 | 22 | void dfs(TreeNode* root,vector& ans) 23 | { 24 | if(root == NULL) return ; 25 | ans.push_back(root->val); 26 | dfs(root->left,ans); 27 | dfs(root->right,ans); 28 | 29 | } 30 | }; 31 | 32 | -------------------------------------------------------------------------------- /00145. Binary Tree Postorder Traversal.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 8 ms cpp 3 | /** 4 | * Definition for binary tree 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector postorderTraversal(TreeNode *root) { 15 | vector ans; 16 | 17 | dfs(root,ans); 18 | 19 | return ans; 20 | } 21 | 22 | void dfs(TreeNode* root,vector& ans) 23 | { 24 | if(root == NULL) return ; 25 | dfs(root->left,ans); 26 | dfs(root->right,ans); 27 | ans.push_back(root->val); 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /00146. LRU Cache.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 360 ms cpp 3 | class LRUCache{ 4 | public: 5 | LRUCache(int capacity) { 6 | _len = capacity; 7 | } 8 | 9 | int get(int key) { 10 | _mit = M.find(key); 11 | if(_mit != M.end()) 12 | { 13 | L.splice(L.begin(), L, _mit->second); 14 | return _mit->second->second; 15 | } 16 | return -1; 17 | } 18 | 19 | void set(int key, int value) { 20 | _mit = M.find(key); 21 | if(_mit != M.end()) 22 | { 23 | L.splice(L.begin(), L, _mit->second); 24 | L.front().second = value; 25 | return ; 26 | } 27 | if(M.size() == _len) 28 | { 29 | M.erase(L.back().first); 30 | L.pop_back(); 31 | } 32 | L.push_front(make_pair(key,value)); 33 | M[key] = L.begin(); 34 | } 35 | private: 36 | list >L; 37 | map >::iterator> M; 38 | map >::iterator>::iterator _mit; 39 | int _len; 40 | }; 41 | -------------------------------------------------------------------------------- /00147. Insertion Sort List.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 204 ms cpp 3 | class Solution { 4 | public: 5 | ListNode *insertionSortList(ListNode *head) { 6 | // IMPORTANT: Please reset any member data you declared, as 7 | // the same Solution instance will be reused for each test case. 8 | if(!head) return NULL; 9 | ListNode* p = head->next; 10 | head->next = NULL; 11 | if(!p) return head; 12 | ListNode* s; 13 | while(p) 14 | { 15 | s = head; 16 | if(p->val < s->val) 17 | { 18 | head = p; 19 | p = p->next; 20 | head->next = s; 21 | continue; 22 | } 23 | while(s->next && s->next->val < p->val) s = s->next; 24 | if(s->next == NULL) 25 | { 26 | s->next = p; 27 | p = p->next; 28 | s->next->next = NULL; 29 | } 30 | else 31 | { 32 | ListNode* tmp = p->next; 33 | p->next = s->next; 34 | s->next = p; 35 | p = tmp; 36 | } 37 | } 38 | return head; 39 | } 40 | }; 41 | 42 | -------------------------------------------------------------------------------- /00148. Sort List.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 256 ms cpp 3 | class Solution { 4 | public: 5 | ListNode *mergeList(ListNode *head1,ListNode *head2) 6 | { 7 | ListNode* tmp; 8 | if(!head1) return head2; 9 | if(!head2) return head1; 10 | if(head1->val < head2->val) 11 | { 12 | tmp = head1; 13 | head1 = head1->next; 14 | } 15 | else 16 | { 17 | tmp = head2; 18 | head2 = head2->next; 19 | } 20 | tmp->next = mergeList(head1,head2); 21 | return tmp; 22 | } 23 | ListNode *sortList(ListNode *head) 24 | { 25 | if(!head) return NULL; 26 | ListNode *quick,*low,*ans; 27 | ans = quick = low = head; 28 | while(quick->next && quick->next->next) 29 | { 30 | quick = quick->next->next; 31 | low = low->next; 32 | } 33 | if(low->next == NULL) return ans; 34 | quick = low->next; 35 | low->next = NULL; 36 | low = head; 37 | ListNode* l = sortList(low); 38 | ListNode* q = sortList(quick); 39 | ans = mergeList(l,q); 40 | return ans; 41 | } 42 | }; 43 | 44 | -------------------------------------------------------------------------------- /00149. Max Points on a Line.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Accepted 104 ms cpp 3 | class Solution { 4 | public: 5 | int maxPoints(vector &points) { 6 | map kmap; 7 | map::iterator it; 8 | int size = points.size(); 9 | if(size < 3) return size; 10 | int ans = 2; 11 | for(int i=0;isecond > yy) yy = it->second; 37 | yy+=same; 38 | if(yy > ans) ans = yy; 39 | } 40 | return ans; 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /00150. Evaluate Reverse Polish Notation.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Accepted 20 ms cpp 3 | class Solution { 4 | public: 5 | int evalRPN(vector &tokens) { 6 | // IMPORTANT: Please reset any member data you declared, as 7 | // the same Solution instance will be reused for each test case. 8 | stack st; 9 | int len = tokens.size(); 10 | for(int i=0;i 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | /** 32 | 33 | 34 | 152. Maximum Product Subarray 35 | 36 | Find the contiguous subarray within an array (containing at least one number) which has the largest product. 37 | 38 | For example, given the array [2,3,-2,4], 39 | the contiguous subarray [2,3] has the largest product = 6. 40 | 41 | Subscribe to see which companies asked this question. 42 | 43 | */ 44 | 45 | 46 | /* 47 | 48 | 49 | 183 / 183 test cases passed. 50 | Status: Accepted 51 | Runtime: 6 ms 52 | 53 | */ 54 | 55 | 56 | class Solution { 57 | public: 58 | int maxProduct(vector& nums) { 59 | int maxVal,minVal,ans; 60 | int tmpMaxVal,tmpMinVal; 61 | ans = maxVal = minVal = nums[0]; 62 | for(int i=1;i 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | /** 32 | 33 | 153. Find Minimum in Rotated Sorted Array 34 | 35 | Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. 36 | 37 | (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 38 | 39 | Find the minimum element. 40 | 41 | You may assume no duplicate exists in the array. 42 | 43 | */ 44 | 45 | 46 | /* 47 | 48 | 49 | 146 / 146 test cases passed. 50 | Status: Accepted 51 | Runtime: 3 ms 52 | 53 | */ 54 | 55 | 56 | class Solution { 57 | public: 58 | int findMin(vector &num) { 59 | int ans = num[0]; 60 | 61 | for(int i=1;i 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | /** 32 | 33 | 154. Find Minimum in Rotated Sorted Array II 34 | 35 | Follow up for "Find Minimum in Rotated Sorted Array": 36 | What if duplicates are allowed? 37 | 38 | Would this affect the run-time complexity? How and why? 39 | Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. 40 | 41 | (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 42 | 43 | Find the minimum element. 44 | 45 | The array may contain duplicates. 46 | */ 47 | 48 | 49 | /* 50 | 192 / 192 test cases passed. 51 | Status: Accepted 52 | Runtime: 12 ms 53 | */ 54 | 55 | 56 | class Solution { 57 | public: 58 | int findMin(vector &num) { 59 | int ans = num[0]; 60 | for(int i=1;i 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | /** 32 | 33 | 164. Maximum Gap 34 | 35 | Given an unsorted array, find the maximum difference between the successive elements in its sorted form. 36 | 37 | Try to solve it in linear time/space. 38 | 39 | Return 0 if the array contains less than 2 elements. 40 | 41 | You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range. 42 | 43 | */ 44 | 45 | 46 | /* 47 | 18 / 18 test cases passed. 48 | Status: Accepted 49 | Runtime: 6 ms 50 | */ 51 | 52 | 53 | 54 | class Solution { 55 | public: 56 | int maximumGap(vector &num) { 57 | if(num.size() < 2) return 0; 58 | sort(num.begin(), num.end(), [&](const int a, const int b){ 59 | return a < b; 60 | }); 61 | int ans = 0; 62 | int now = num[0]; 63 | for_each(num.begin(), num.end(), [&](int number){ 64 | ans = max(ans,number-now); 65 | now = number; 66 | }); 67 | return ans; 68 | } 69 | }; 70 | 71 | 72 | int main(){ 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /00167. Two Sum II.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // LeetCodeWithSwift 4 | // 5 | // Created by miaoyou.gmy on 2016/12/26. 6 | // Copyright © 2016年 miaoyou.gmy. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | /** 12 | 13 | 167. Two Sum II - Input array is sorted 14 | 15 | Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. 16 | 17 | The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based. 18 | 19 | You may assume that each input would have exactly one solution and you may not use the same element twice. 20 | 21 | Input: numbers={2, 7, 11, 15}, target=9 22 | Output: index1=1, index2=2 23 | 24 | */ 25 | 26 | 27 | /* 28 | 16 / 16 test cases passed. 29 | Status: Accepted 30 | Runtime: 23 ms 31 | */ 32 | 33 | class Solution { 34 | func twoSum(_ numbers: [Int], _ target: Int) -> [Int] { 35 | var index1 = 0,index2 = numbers.count - 1; 36 | var sum = numbers[index1] + numbers[index2]; 37 | 38 | 39 | while sum != target { 40 | if(sum > target){ 41 | index2 -= 1; 42 | } else{ 43 | index1 += 1; 44 | } 45 | sum = numbers[index1] + numbers[index2]; 46 | } 47 | 48 | return [index1+1,index2+1]; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /00168. Excel Sheet Column Title.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // leetCode 4 | // 5 | // Created by miaoyou.gmy 6 | // Copyright © 2016年 miaoyou.gmy. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | /** 32 | 33 | 168. Excel Sheet Column Title 34 | 35 | Given a positive integer, return its corresponding column title as appear in an Excel sheet. 36 | 37 | For example: 38 | 39 | 1 -> A 40 | 2 -> B 41 | 3 -> C 42 | ... 43 | 26 -> Z 44 | 27 -> AA 45 | 28 -> AB 46 | 47 | 48 | */ 49 | 50 | /* 51 | 52 | 模拟 26进制 53 | 54 | 18 / 18 test cases passed. 55 | Status: Accepted 56 | Runtime: 0 ms 57 | 58 | */ 59 | 60 | 61 | class Solution { 62 | public: 63 | string convertToTitle(int n) { 64 | string str = ""; 65 | 66 | while(n){ 67 | str += ('A' + (n-1)%26); 68 | n = (n-1)/26; 69 | } 70 | 71 | reverse(str.begin(), str.end()); 72 | return str; 73 | } 74 | }; 75 | 76 | int main(){ 77 | 78 | Solution solve; 79 | for(int i = 1; i<40;i++){ 80 | cout< 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | /** 32 | 33 | 169. Majority Element 34 | 35 | Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. 36 | 37 | You may assume that the array is non-empty and the majority element always exist in the array. 38 | 39 | */ 40 | 41 | 42 | /* 43 | 44 / 44 test cases passed. 44 | Status: Accepted 45 | Runtime: 16 ms 46 | */ 47 | 48 | 49 | class Solution { 50 | public: 51 | int majorityElement(vector& nums) { 52 | int count = 0; 53 | int result = 0; 54 | for_each(nums.begin(),nums.end(),[&](const int num){ 55 | if(!count){ 56 | result = num; 57 | count++; 58 | } 59 | else if(num != result){ 60 | count--; 61 | } 62 | else{ 63 | count++; 64 | } 65 | }); 66 | return result; 67 | } 68 | }; 69 | 70 | int main(){ 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /00171. Excel Sheet Column Number.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // leetCode 4 | // 5 | // Created by miaoyou.gmy 6 | // Copyright © 2016年 miaoyou.gmy. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | /** 32 | 33 | 171. Excel Sheet Column Number 34 | 35 | Related to question Excel Sheet Column Title 36 | 37 | Given a column title as appear in an Excel sheet, return its corresponding column number. 38 | 39 | For example: 40 | 41 | A -> 1 42 | B -> 2 43 | C -> 3 44 | ... 45 | Z -> 26 46 | AA -> 27 47 | AB -> 28 48 | 49 | */ 50 | 51 | 52 | /* 53 | 1000 / 1000 test cases passed. 54 | Status: Accepted 55 | Runtime: 3 ms 56 | */ 57 | 58 | 59 | int titleToNumber(char* s) { 60 | int ans = 0; 61 | int len = (int)strlen(s); 62 | 63 | for(int i = 0;i 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | /** 32 | 33 | 172. Factorial Trailing Zeroes 34 | 35 | Given an integer n, return the number of trailing zeroes in n!. 36 | 37 | Note: Your solution should be in logarithmic time complexity. 38 | 39 | */ 40 | 41 | 42 | /* 43 | 502 / 502 test cases passed. 44 | Status: Accepted 45 | Runtime: 3 ms 46 | */ 47 | 48 | 49 | int trailingZeroes(int n) { 50 | int ans = 0; 51 | while(n){ 52 | ans += (n / 5); 53 | n/=5; 54 | } 55 | return ans; 56 | } 57 | 58 | 59 | int main(){ 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /00174. Dungeon Game.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | [Dungeon Game](https://leetcode.com/problems/dungeon-game/) 3 | 4 | 这题还是比较坑的。我一开始 从(0,0)开始bfs。 5 | 剪枝策略是 `dp[x][y] < minHP` 就更新 `dp[x][y]` 值。 最后发现这个是不对。如果不是重点右下角的话,这种判断 会过滤一些有利的条件。 具体的TestCase 可以看这个: 6 | 7 | `[[0,0,0],[-1,0,0],[2,0,-2]]` 8 | 9 | 答案是 2. 10 | 11 | 12 | 其实,逆向思维 从右下角开始就OK! 13 | 14 | */ 15 | int min(const int a,const int b){ 16 | return a 0 ? 1 : (1 - dungeon[row][col]); 30 | 31 | for(int i=row-1;i>-1;i--){ 32 | dp[i][col] = dp[i+1][col] - dungeon[i][col]; 33 | if(dp[i][col] < 1) dp[i][col] = 1; 34 | } 35 | for(int i=col-1;i>-1;i--){ 36 | dp[row][i] = dp[row][i+1] - dungeon[row][i]; 37 | if(dp[row][i] < 1) dp[row][i] = 1; 38 | } 39 | 40 | 41 | for(int i=row-1;i>-1;i--){ 42 | for(int j=col-1;j>-1;j--){ 43 | dp[i][j] = min(dp[i+1][j],dp[i][j+1]) - dungeon[i][j]; 44 | if(dp[i][j] < 1) dp[i][j] = 1; 45 | } 46 | } 47 | 48 | ans = dp[0][0]; 49 | 50 | for(int i=0;i 11 | #include 12 | #include 13 | 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | 30 | /** 31 | 32 | 189. Rotate Array 33 | 34 | Rotate an array of n elements to the right by k steps. 35 | 36 | For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 37 | 38 | Note: 39 | Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. 40 | 41 | 42 | Hint: 43 | Could you do it in-place with O(1) extra space? 44 | 45 | */ 46 | 47 | /** 48 | 49 | Array 50 | 51 | 33 / 33 test cases passed. 52 | Status: Accepted 53 | Runtime: 269 ms 54 | 55 | */ 56 | 57 | void mySwap(int *a,int *b){ 58 | int c = *a; 59 | *a = *b; 60 | *b = c; 61 | } 62 | 63 | void rotate(int* nums, int numsSize, int k) { 64 | k = k % numsSize; 65 | 66 | int tmp; 67 | while(k--){ 68 | tmp = nums[0]; 69 | nums[0] = nums[numsSize - 1]; 70 | nums[numsSize - 1] = tmp; 71 | for(int i=1;i>=1; 17 | } 18 | return cnt; 19 | } 20 | -------------------------------------------------------------------------------- /00198. House Robber.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 0 ms 4 | Submitted: 1 year, 3 months ago 5 | */ 6 | 7 | int max(const int a,const int b){ 8 | return a > b ? a : b; 9 | } 10 | 11 | int easyRob(int* nums, int numsSize){ 12 | int odd = 0, eve = 0; 13 | 14 | for(int i=0;i>& grid) { 12 | int count = 0; 13 | 14 | int n = (int)grid.size(); 15 | if(!n) return count; 16 | int m = (int)grid[0].size(); 17 | memset(vis, false, sizeof(bool) * 1024 * 1024); 18 | 19 | for(int i=0;i>& grid){ 32 | queue> Q; 33 | Q.push(make_pair(x, y)); 34 | 35 | while(!Q.empty()){ 36 | pair star = Q.front(); 37 | Q.pop(); 38 | vis[star.first][star.second] = true; 39 | for(int i=0;i<4;i++){ 40 | int xx = star.first + dir[i][0]; 41 | int yy = star.second + dir[i][1]; 42 | 43 | if(xx>-1&&xx-1&&yy(); 11 | str = new char[1024]; 12 | } 13 | 14 | ~Solution(){ 15 | delete []str; 16 | delete hashSet; 17 | } 18 | 19 | bool isHappy(int n) { 20 | while(n!=1){ 21 | hashSet->insert(n); 22 | memset(str, '\0', sizeof(char)*1024); 23 | sprintf(str, "%d",n); 24 | int len = (int)strlen(str); 25 | int sum = 0; 26 | for(int i=0;ifind(n) != hashSet->end()){ 32 | return false; 33 | } 34 | } 35 | return true; 36 | } 37 | private: 38 | set *hashSet; 39 | char *str; 40 | }; 41 | -------------------------------------------------------------------------------- /00204. Count Primes.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // LeetCode 4 | // 5 | // Created by 郭妙友 on 17/2/5. 6 | // Copyright © 2017年 miaoyou.gmy. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | /** 14 | 15 | 204. Count Primes 16 | 17 | Total Accepted: 98807 18 | Total Submissions: 377477 19 | Difficulty: Easy 20 | Contributors: Admin 21 | Description: 22 | 23 | Count the number of prime numbers less than a non-negative number, n. 24 | */ 25 | 26 | 27 | /** 28 | 29 | 20 / 20 test cases passed. 30 | Status: Accepted 31 | Runtime: 103 ms 32 | 33 | */ 34 | 35 | class Solution { 36 | public: 37 | int countPrimes(int n) { 38 | int ans = 0; 39 | bool *vis = (bool *)malloc(sizeof(bool) * (n + 1)); 40 | memset(vis, true, sizeof(bool) * (n+1)); 41 | 42 | for(int i=2;inext; 12 | 13 | while(cur && next){ 14 | cur->next = pre; 15 | pre = cur; 16 | 17 | cur = next; 18 | next = cur->next; 19 | } 20 | cur->next = pre; 21 | return cur; 22 | } 23 | -------------------------------------------------------------------------------- /00209. Minimum Size Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 4 ms 4 | Submitted: 1 year, 4 months ago 5 | */ 6 | class Solution { 7 | public: 8 | int minSubArrayLen(int s, vector& nums) { 9 | size_t n = nums.size(); 10 | int sum = 0; 11 | int ans = INT_MAX; 12 | int p = 0,q = 0; 13 | 14 | while(q < n){ 15 | sum += nums[q++]; 16 | 17 | while(sum >= s){ 18 | ans = min(ans,q-p); 19 | sum -= nums[p++]; 20 | } 21 | 22 | } 23 | 24 | return ans==INT_MAX ? 0 : ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /00215. Kth Largest Element in an Array.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 0 ms 4 | Submitted: 2 years, 2 months ago 5 | */ 6 | void swap(int *a, int *b){ 7 | int c = *a; 8 | *a = *b; 9 | *b = c; 10 | } 11 | 12 | void output(int *nums,int p,int q){ 13 | for(int i=p;i k){ 43 | return findKthElement(nums, startIndex, idx,k); 44 | } 45 | return nums[idx]; 46 | } 47 | 48 | int findKthLargest(int* nums, int numsSize, int k) { 49 | return findKthElement(nums,0,numsSize,numsSize-k+1); 50 | } 51 | -------------------------------------------------------------------------------- /00216. Combination Sum III.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 2 ms 4 | Submitted: 1 year, 2 months ago 5 | */ 6 | class Solution { 7 | public: 8 | vector> combinationSum3(int k, int n) { 9 | vector> ans; 10 | vector tmpVct; 11 | dfs(1,0,0,tmpVct,ans,k,n); 12 | 13 | return ans; 14 | } 15 | private: 16 | void dfs(int starVal,int step,int total,vector& tmpVct,vector>& ans,const int k,const int n){ 17 | if(step < k && starVal > n - total) return; 18 | if(step > k) return; 19 | if(step == k && total < n) return; 20 | if(step == k && total == n){ 21 | ans.push_back(tmpVct); 22 | return; 23 | } 24 | 25 | 26 | for(int i=starVal; i<10; ++i){ 27 | tmpVct.push_back(i); 28 | dfs(i+1, step+1, total+i, tmpVct, ans, k, n); 29 | tmpVct.pop_back(); 30 | } 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /00217. Contains Duplicate.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 104 ms 4 | Submitted: 1 year, 6 months ago 5 | */ 6 | class Solution { 7 | public: 8 | bool containsDuplicate(vector& nums) { 9 | map table; 10 | 11 | for(int i=0;i<(int)nums.size();i++){ 12 | if(table[nums[i]]) return true; 13 | table[nums[i]] = 1; 14 | } 15 | 16 | return false; 17 | } 18 | 19 | }; 20 | -------------------------------------------------------------------------------- /00219. Contains Duplicate II.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 80 ms 4 | Submitted: 1 year, 6 months ago 5 | */ 6 | 7 | class Solution { 8 | public: 9 | bool containsNearbyDuplicate(vector& nums, int k) { 10 | if(!k) return false; 11 | int n = (int)nums.size(); 12 | if(k >= n) k = n; 13 | map table; 14 | 15 | for(int i=0;i<=k;i++){ 16 | if(table[nums[i]]) return true; 17 | table[nums[i]] = 1; 18 | } 19 | 20 | for(int i=k+1;i>& matrix) { 11 | if(matrix.size() == 0) return 0; 12 | 13 | int n = matrix.size(), m = matrix[0].size(), ans = 0; 14 | 15 | vector > dp(n+1,vector(m+1,0)); 16 | 17 | for(int i=1;i<=n;i++){ 18 | for(int j=1;j<=m;j++){ 19 | if(matrix[i-1][j-1] == '1'){ 20 | dp[i][j] = min(min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1; 21 | 22 | ans = max(dp[i][j], ans); 23 | } 24 | } 25 | } 26 | 27 | return ans*ans; 28 | } 29 | 30 | }; 31 | -------------------------------------------------------------------------------- /00222. Count Complete Tree Nodes.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // leetcode-whiteboard 4 | // 5 | // Created by miaoyou.gmy 6 | // Copyright © 2017年 miaoyou.gmy. All rights reserved. 7 | // 8 | 9 | 10 | 11 | /** 12 | 13 | 222. Count Complete Tree Nodes 14 | 15 | Given a complete binary tree, count the number of nodes. 16 | 17 | Definition of a complete binary tree from Wikipedia: 18 | In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h. 19 | 20 | */ 21 | 22 | /** 23 | 24 | 18 / 18 test cases passed. 25 | Status: Accepted 26 | Runtime: 169 ms 27 | 28 | */ 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | using namespace std; 36 | 37 | 38 | struct TreeNode { 39 | int val; 40 | TreeNode *left; 41 | TreeNode *right; 42 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 43 | }; 44 | 45 | class Solution { 46 | public: 47 | int countNodes(TreeNode* root) { 48 | if(!root) return 0; 49 | 50 | int leftLevel = 1,rightLevel = 1; 51 | TreeNode *tmp = root; 52 | while(tmp->left){ 53 | ++leftLevel; 54 | tmp = tmp->left; 55 | } 56 | tmp = root; 57 | while(tmp->right){ 58 | ++rightLevel; 59 | tmp = tmp->right; 60 | } 61 | 62 | if(leftLevel == rightLevel) return pow(2,leftLevel) - 1; 63 | 64 | return 1 + countNodes(root->left) + countNodes(root->right); 65 | } 66 | }; 67 | 68 | 69 | int main(int argc, const char * argv[]) { 70 | 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /00223. Rectangle Area.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 24 ms 4 | Submitted: 2 years, 2 months ago 5 | */ 6 | class Solution { 7 | public: 8 | int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 9 | int area = (C-A)*(D-B) + (G-E)*(H-F); 10 | return area - judge(A,C,E,G) * judge(B,D,F,H); 11 | } 12 | 13 | int judge(int x1,int x2,int x3,int x4){ 14 | if(x3 >= x2) return 0; 15 | if(x1 >= x4) return 0; 16 | return min(x2,x4) - max(x1,x3); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /00225. Implement Stack using Queues.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 0 ms 4 | Submitted: 2 years, 2 months ago 5 | */ 6 | 7 | class Stack { 8 | public: 9 | void push(int x) { 10 | size_t cnt = q.size(); 11 | q.push(x); 12 | while (cnt--) { 13 | q.push(q.front()); 14 | q.pop(); 15 | } 16 | } 17 | void pop() { 18 | q.pop(); 19 | } 20 | int top() { 21 | return q.front(); 22 | } 23 | bool empty() { 24 | return q.empty(); 25 | } 26 | private: 27 | queue q; 28 | }; 29 | -------------------------------------------------------------------------------- /00226. Invert Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 0 ms 4 | Submitted: 2 years, 2 months ago 5 | */ 6 | 7 | class Solution { 8 | public: 9 | TreeNode* invertTree(TreeNode* root) { 10 | if(!root) return NULL; 11 | stack S; 12 | S.push(root); 13 | 14 | while(!S.empty()){ 15 | TreeNode *node = S.top(); 16 | S.pop(); 17 | 18 | if(node->left){ 19 | S.push(node->left); 20 | } 21 | if(node->right){ 22 | S.push(node->right); 23 | } 24 | 25 | swap(node->left, node->right); 26 | } 27 | return root; 28 | } 29 | 30 | 31 | void dfs(TreeNode *root,vector& array){ 32 | if(!root) return; 33 | dfs(root->left, array); 34 | array.push_back(root->val); 35 | dfs(root->right, array); 36 | } 37 | 38 | void output(const vector& array){ 39 | cout<<"output array all the elements:"< summaryRanges(vector& nums) { 9 | vector ans; 10 | 11 | int star = -778477,end = -778477; 12 | int len = (int)nums.size(); 13 | 14 | for(int i=0;i"<"< 11 | #include 12 | #include 13 | 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | /** 32 | 33 | 230. Kth Smallest Element in a BST 34 | 35 | Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. 36 | 37 | Note: 38 | You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 39 | 40 | Follow up: 41 | What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine? 42 | 43 | */ 44 | 45 | 46 | /** 47 | 中序遍历,生成有序数组之后,访问下标 k-1 元素 48 | 49 | 50 | 91 / 91 test cases passed. 51 | Status: Accepted 52 | Runtime: 16 ms 53 | */ 54 | 55 | 56 | 57 | struct TreeNode { 58 | int val; 59 | TreeNode *left; 60 | TreeNode *right; 61 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 62 | }; 63 | 64 | 65 | class Solution { 66 | public: 67 | int kthSmallest(TreeNode* root, int k) { 68 | vector nums; 69 | dfs(root,nums); 70 | return nums[k - 1]; 71 | } 72 | void dfs(TreeNode *root,vector& nums){ 73 | if(!root) return ; 74 | 75 | dfs(root->left,nums); 76 | nums.push_back(root->val); 77 | dfs(root->right,nums); 78 | } 79 | 80 | }; 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /00231. Power of Two.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 8 ms 4 | Submitted: 1 year, 3 months ago 5 | */ 6 | 7 | class Solution { 8 | public: 9 | bool isPowerOfTwo(int n) { 10 | if(n < 1) return false; 11 | int cnt=0; 12 | while(n){ 13 | if(n&0x01) cnt++; 14 | n>>=1; 15 | } 16 | return cnt==1; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /00232. Implement Queue using Stacks.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 0 ms 4 | Submitted: 1 year, 3 months ago 5 | */ 6 | class Queue { 7 | public: 8 | // Push element x to the back of queue. 9 | void push(int x) { 10 | s.push(x); 11 | } 12 | 13 | // Removes the element from in front of queue. 14 | void pop(void) { 15 | while(!s.empty()){ 16 | o.push(s.top()); 17 | s.pop(); 18 | } 19 | 20 | o.pop(); 21 | 22 | while(!o.empty()){ 23 | s.push(o.top()); 24 | o.pop(); 25 | } 26 | } 27 | 28 | // Get the front element. 29 | int peek(void) { 30 | while(!s.empty()){ 31 | o.push(s.top()); 32 | s.pop(); 33 | } 34 | 35 | int ans = o.top(); 36 | 37 | while(!o.empty()){ 38 | s.push(o.top()); 39 | o.pop(); 40 | } 41 | 42 | return ans; 43 | } 44 | 45 | // Return whether the queue is empty. 46 | bool empty(void) { 47 | return s.empty(); 48 | } 49 | private: 50 | stack s; 51 | stack o; 52 | }; 53 | -------------------------------------------------------------------------------- /00235. Lowest Common Ancestor of a Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 24 ms 4 | Submitted: 1 year, 3 months ago 5 | */ 6 | struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) { 7 | bool p_onRight = p->val >= root->val; 8 | bool q_onRight = q->val >= root->val; 9 | 10 | if((p_onRight && !q_onRight) || (q_onRight && !p_onRight)){ 11 | return root; 12 | } 13 | 14 | if(!p_onRight && !q_onRight){ 15 | return lowestCommonAncestor(root->left, p, q); 16 | } 17 | 18 | if(p_onRight && q_onRight){ 19 | if(p->val == root->val) return p; 20 | if(q->val == root->val) return q; 21 | return lowestCommonAncestor(root->right, p, q); 22 | } 23 | 24 | return NULL; 25 | } 26 | -------------------------------------------------------------------------------- /00237. Delete Node in a Linked List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 4 ms 4 | Submitted: 1 year, 3 months ago 5 | */ 6 | 7 | void deleteNode(struct ListNode* node) { 8 | struct ListNode* tmp = node->next; 9 | 10 | if(tmp){ 11 | node->val = tmp->val; 12 | node->next = tmp->next; 13 | free(tmp); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /00242. Valid Anagram.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 0 ms 4 | Submitted: 1 year, 10 months ago 5 | */ 6 | bool isAnagram(char* s, char* t) { 7 | int *chars = (int *)malloc(sizeof(int)*27); 8 | int slen = strlen(s),tlen = strlen(t),i,sum; 9 | memset(chars,0,sizeof(int)*27); 10 | for(i=0;i 1) { 15 | ans = 0; 16 | for(int i=0;i 6&=-6 (110)& (010) = (010) 34 | */ 35 | class Solution { 36 | func singleNumber(_ nums: [Int]) -> [Int] { 37 | var ans = [0 , 0] 38 | 39 | var tmp = nums.reduce(0) { $0^$1 } 40 | 41 | tmp &= -tmp 42 | 43 | for num in nums { 44 | if tmp & num == 0 { 45 | ans[0] = ans[0] ^ num 46 | } else { 47 | ans[1] = ans[1] ^ num 48 | } 49 | } 50 | 51 | 52 | return ans 53 | } 54 | } 55 | 56 | -------------------------------------------------------------------------------- /00263. Ugly Number.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 4 ms 4 | Submitted: 1 year, 3 months ago 5 | */ 6 | bool isUgly(int num) { 7 | 8 | if(!num) return false; 9 | 10 | while(!(num%2)) num/=2; 11 | 12 | while(!(num%3)) num/=3; 13 | 14 | while(!(num%5)) num/=5; 15 | 16 | 17 | return num == 1; 18 | } 19 | -------------------------------------------------------------------------------- /00268. Missing Number.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 12 ms 4 | Submitted: 1 year, 5 months ago 5 | */ 6 | int missingNumber(int* nums, int numsSize) { 7 | int sum = (numsSize - 1)*numsSize/2; 8 | int total = 0; 9 | for(int i=0;i 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | 32 | /** 33 | 34 | 35 | 275. H-Index II 36 | 37 | Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? 38 | 39 | 40 | */ 41 | 42 | 43 | /** 44 | 45 | H-index h因子,评价一个学术成就的新方法。 指的是一个至少有n篇论文被引用了n次 46 | 47 | 82 / 82 test cases passed. 48 | Status: Accepted 49 | Runtime: 19 ms 50 | 51 | 52 | */ 53 | 54 | class Solution { 55 | public: 56 | int hIndex(vector& citations) { 57 | int ans = 0; 58 | 59 | int len = (int)citations.size() ; 60 | 61 | for(int i=1,last = len - 1; last > -1; --last,++i){ 62 | if(citations[last] < i){ 63 | break; 64 | } 65 | ++ans; 66 | } 67 | 68 | return ans; 69 | } 70 | }; 71 | 72 | 73 | int main(){ 74 | Solution solve; 75 | vector citations{4,4,4,4,4}; 76 | cout< Q; 35 | Q.push(State(n)); 36 | vector V(n,0xfff3fff); 37 | 38 | while(!Q.empty()){ 39 | State s = Q.front(); 40 | Q.pop(); 41 | 42 | if(!s.num) return s.step; 43 | 44 | int starIdx = sqrt(s.num); 45 | for(int i = starIdx; i>0;i--){ 46 | int tmp = s.num - i*i; 47 | if(V[tmp] > s.step + 1){ 48 | Q.push(State(s.num - i*i,s.step+1)); 49 | V[tmp] = s.step + 1; 50 | } 51 | } 52 | 53 | } 54 | 55 | return 0; 56 | } 57 | 58 | private: 59 | class State{ 60 | public: 61 | int num,step; 62 | State(int _n=0,int _s=0){ 63 | num = _n; 64 | step =_s; 65 | } 66 | }; 67 | 68 | }; 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /00283. Move Zeroes.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 124 ms 4 | Submitted: 1 year, 6 months ago 5 | */ 6 | class Solution { 7 | public: 8 | void moveZeroes(vector& nums) { 9 | int n = (int)nums.size(); 10 | int cnt = 0; 11 | int idx = -1; 12 | for(int i=0;i& nums) { 14 | int slow = 0; 15 | int fast = 0; 16 | do { 17 | slow = nums[slow]; 18 | fast = nums[nums[fast]]; 19 | } while (slow != fast); 20 | 21 | slow = 0; 22 | while (slow != fast) { 23 | slow = nums[slow]; 24 | fast = nums[fast]; 25 | } 26 | 27 | return slow; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /00290. Word Pattern.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 0 ms 4 | Submitted: 1 year, 3 months ago 5 | */ 6 | class Solution { 7 | public: 8 | bool wordPattern(string pattern, string str) { 9 | vector v = spilt(str, " "); 10 | if(pattern.size() != v.size()) return false; 11 | map m; 12 | int mark[256] = {0}; 13 | 14 | for(int i=0;i spilt(const string& s,const string& delim){ 27 | vector v; 28 | 29 | size_t last = 0; 30 | size_t index=s.find_first_of(delim,last); 31 | if(index == string::npos){ 32 | v.push_back(s); 33 | return v; 34 | } 35 | while (index!=std::string::npos) 36 | { 37 | v.push_back(s.substr(last,index-last)); 38 | last=index+1; 39 | index=s.find_first_of(delim,last); 40 | } 41 | if (index-last>0) 42 | { 43 | v.push_back(s.substr(last,index-last)); 44 | } 45 | 46 | return v; 47 | } 48 | }; 49 | -------------------------------------------------------------------------------- /00292. Nim Game.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 0 ms 4 | Submitted: 1 year, 6 months ago 5 | */ 6 | class Solution { 7 | public: 8 | bool canWinNim(int n) { 9 | return n%4 ? true : false; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /00299. Bulls and Cows.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 4 ms 4 | Submitted: 1 year, 3 months ago 5 | */ 6 | const int CHARMAX = 10; 7 | char* getHint(char* secret, char* guess) { 8 | int _aaa[CHARMAX],_bbb[CHARMAX]; 9 | memset(_aaa, 0, sizeof(int)*CHARMAX); 10 | memset(_bbb, 0, sizeof(int)*CHARMAX); 11 | 12 | int bulls = 0; 13 | int cows = 0; 14 | 15 | int len = (int)strlen(secret); 16 | for(int i=0;i j) return 0; 25 | 26 | return numArray[j+1].sumValue - numArray[i+1].sumValue + numArray[i+1].value; 27 | } 28 | 29 | /** Deallocates memory previously allocated for the data structure. */ 30 | void NumArrayFree(struct NumArray* numArray) { 31 | free(numArray); 32 | } 33 | -------------------------------------------------------------------------------- /00315. Count of Smaller Numbers After Self.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | [Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/) 3 | 4 | 5 | 6 | 题目大意:给一个整形数组`nums`,你需要返回一个新的数组`counts`: 其中`counts[i]` 表示位于元素`nums[i]`右侧小于`nums[i]`值的个数 7 | 8 | 解题思路:倒序遍历构造顺序数组 + 二分查找。 手撸二分查找也是可以的,时间复杂度为`NlogN`。STL 中有`lower_bound`直接水过去即可。 9 | 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | vector countSmaller(vector& nums) { 16 | vector ans(nums.size(),0),tmp; 17 | 18 | int n = (int)nums.size(); 19 | 20 | for(int i=n-1;i>-1;i--){ 21 | auto iter = lower_bound(tmp.begin(), tmp.end(), nums[i]); 22 | ans[i] = iter - tmp.begin(); 23 | tmp.insert(iter, nums[i]); 24 | } 25 | 26 | return ans; 27 | } 28 | }; 29 | 30 | 31 | -------------------------------------------------------------------------------- /00326. Power of Three.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 124 ms 4 | Submitted: 1 year, 3 months ago 5 | */ 6 | class Solution { 7 | public: 8 | bool isPowerOfThree(int n) { 9 | if(n < 1) return false; 10 | 11 | while(!(n%3)) n/=3; 12 | 13 | return n == 1; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /00338. Counting Bits.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // LeetCode 4 | // 5 | // Created by 郭妙友 on 17/2/5. 6 | // Copyright © 2017年 miaoyou.gmy. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using namespace std; 16 | 17 | /** 18 | 19 | 338. Counting Bits 20 | Description Submission Solutions Add to List 21 | Total Accepted: 64207 22 | Total Submissions: 107677 23 | Difficulty: Medium 24 | Contributors: Admin 25 | Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. 26 | 27 | Example: 28 | For num = 5 you should return [0,1,1,2,1,2]. 29 | 30 | Follow up: 31 | 32 | It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass? 33 | Space complexity should be O(n). 34 | Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language. 35 | Show Hint 36 | 37 | 38 | */ 39 | 40 | 41 | /** 42 | 43 | 15 / 15 test cases passed. 44 | Status: Accepted 45 | Runtime: 72 ms 46 | 47 | */ 48 | 49 | class Solution { 50 | public: 51 | vector countBits(int num) { 52 | vector ans(num+1,0); 53 | 54 | for(int i=1;i<=num;i++){ 55 | ans[i] = ans[i&(i-1)] + 1; 56 | } 57 | 58 | return ans; 59 | } 60 | }; 61 | 62 | 63 | int main(){ 64 | Solution solve; 65 | vector ans = solve.countBits(25); 66 | 67 | 68 | for_each(ans.begin(), ans.end(), [](int num){ 69 | cout< 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | using namespace std; 42 | 43 | class Solution { 44 | public: 45 | vector topKFrequent(vector& nums, int k) { 46 | vector ans; 47 | 48 | unordered_map cnt; 49 | for(int num : nums){ 50 | ++cnt[num]; 51 | } 52 | 53 | priority_queue> q; 54 | for(auto i : cnt){ 55 | q.push(make_pair(i.second, i.first)); 56 | } 57 | 58 | while(k--){ 59 | ans.push_back(q.top().second); 60 | q.pop(); 61 | } 62 | 63 | return ans; 64 | } 65 | }; 66 | 67 | 68 | int main(int argc, const char * argv[]) { 69 | 70 | Solution solve; 71 | 72 | 73 | vector nums = {1,1,1,2,2,3}; 74 | vector ans = solve.topKFrequent(nums, 1); 75 | 76 | for(int i : ans){ 77 | cout< intersection(vector& nums1, vector& nums2) { 9 | vector ans; 10 | set set1; 11 | set set2; 12 | 13 | for_each(nums1.begin(), nums1.end(), [&](const int num){ 14 | set1.insert(num); 15 | }); 16 | 17 | for_each(nums2.begin(), nums2.end(), [&](const int num){ 18 | set2.insert(num); 19 | }); 20 | 21 | for(set::iterator iter = set1.begin() ; iter != set1.end(); ++iter){ 22 | if(set2.count(*iter)){ 23 | ans.push_back(*iter); 24 | } 25 | } 26 | 27 | return ans; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /00367. Valid Perfect Square.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 0 ms 4 | Submitted: 1 year ago 5 | */ 6 | int mySqrt(int x) { 7 | 8 | 9 | float f = x; 10 | int valInt = *(int *)&f; 11 | valInt = (1 << 29) + (valInt >> 1) - (1 << 2) + 2; // guess the star number 12 | f = *(float *)&valInt; 13 | 14 | double val = f; 15 | val = 0.5*val + 0.5*x / val; 16 | val = 0.5*val + 0.5*x / val; 17 | val = 0.5*val + 0.5*x / val; 18 | val = 0.5*val + 0.5*x / val; 19 | 20 | return val; 21 | } 22 | 23 | 24 | bool isPerfectSquare(int num) { 25 | int tmp = mySqrt(num); 26 | 27 | return tmp*tmp == num; 28 | } 29 | -------------------------------------------------------------------------------- /00371. Sum of Two Integers.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // leetCode 4 | // 5 | // Created by miaoyou.gmy 6 | // Copyright © 2016年 miaoyou.gmy. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | 30 | /** 31 | 32 | 371. Sum of Two Integers 33 | 34 | Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. 35 | 36 | Example: 37 | Given a = 1 and b = 2, return 3. 38 | 39 | Credits: 40 | Special thanks to @fujiaozhu for adding this problem and creating all test cases. 41 | 42 | Subscribe to see which companies asked this question. 43 | 44 | 45 | */ 46 | 47 | 48 | /** 49 | 50 | Bit Manipulation 51 | 52 | 13 / 13 test cases passed. 53 | Status: Accepted 54 | Runtime: 0 ms 55 | 56 | */ 57 | 58 | class Solution { 59 | public: 60 | int getSum(int a, int b) { 61 | return b == 0 ? a : getSum(a^b,(a&b)<<1); 62 | } 63 | }; 64 | 65 | 66 | int main(){ 67 | 68 | 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /00373. Find K Pairs with Smallest Sums.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | [Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/) 3 | 4 | 5 | 重点在于使用优先队列(堆 数据结构),定义 比较函数。 6 | 7 | 时间复杂度是 `O(Klog(N))`,空间复杂度是 `O(N)` 8 | 9 | ``` 10 | K = min(k,nm) 11 | N = min(k,n) 12 | m,n 是题中array的长度。 13 | ``` 14 | 15 | */ 16 | 17 | class Solution { 18 | public: 19 | vector> kSmallestPairs(vector& nums1, vector& nums2, int k) { 20 | vector> ans; 21 | 22 | if(k < 1 || nums1.empty() || nums2.empty()) return ans; 23 | 24 | 25 | auto comp = [&nums1,&nums2](pair a, pair b){ 26 | return nums1[a.first] + nums2[a.second] > nums1[b.first] + nums2[b.second]; 27 | }; 28 | 29 | 30 | priority_queue, vector>, decltype(comp)> minHeap(comp); 31 | 32 | minHeap.emplace(0,0); 33 | while(!minHeap.empty() && k > 0){ 34 | k--; 35 | auto topPair = minHeap.top(); 36 | minHeap.pop(); 37 | ans.push_back(make_pair(nums1[topPair.first], nums2[topPair.second])); 38 | 39 | if(topPair.first + 1 < nums1.size()){ 40 | minHeap.emplace(topPair.first+1,topPair.second); 41 | } 42 | 43 | if(topPair.first == 0 && topPair.second + 1 < nums2.size()){ 44 | minHeap.emplace(topPair.first,topPair.second+1); 45 | } 46 | } 47 | 48 | return ans; 49 | } 50 | }; 51 | 52 | -------------------------------------------------------------------------------- /00374. Guess Number Higher or Lower.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/) 3 | 4 | 裸二分 5 | 6 | // Forward declaration of guess API. 7 | // @param num, your guess 8 | // @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 9 | */ 10 | 11 | 12 | int guess(int num); 13 | 14 | class Solution { 15 | public: 16 | int guessNumber(int n) { 17 | int left = 1,right = n; 18 | int mid = left + ((right-left)>>1); 19 | int ans = guess(mid); 20 | 21 | while(ans){ 22 | if(ans > 0){ 23 | left = mid+1; 24 | } 25 | else{ 26 | right = mid-1; 27 | } 28 | mid = ((right-left)>>1)+left; 29 | ans = guess(mid); 30 | } 31 | 32 | return mid; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /00378. Kth Smallest Element in a Sorted Matrix.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | [ Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/) 3 | 4 | 5 | 6 | 和[373. Find K Pairs with Smallest Sums](https://github.com/778477/LeetCode/blob/master/373.%20Find%20K%20Pairs%20with%20Smallest%20Sums.md)解题思路一致,略费内存! 7 | 8 | */ 9 | 10 | class Solution { 11 | public: 12 | 13 | int kthSmallest(vector>& matrix, int k) { 14 | size_t n = matrix.size(); 15 | vector ans; 16 | 17 | auto comp = [&matrix](pair a, pair b){ 18 | return matrix[a.first][a.second] > matrix[b.first][b.second]; 19 | }; 20 | 21 | 22 | priority_queue, vector>, decltype(comp)> minHeap(comp); 23 | 24 | minHeap.emplace(0,0); 25 | while(k > 0){ 26 | k--; 27 | auto topPair = minHeap.top(); 28 | minHeap.pop(); 29 | ans.push_back(matrix[topPair.first][topPair.second]); 30 | 31 | if(topPair.first + 1 < n){ 32 | minHeap.emplace(topPair.first+1,topPair.second); 33 | } 34 | 35 | if(topPair.first == 0 && topPair.second + 1 < n){ 36 | minHeap.emplace(topPair.first,topPair.second+1); 37 | } 38 | } 39 | 40 | return ans.back(); 41 | } 42 | 43 | 44 | }; 45 | -------------------------------------------------------------------------------- /00387. First Unique Character in a String.js: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | 387. First Unique Character in a String 4 | 5 | Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. 6 | 7 | Examples: 8 | 9 | s = "leetcode" 10 | return 0. 11 | 12 | s = "loveleetcode", 13 | return 2. 14 | 15 | Note: You may assume the string contain only lowercase letters. 16 | 17 | */ 18 | 19 | /** 20 | 104 / 104 test cases passed. 21 | Status: Accepted 22 | Runtime: 242 ms 23 | */ 24 | var firstUniqChar = function(s) { 25 | for(let i = 0; i 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | using namespace std; 19 | 20 | /** 21 | 22 | 389. Find the Difference 23 | 24 | Given two strings s and t which consist of only lowercase letters. 25 | 26 | String t is generated by random shuffling string s and then add one more letter at a random position. 27 | 28 | Find the letter that was added in t. 29 | 30 | Example: 31 | 32 | Input: 33 | s = "abcd" 34 | t = "abcde" 35 | 36 | Output: 37 | e 38 | 39 | Explanation: 40 | 'e' is the letter that was added. 41 | 42 | */ 43 | 44 | 45 | /** 46 | 47 | 54 / 54 test cases passed. 48 | Status: Accepted 49 | Runtime: 16 ms 50 | 51 | */ 52 | 53 | int compare( const void *a, const void *b ){ 54 | char *ch1 = (char *)a,*ch2 = (char *)b; 55 | 56 | return (*ch1) - (*ch2); 57 | } 58 | 59 | 60 | char findTheDifference(char* s, char* t) { 61 | int idx = (int)strlen(t)-1; 62 | qsort(s,strlen(s),sizeof(char),compare); 63 | qsort(t,strlen(t),sizeof(char),compare); 64 | for(int i=0;i 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | using namespace std; 18 | 19 | /** 20 | 21 | 404. Sum of Left Leaves 22 | 23 | Find the sum of all left leaves in a given binary tree. 24 | 25 | Example: 26 | 27 | 3 28 | / \ 29 | 9 20 30 | / \ 31 | 15 7 32 | 33 | There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 34 | 35 | 36 | */ 37 | 38 | 39 | /** 40 | 41 | 102 / 102 test cases passed. 42 | Status: Accepted 43 | Runtime: 3 ms 44 | 45 | */ 46 | 47 | //Definition for a binary tree node. 48 | struct TreeNode { 49 | int val; 50 | TreeNode *left; 51 | TreeNode *right; 52 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 53 | }; 54 | 55 | class Solution { 56 | public: 57 | int sumOfLeftLeaves(TreeNode* root) { 58 | int ans = 0; 59 | dfs(root,&ans,false); 60 | return ans; 61 | } 62 | 63 | void dfs(TreeNode *root, int *ans,bool isLeft){ 64 | if(!root) return ; 65 | if(!root->left && !root->right && isLeft){ 66 | *ans += root->val; 67 | } 68 | 69 | dfs(root->left, ans, true); 70 | dfs(root->right, ans, false); 71 | } 72 | }; 73 | 74 | int main(){ 75 | 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /00453. Minimum Moves to Equal Array Elements.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // leetCode 4 | // 5 | // Created by miaoyou.gmy 6 | // Copyright © 2016年 miaoyou.gmy. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | 30 | /** 31 | 32 | 453. Minimum Moves to Equal Array Elements 33 | 34 | Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. 35 | 36 | Example: 37 | 38 | Input: 39 | [1,2,3] 40 | 41 | Output: 42 | 3 43 | 44 | Explanation: 45 | Only three moves are needed (remember each move increments two elements): 46 | 47 | [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4] 48 | 49 | */ 50 | 51 | /** 52 | 53 | Math 54 | 55 | 84 / 84 test cases passed. 56 | Status: Accepted 57 | Runtime: 56 ms 58 | 59 | */ 60 | 61 | 62 | class Solution { 63 | public: 64 | int minMoves(vector& nums) { 65 | int ans = 0; 66 | int minNum = nums[0]; 67 | for(int num : nums){ 68 | minNum = minNum > num ? num : minNum; 69 | } 70 | 71 | for(int num : nums){ 72 | ans += (num - minNum); 73 | } 74 | 75 | return ans; 76 | } 77 | }; 78 | 79 | int main(){ 80 | return 0; 81 | } 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /00461. Hamming Distance.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // LeetCode 4 | // 5 | // Created by 郭妙友 on 17/2/5. 6 | // Copyright © 2017年 miaoyou.gmy. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | /** 14 | 15 | 461. Hamming Distance 16 | 17 | The Hamming distance between two integers is the number of positions at which the corresponding bits are different. 18 | 19 | Given two integers x and y, calculate the Hamming distance. 20 | 21 | Note: 22 | 0 ≤ x, y < 2^31. 23 | 24 | Example: 25 | 26 | Input: x = 1, y = 4 27 | 28 | Output: 2 29 | 30 | Explanation: 31 | 1 (0 0 0 1) 32 | 4 (0 1 0 0) 33 | ↑ ↑ 34 | 35 | The above arrows point to positions where the corresponding bits are different. 36 | 37 | 38 | */ 39 | 40 | 41 | /** 42 | 43 | 149 / 149 test cases passed. 44 | Status: Accepted 45 | Runtime: 3 ms 46 | 47 | */ 48 | 49 | class Solution { 50 | public: 51 | int hammingDistance(int x, int y) { 52 | int ans = 0; 53 | 54 | while(x || y){ 55 | if( (x&0x01) ^ (y&0x01) ) ans++; 56 | 57 | x>>=1; 58 | y>>=1; 59 | } 60 | 61 | return ans; 62 | } 63 | }; 64 | -------------------------------------------------------------------------------- /00476. Number Complement.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // LeetCode 4 | // 5 | // Created by 郭妙友 on 17/2/5. 6 | // Copyright © 2017年 miaoyou.gmy. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | /** 15 | 16 | 476. Number Complement 17 | 18 | Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. 19 | 20 | Note: 21 | The given integer is guaranteed to fit within the range of a 32-bit signed integer. 22 | You could assume no leading zero bit in the integer’s binary representation. 23 | Example 1: 24 | Input: 5 25 | Output: 2 26 | Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2. 27 | Example 2: 28 | Input: 1 29 | Output: 0 30 | Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0. 31 | 32 | 33 | */ 34 | 35 | 36 | /** 37 | 38 | 149 / 149 test cases passed. 39 | Status: Accepted 40 | Runtime: 3 ms 41 | 42 | */ 43 | 44 | class Solution { 45 | public: 46 | int findComplement(int num) { 47 | int ans = 0; 48 | int bit = 0; 49 | int tmp = 0; 50 | 51 | while(num){ 52 | tmp = !(num&0x01); 53 | ans = ans + tmp * pow(2,bit); 54 | num >>= 1; 55 | bit++; 56 | } 57 | 58 | 59 | return ans; 60 | } 61 | }; 62 | -------------------------------------------------------------------------------- /00485. Max Consecutive Ones.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Status: Accepted 3 | Runtime: 36 ms 4 | Submitted: 6 months, 1 week ago 5 | */ 6 | 7 | class Solution { 8 | public: 9 | int findMaxConsecutiveOnes(vector& nums) { 10 | int ans = 0; 11 | int count = 0; 12 | 13 | for(int i=0;i ans ? count : ans; 16 | count = 0; 17 | } else{ 18 | count++; 19 | } 20 | } 21 | 22 | ans = count > ans ? count : ans; 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /00504. Base 7.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // leetCode 4 | // 5 | // Created by miaoyou.gmy 6 | // Copyright © 2016年 miaoyou.gmy. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | /** 32 | 33 | 504. Base 7 34 | 35 | Given an integer, return its base 7 string representation. 36 | 37 | Example 1: 38 | Input: 100 39 | Output: "202" 40 | 41 | Example 2: 42 | Input: -7 43 | Output: "-10" 44 | 45 | Note: The input will be in range of [-1e7, 1e7]. 46 | */ 47 | 48 | 49 | /** 50 | 51 | 241 / 241 test cases passed. 52 | Status: Accepted 53 | Runtime: 3 ms 54 | 55 | */ 56 | 57 | 58 | 59 | class Solution { 60 | public: 61 | string convertToBase7(int num) { 62 | string ans(""); 63 | transform(abs(num), ans); 64 | reverse(ans.begin(), ans.end()); 65 | if(num < 0) return "-"+ans; 66 | 67 | return ans; 68 | } 69 | 70 | void transform(int num,string& str){ 71 | if(num < BASE){ 72 | str+=to_string(num); 73 | return ; 74 | } 75 | transform(num/7, str+=to_string(num%BASE)); 76 | } 77 | private: 78 | const int BASE = 7; 79 | }; 80 | 81 | 82 | 83 | int main(){ 84 | Solution solve; 85 | 86 | cout<='a' && word[i] <= 'z'){ 12 | isCap = false; 13 | } 14 | } 15 | if(isCap) return true; 16 | 17 | for(int i=1;i='A' && word[i]<='Z'){ 19 | isSam = false; 20 | } 21 | } 22 | 23 | if(isSam) return true; 24 | 25 | 26 | return false; 27 | } 28 | -------------------------------------------------------------------------------- /00540. Single Element in a Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 7 / 7 test cases passed. 3 | Status: Accepted 4 | Runtime: 6 ms 5 | Submitted: 2 weeks, 3 days ago 6 | */ 7 | class Solution { 8 | public: 9 | int singleNonDuplicate(vector& nums) { 10 | int val = 0; 11 | 12 | for(int num : nums){ 13 | val = val^num; 14 | } 15 | 16 | return val; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /00551. Student Attendance Record I.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | 113 / 113 test cases passed. 4 | Status: Accepted 5 | Runtime: 3 ms 6 | Submitted: 3 months, 1 week ago 7 | */ 8 | 9 | class Solution { 10 | public: 11 | bool checkRecord(string s) { 12 | int a,l; 13 | a = l = 0; 14 | 15 | for(char c : s){ 16 | if(c == 'L'){ 17 | ++l; 18 | } else { 19 | l = 0; 20 | } 21 | 22 | if(c == 'A') a++; 23 | 24 | if(a>1 || l>2) return false; 25 | } 26 | 27 | return true; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /00560. Subarray Sum Equals K.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // leetcode-whiteboard 4 | // 5 | // Created by miaoyou.gmy 6 | // Copyright © 2017年 miaoyou.gmy. All rights reserved. 7 | // 8 | 9 | 10 | 11 | /** 12 | 13 | 14 | 560. Subarray Sum Equals K 15 | 16 | Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. 17 | 18 | Example 1: 19 | Input:nums = [1,1,1], k = 2 20 | Output: 2 21 | Note: 22 | The length of the array is in range [1, 20,000]. 23 | The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7]. 24 | 25 | 26 | */ 27 | 28 | /** 29 | 30 | 80 / 80 test cases passed. 31 | Status: Accepted 32 | Runtime: 639 ms 33 | 34 | */ 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | using namespace std; 42 | 43 | 44 | class Solution { 45 | public: 46 | int subarraySum(vector& nums, int k) { 47 | int ans = 0; 48 | 49 | vector sum(nums.size(),0); 50 | 51 | for(size_t i = 0;i nums = {1,0,1}; 66 | 67 | Solution solve; 68 | 69 | cout<& nums){ 10 | if(!root) return; 11 | 12 | dfs(root->left,nums); 13 | nums.push_back(root->val); 14 | dfs(root->right, nums); 15 | } 16 | bool findTarget(TreeNode* root, int k) { 17 | vector nums; 18 | dfs(root,nums); 19 | 20 | if(nums.size() < 1) return false; 21 | unsigned long i = 0, j = nums.size() - 1; 22 | 23 | while(i < j){ 24 | if(nums[i] + nums[j] == k) return true; 25 | else if(nums[i] + nums[j] < k) ++i; 26 | else --j; 27 | } 28 | 29 | return false; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /00820. Short Encoding of Words.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumLengthEncoding(vector& w) { 4 | unordered_set st(w.begin(), w.end()); 5 | 6 | 7 | for(auto && str : w) 8 | for(auto i=1;i 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | using namespace std; 23 | 24 | /** 25 | 26 | 27 | 829. Consecutive Numbers Sum 28 | 29 | Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? 30 | 31 | Example 1: 32 | 33 | Input: 5 34 | Output: 2 35 | Explanation: 5 = 5 = 2 + 3 36 | Example 2: 37 | 38 | Input: 9 39 | Output: 3 40 | Explanation: 9 = 9 = 4 + 5 = 2 + 3 + 4 41 | Example 3: 42 | 43 | Input: 15 44 | Output: 4 45 | Explanation: 15 = 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5 46 | Note: 1 <= N <= 10 ^ 9. 47 | 48 | 49 | */ 50 | 51 | 52 | /** 53 | 54 | 170 / 170 test cases passed. 55 | Status: Accepted 56 | Runtime: 10 ms 57 | 58 | 小学数学题 59 | 60 | */ 61 | 62 | class Solution { 63 | public: 64 | int consecutiveNumbersSum(int N) { 65 | int ans = 1,sum = 0; 66 | 67 | for(int i=2; 1; ++i){ 68 | sum = sum + i - 1; 69 | 70 | if(sum >= N) break; 71 | 72 | if((N-sum) % i == 0){ 73 | ++ans; 74 | } 75 | } 76 | 77 | return ans; 78 | } 79 | }; 80 | 81 | 82 | int main(){ 83 | 84 | 85 | Solution solve; 86 | cout< p){ 4 | return p[0]*p[0] + p[1]*p[1]; 5 | } 6 | 7 | vector> kClosest(vector>& points, int K) { 8 | sort(points.begin(), points.end(), [&](const vector& a, vector& b){ 9 | return dis(a) < dis(b); 10 | }); 11 | 12 | 13 | vector> ans; 14 | for(int i=0;i& A, int K) { 4 | unordered_map cnt; 5 | int ans = 0, sum = 0; 6 | cnt[0] = 1; 7 | for(int a:A){ 8 | sum = (sum + a) % K; 9 | if(sum < 0) sum += K; 10 | ans += cnt[sum]; 11 | ++cnt[sum]; 12 | } 13 | 14 | return ans; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /00975. Odd Even Jump.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution { 4 | public: 5 | int oddEvenJumps(vector& A) { 6 | vector odd = vector(A.size(), false); 7 | vector even = vector(A.size(), false); 8 | int n = A.size() - 1; 9 | odd[n] = even[n] = true; 10 | int ans = 1; 11 | unordered_map mp; 12 | set st; 13 | st.insert(A.back()); 14 | mp[A.back()] = n; 15 | 16 | for(int i=n-1; i>-1; --i){ 17 | 18 | // odd 19 | auto oddIdx = st.lower_bound(A[i]); 20 | if(oddIdx != st.end() && mp.find(*oddIdx) != mp.end()){ 21 | odd[i] = even[mp[*oddIdx]]; 22 | ans = ans + (odd[i] ? 1 : 0); 23 | } 24 | // even 25 | auto evenIdx = st.lower_bound(A[i]); 26 | if(*evenIdx != A[i] && evenIdx != st.begin()){ 27 | evenIdx = prev(evenIdx); 28 | } 29 | if(*evenIdx <= A[i] && mp.find(*evenIdx) != mp.end()){ 30 | even[i] = odd[mp[*evenIdx]]; 31 | } 32 | 33 | if(st.find(A[i]) == st.end()){ 34 | st.insert(A[i]); 35 | } 36 | mp[A[i]] = i; 37 | } 38 | 39 | // for(int i=0;i<=n;++i){ 40 | // cout< c 2 | // max(a+b+c) 3 | class Solution { 4 | public: 5 | int largestPerimeter(vector& A) { 6 | sort(A.begin(),A.end()); 7 | 8 | 9 | for(int i = A.size()-1; i>1; i-=1){ 10 | if(A[i-2] + A[i-1] > A[i]) return A[i-2]+A[i-1]+A[i]; 11 | } 12 | 13 | return 0; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [LeetCode Online Judge Algorithms](https://leetcode.com/problemset/algorithms/) 2 | 3 | 4 | 5 | |Difficulty|Accepted| 6 | | --- | ---: | 7 | |Easy|121| 8 | |Medium|154| 9 | |Hard|46| 10 | 11 | ![progress](http://progressed.io/bar/50) `321/636` 12 | 13 | 代码多数为 C/C++。如果是Java的话,那应该是我在实习的时候用Java练手提交的。还会乱入一些swift,那是因为Apple String标准库支持的方法比较全面。 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /String Search/Leetcode 1044.md: -------------------------------------------------------------------------------- 1 | [1044. Longest Duplicate Substring](https://leetcode.com/problems/longest-duplicate-substring/description/) 2 | 3 | 4 | ## Rabin-Karp 5 | 6 | 7 | `Rabin-Karp`是一种字符串搜索算法,基于哈希在匹配串中查找模式串。对于长度为`n`的匹配串和长度为`m`的模式串来讲,该算法的最优运行时间是`O(n+m)` 8 | 9 | 10 | 11 | ``` 12 | function RabinKarp(string s[1..n], string pattern[1..m]) 13 | hpattern := hash(pattern[1..m]); 14 | for i from 1 to n-m+1 15 | hs := hash(s[i..i+m-1]) 16 | if hs = hpattern 17 | if s[i..i+m-1] = pattern[1..m] 18 | return i 19 | return not found 20 | ``` 21 | 22 | 这里算法的技巧是支持`rolling hash`,这使的下一个hash值可基于上一个hash值计算产生。减少了重复计算量。 23 | 24 | ``` 25 | s[i+1..i+m] = s[i..i+m-1] - s[i] + s[i+m] 26 | ``` 27 | 28 | 29 | 算法的关键在于你的哈希实现,[Rabin_fingerprint](https://en.wikipedia.org/wiki/Rabin_fingerprint)是一种主流的`rolling hash`方法 30 | 31 | 32 | ## Suffix Array 33 | [Suffix Array](https://www.geeksforgeeks.org/%C2%AD%C2%ADkasais-algorithm-for-construction-of-lcp-array-from-suffix-array/) 34 | 35 | > [Ranbin-Karp Algorithm](https://www.geeksforgeeks.org/rabin-karp-algorithm-for-pattern-searching/) 36 | 37 | > [Rabin-Karp Algorithm](https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm) -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CXXFLAGS = -std=c++11 3 | OBJECTS = leetcode.o 4 | 5 | leetcode: $(OBJECTS) 6 | $(CXX) $(CXXFLAGS) $< -o $@ 7 | 8 | %.o: %.cpp 9 | $(CXX) $(CXXFLAGS) -c $^ -o $@ 10 | 11 | clean: 12 | rm $(OBJECTS) leetcode 13 | 14 | format: 15 | clang-format -i -style=google *.cpp -------------------------------------------------------------------------------- /update.sh: -------------------------------------------------------------------------------- 1 | # !/bin/sh 2 | 3 | git add * 4 | git commit -m "add new solver" 5 | git push 6 | say "push success" --------------------------------------------------------------------------------