5 | {{ .Title }} 6 |
7 | {{ partial "docs/post-meta" . }} 8 |9 | {{- .Summary -}} 10 | {{ if .Truncated }} 11 | ... 12 | {{ end }} 13 |
14 |├── LICENSE ├── README.md ├── gotest.sh ├── leetcode ├── 0001.Two-Sum │ ├── 1. Two Sum.go │ ├── 1. Two Sum_test.go │ └── README.md ├── 0002.Add-Two-Numbers │ ├── 2. Add Two Numbers.go │ ├── 2. Add Two Numbers_test.go │ └── README.md ├── 0003.Longest-Substring-Without-Repeating-Characters │ ├── 3. Longest Substring Without Repeating Characters.go │ ├── 3. Longest Substring Without Repeating Characters_test.go │ └── README.md ├── 0004.Median-of-Two-Sorted-Arrays │ ├── 4. Median of Two Sorted Arrays.go │ ├── 4. Median of Two Sorted Arrays_test.go │ └── README.md ├── 0007.Reverse-Integer │ ├── 7. Reverse Integer.go │ ├── 7. Reverse Integer_test.go │ └── README.md ├── 0009.Palindrome-Number │ ├── 9. Palindrome Number.go │ ├── 9. Palindrome Number_test.go │ └── README.md ├── 0011.Container-With-Most-Water │ ├── 11. Container With Most Water.go │ ├── 11. Container With Most Water_test.go │ └── README.md ├── 0013.Roman-to-Integer │ ├── 13. Roman to Integer.go │ ├── 13. Roman to Integer_test.go │ └── README.md ├── 0015.3Sum │ ├── 15. 3Sum.go │ ├── 15. 3Sum_test.go │ └── README.md ├── 0016.3Sum-Closest │ ├── 16. 3Sum Closest.go │ ├── 16. 3Sum Closest_test.go │ └── README.md ├── 0017.Letter-Combinations-of-a-Phone-Number │ ├── 17. Letter Combinations of a Phone Number.go │ ├── 17. Letter Combinations of a Phone Number_test.go │ └── README.md ├── 0018.4Sum │ ├── 18. 4Sum.go │ ├── 18. 4Sum_test.go │ └── README.md ├── 0019.Remove-Nth-Node-From-End-of-List │ ├── 19. Remove Nth Node From End of List.go │ ├── 19. Remove Nth Node From End of List_test.go │ └── README.md ├── 0020.Valid-Parentheses │ ├── 20. Valid Parentheses.go │ ├── 20. Valid Parentheses_test.go │ └── README.md ├── 0021.Merge-Two-Sorted-Lists │ ├── 21. Merge Two Sorted Lists.go │ ├── 21. Merge Two Sorted Lists_test.go │ └── README.md ├── 0022.Generate-Parentheses │ ├── 22. Generate Parentheses.go │ ├── 22. Generate Parentheses_test.go │ └── README.md ├── 0023.Merge-k-Sorted-Lists │ ├── 23. Merge k Sorted Lists.go │ ├── 23. Merge k Sorted Lists_test.go │ └── README.md ├── 0024.Swap-Nodes-in-Pairs │ ├── 24. Swap Nodes in Pairs.go │ ├── 24. Swap Nodes in Pairs_test.go │ └── README.md ├── 0025.Reverse-Nodes-in-k-Group │ ├── 25. Reverse Nodes in k Group.go │ ├── 25. Reverse Nodes in k Group_test.go │ └── README.md ├── 0026.Remove-Duplicates-from-Sorted-Array │ ├── 26. Remove Duplicates from Sorted Array.go │ ├── 26. Remove Duplicates from Sorted Array_test.go │ └── README.md ├── 0027.Remove-Element │ ├── 27. Remove Element.go │ ├── 27. Remove Element_test.go │ └── README.md ├── 0028.Implement-strStr │ ├── 28. Implement strStr().go │ ├── 28. Implement strStr()_test.go │ └── README.md ├── 0029.Divide-Two-Integers │ ├── 29. Divide Two Integers.go │ ├── 29. Divide Two Integers_test.go │ └── README.md ├── 0030.Substring-with-Concatenation-of-All-Words │ ├── 30. Substring with Concatenation of All Words.go │ ├── 30. Substring with Concatenation of All Words_test.go │ └── README.md ├── 0033.Search-in-Rotated-Sorted-Array │ ├── 33. Search in Rotated Sorted Array.go │ ├── 33. Search in Rotated Sorted Array_test.go │ └── README.md ├── 0034.Find-First-and-Last-Position-of-Element-in-Sorted-Array │ ├── 34. Find First and Last Position of Element in Sorted Array.go │ ├── 34. Find First and Last Position of Element in Sorted Array_test.go │ └── README.md ├── 0035.Search-Insert-Position │ ├── 35. Search Insert Position.go │ ├── 35. Search Insert Position_test.go │ └── README.md ├── 0036.Valid-Sudoku │ ├── 36. Valid Sudoku.go │ ├── 36. Valid Sudoku_test.go │ └── README.md ├── 0037.Sudoku-Solver │ ├── 37. Sudoku Solver.go │ ├── 37. Sudoku Solver_test.go │ └── README.md ├── 0039.Combination-Sum │ ├── 39. Combination Sum.go │ ├── 39. Combination Sum_test.go │ └── README.md ├── 0040.Combination-Sum-II │ ├── 40. Combination Sum II.go │ ├── 40. Combination Sum II_test.go │ └── README.md ├── 0041.First-Missing-Positive │ ├── 41. First Missing Positive.go │ ├── 41. First Missing Positive_test.go │ └── README.md ├── 0042.Trapping-Rain-Water │ ├── 42. Trapping Rain Water.go │ ├── 42. Trapping Rain Water_test.go │ └── README.md ├── 0046.Permutations │ ├── 46. Permutations.go │ ├── 46. Permutations_test.go │ └── README.md ├── 0047.Permutations-II │ ├── 47. Permutations II.go │ ├── 47. Permutations II_test.go │ └── README.md ├── 0048.Rotate-Image │ ├── 48. Rotate Image.go │ ├── 48. Rotate Image_test.go │ └── README.md ├── 0049.Group-Anagrams │ ├── 49. Group Anagrams.go │ ├── 49. Group Anagrams_test.go │ └── README.md ├── 0050.Powx-n │ ├── 50. Pow(x, n).go │ ├── 50. Pow(x, n)_test.go │ └── README.md ├── 0051.N-Queens │ ├── 51. N-Queens.go │ ├── 51. N-Queens_test.go │ └── README.md ├── 0052.N-Queens-II │ ├── 52. N-Queens II.go │ ├── 52. N-Queens II_test.go │ └── README.md ├── 0053.Maximum-Subarray │ ├── 53. Maximum Subarray.go │ ├── 53. Maximum Subarray_test.go │ └── README.md ├── 0054.Spiral-Matrix │ ├── 54. Spiral Matrix.go │ ├── 54. Spiral Matrix_test.go │ └── README.md ├── 0055.Jump-Game │ ├── 55. Jump Game.go │ ├── 55. Jump Game_test.go │ └── README.md ├── 0056.Merge-Intervals │ ├── 56. Merge Intervals.go │ ├── 56. Merge Intervals_test.go │ └── README.md ├── 0057.Insert-Interval │ ├── 57. Insert Interval.go │ ├── 57. Insert Interval_test.go │ └── README.md ├── 0059.Spiral-Matrix-II │ ├── 59. Spiral Matrix II.go │ ├── 59. Spiral Matrix II_test.go │ └── README.md ├── 0060.Permutation-Sequence │ ├── 60. Permutation Sequence.go │ ├── 60. Permutation Sequence_test.go │ └── README.md ├── 0061.Rotate-List │ ├── 61. Rotate List.go │ ├── 61. Rotate List_test.go │ └── README.md ├── 0062.Unique-Paths │ ├── 62. Unique Paths.go │ ├── 62. Unique Paths_test.go │ └── README.md ├── 0063.Unique-Paths-II │ ├── 63. Unique Paths II.go │ ├── 63. Unique Paths II_test.go │ └── README.md ├── 0064.Minimum-Path-Sum │ ├── 64. Minimum Path Sum.go │ ├── 64. Minimum Path Sum_test.go │ └── README.md ├── 0066.Plus-One │ ├── 66. Plus One.go │ ├── 66. Plus One_test.go │ └── README.md ├── 0067.Add-Binary │ ├── 67. Add Binary.go │ ├── 67. Add Binary_test.go │ └── README.md ├── 0069.Sqrtx │ ├── 69. Sqrt(x).go │ ├── 69. Sqrt(x)_test.go │ └── README.md ├── 0070.Climbing-Stairs │ ├── 70. Climbing Stairs.go │ ├── 70. Climbing Stairs_test.go │ └── README.md ├── 0071.Simplify-Path │ ├── 71. Simplify Path.go │ ├── 71. Simplify Path_test.go │ └── README.md ├── 0074.Search-a-2D-Matrix │ ├── 74. Search a 2D Matrix.go │ ├── 74. Search a 2D Matrix_test.go │ └── README.md ├── 0075.Sort-Colors │ ├── 75. Sort Colors.go │ ├── 75. Sort Colors_test.go │ └── README.md ├── 0076.Minimum-Window-Substring │ ├── 76. Minimum Window Substring.go │ ├── 76. Minimum Window Substring_test.go │ └── README.md ├── 0077.Combinations │ ├── 77. Combinations.go │ ├── 77. Combinations_test.go │ └── README.md ├── 0078.Subsets │ ├── 78. Subsets.go │ ├── 78. Subsets_test.go │ └── README.md ├── 0079.Word-Search │ ├── 79. Word Search.go │ ├── 79. Word Search_test.go │ └── README.md ├── 0080.Remove-Duplicates-from-Sorted-Array-II │ ├── 80. Remove Duplicates from Sorted Array II.go │ ├── 80. Remove Duplicates from Sorted Array II_test.go │ └── README.md ├── 0081.Search-in-Rotated-Sorted-Array-II │ ├── 81. Search in Rotated Sorted Array II.go │ ├── 81. Search in Rotated Sorted Array II_test.go │ └── README.md ├── 0082.Remove-Duplicates-from-Sorted-List-II │ ├── 82. Remove Duplicates from Sorted List II.go │ ├── 82. Remove Duplicates from Sorted List II_test.go │ └── README.md ├── 0083.Remove-Duplicates-from-Sorted-List │ ├── 83. Remove Duplicates from Sorted List.go │ ├── 83. Remove Duplicates from Sorted List_test.go │ └── README.md ├── 0084.Largest-Rectangle-in-Histogram │ ├── 84. Largest Rectangle in Histogram.go │ ├── 84. Largest Rectangle in Histogram_test.go │ └── README.md ├── 0086.Partition-List │ ├── 86. Partition List.go │ ├── 86. Partition List_test.go │ └── README.md ├── 0088.Merge-Sorted-Array │ ├── 88. Merge Sorted Array.go │ ├── 88. Merge Sorted Array_test.go │ └── README.md ├── 0089.Gray-Code │ ├── 89. Gray Code.go │ ├── 89. Gray Code_test.go │ └── README.md ├── 0090.Subsets-II │ ├── 90. Subsets II.go │ ├── 90. Subsets II_test.go │ └── README.md ├── 0091.Decode-Ways │ ├── 91. Decode Ways.go │ ├── 91. Decode Ways_test.go │ └── README.md ├── 0092.Reverse-Linked-List-II │ ├── 92. Reverse Linked List II.go │ ├── 92. Reverse Linked List II_test.go │ └── README.md ├── 0093.Restore-IP-Addresses │ ├── 93. Restore IP Addresses.go │ ├── 93. Restore IP Addresses_test.go │ └── README.md ├── 0094.Binary-Tree-Inorder-Traversal │ ├── 94. Binary Tree Inorder Traversal.go │ ├── 94. Binary Tree Inorder Traversal_test.go │ └── README.md ├── 0095.Unique-Binary-Search-Trees-II │ ├── 95. Unique Binary Search Trees II.go │ ├── 95. Unique Binary Search Trees II_test.go │ └── README.md ├── 0096.Unique-Binary-Search-Trees │ ├── 96. Unique Binary Search Trees.go │ ├── 96. Unique Binary Search Trees_test.go │ └── README.md ├── 0098.Validate-Binary-Search-Tree │ ├── 98. Validate Binary Search Tree.go │ ├── 98. Validate Binary Search Tree_test.go │ └── README.md ├── 0099.Recover-Binary-Search-Tree │ ├── 99. Recover Binary Search Tree.go │ ├── 99. Recover Binary Search Tree_test.go │ └── README.md ├── 0100.Same-Tree │ ├── 100. Same Tree.go │ ├── 100. Same Tree_test.go │ └── README.md ├── 0101.Symmetric-Tree │ ├── 101. Symmetric Tree.go │ ├── 101. Symmetric Tree_test.go │ └── README.md ├── 0102.Binary-Tree-Level-Order-Traversal │ ├── 102. Binary Tree Level Order Traversal.go │ ├── 102. Binary Tree Level Order Traversal_test.go │ └── README.md ├── 0103.Binary-Tree-Zigzag-Level-Order-Traversal │ ├── 103. Binary Tree Zigzag Level Order Traversal.go │ ├── 103. Binary Tree Zigzag Level Order Traversal_test.go │ └── README.md ├── 0104.Maximum-Depth-of-Binary-Tree │ ├── 104. Maximum Depth of Binary Tree.go │ ├── 104. Maximum Depth of Binary Tree_test.go │ └── README.md ├── 0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal │ ├── 105. Construct Binary Tree from Preorder and Inorder Traversal.go │ ├── 105. Construct Binary Tree from Preorder and Inorder Traversal_test.go │ └── README.md ├── 0106.Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal │ ├── 106. Construct Binary Tree from Inorder and Postorder Traversal.go │ ├── 106. Construct Binary Tree from Inorder and Postorder Traversal_test.go │ └── README.md ├── 0107.Binary-Tree-Level-Order-Traversal-II │ ├── 107. Binary Tree Level Order Traversal II.go │ ├── 107. Binary Tree Level Order Traversal II_test.go │ └── README.md ├── 0108.Convert-Sorted-Array-to-Binary-Search-Tree │ ├── 108. Convert Sorted Array to Binary Search Tree.go │ ├── 108. Convert Sorted Array to Binary Search Tree_test.go │ └── README.md ├── 0109.Convert-Sorted-List-to-Binary-Search-Tree │ ├── 109. Convert Sorted List to Binary Search Tree.go │ ├── 109. Convert Sorted List to Binary Search Tree_test.go │ └── README.md ├── 0110.Balanced-Binary-Tree │ ├── 110. Balanced Binary Tree.go │ ├── 110. Balanced Binary Tree_test.go │ └── README.md ├── 0111.Minimum-Depth-of-Binary-Tree │ ├── 111. Minimum Depth of Binary Tree.go │ ├── 111. Minimum Depth of Binary Tree_test.go │ └── README.md ├── 0112.Path-Sum │ ├── 112. Path Sum.go │ ├── 112. Path Sum_test.go │ └── README.md ├── 0113.Path-Sum-II │ ├── 113. Path Sum II.go │ ├── 113. Path Sum II_test.go │ └── README.md ├── 0114.Flatten-Binary-Tree-to-Linked-List │ ├── 114. Flatten Binary Tree to Linked List.go │ ├── 114. Flatten Binary Tree to Linked List_test.go │ └── README.md ├── 0118.Pascals-Triangle │ ├── 118. Pascal's Triangle.go │ ├── 118. Pascal's Triangle_test.go │ └── README.md ├── 0120.Triangle │ ├── 120. Triangle.go │ ├── 120. Triangle_test.go │ └── README.md ├── 0121.Best-Time-to-Buy-and-Sell-Stock │ ├── 121. Best Time to Buy and Sell Stock.go │ ├── 121. Best Time to Buy and Sell Stock_test.go │ └── README.md ├── 0122.Best-Time-to-Buy-and-Sell-Stock-II │ ├── 122. Best Time to Buy and Sell Stock II.go │ ├── 122. Best Time to Buy and Sell Stock II_test.go │ └── README.md ├── 0124.Binary-Tree-Maximum-Path-Sum │ ├── 124. Binary Tree Maximum Path Sum.go │ ├── 124. Binary Tree Maximum Path Sum_test.go │ └── README.md ├── 0125.Valid-Palindrome │ ├── 125. Valid Palindrome.go │ ├── 125. Valid Palindrome_test.go │ └── README.md ├── 0126.Word-Ladder-II │ ├── 126. Word Ladder II.go │ ├── 126. Word Ladder II_test.go │ └── README.md ├── 0127.Word-Ladder │ ├── 127. Word Ladder.go │ ├── 127. Word Ladder_test.go │ └── README.md ├── 0128.Longest-Consecutive-Sequence │ ├── 128. Longest Consecutive Sequence.go │ ├── 128. Longest Consecutive Sequence_test.go │ └── README.md ├── 0129.Sum-Root-to-Leaf-Numbers │ ├── 129. Sum Root to Leaf Numbers.go │ ├── 129. Sum Root to Leaf Numbers_test.go │ └── README.md ├── 0130.Surrounded-Regions │ ├── 130. Surrounded Regions.go │ ├── 130. Surrounded Regions_test.go │ └── README.md ├── 0131.Palindrome-Partitioning │ ├── 131. Palindrome Partitioning.go │ ├── 131. Palindrome Partitioning_test.go │ └── README.md ├── 0136.Single-Number │ ├── 136. Single Number.go │ ├── 136. Single Number_test.go │ └── README.md ├── 0137.Single-Number-II │ ├── 137. Single Number II.go │ ├── 137. Single Number II_test.go │ └── README.md ├── 0141.Linked-List-Cycle │ ├── 141. Linked List Cycle.go │ ├── 141. Linked List Cycle_test.go │ └── README.md ├── 0142.Linked-List-Cycle-II │ ├── 142. Linked List Cycle II.go │ ├── 142. Linked List Cycle II_test.go │ └── README.md ├── 0143.Reorder-List │ ├── 143. Reorder List.go │ ├── 143. Reorder List_test.go │ └── README.md ├── 0144.Binary-Tree-Preorder-Traversal │ ├── 144. Binary Tree Preorder Traversal.go │ ├── 144. Binary Tree Preorder Traversal_test.go │ └── README.md ├── 0145.Binary-Tree-Postorder-Traversal │ ├── 145. Binary Tree Postorder Traversal.go │ ├── 145. Binary Tree Postorder Traversal_test.go │ └── README.md ├── 0147.Insertion-Sort-List │ ├── 147. Insertion Sort List.go │ ├── 147. Insertion Sort List_test.go │ └── README.md ├── 0148.Sort-List │ ├── 148. Sort List.go │ ├── 148. Sort List_test.go │ └── README.md ├── 0150.Evaluate-Reverse-Polish-Notation │ ├── 150. Evaluate Reverse Polish Notation.go │ ├── 150. Evaluate Reverse Polish Notation_test.go │ └── README.md ├── 0151.Reverse-Words-in-a-String │ ├── 151. Reverse Words in a String.go │ ├── 151. Reverse Words in a String_test.go │ └── README.md ├── 0152.Maximum-Product-Subarray │ ├── 152. Maximum Product Subarray.go │ ├── 152. Maximum Product Subarray_test.go │ └── README.md ├── 0153.Find-Minimum-in-Rotated-Sorted-Array │ ├── 153. Find Minimum in Rotated Sorted Array.go │ ├── 153. Find Minimum in Rotated Sorted Array_test.go │ └── README.md ├── 0154.Find-Minimum-in-Rotated-Sorted-Array-II │ ├── 154. Find Minimum in Rotated Sorted Array II.go │ ├── 154. Find Minimum in Rotated Sorted Array II_test.go │ └── README.md ├── 0155.Min-Stack │ ├── 155. Min Stack.go │ ├── 155. Min Stack_test.go │ └── README.md ├── 0160.Intersection-of-Two-Linked-Lists │ ├── 160. Intersection of Two Linked Lists.go │ ├── 160. Intersection of Two Linked Lists_test.go │ └── README.md ├── 0162.Find-Peak-Element │ ├── 162. Find Peak Element.go │ ├── 162. Find Peak Element_test.go │ └── README.md ├── 0164.Maximum-Gap │ ├── 164. Maximum Gap.go │ ├── 164. Maximum Gap_test.go │ └── README.md ├── 0167.Two-Sum-II---Input-array-is-sorted │ ├── 167. Two Sum II - Input array is sorted.go │ ├── 167. Two Sum II - Input array is sorted_test.go │ └── README.md ├── 0168.Excel-Sheet-Column-Title │ ├── 168. Excel Sheet Column Title.go │ ├── 168. Excel Sheet Column Title_test.go │ └── README.md ├── 0169.Majority-Element │ ├── 169. Majority Element.go │ ├── 169. Majority Element_test.go │ └── README.md ├── 0171.Excel-Sheet-Column-Number │ ├── 171. Excel Sheet Column Number.go │ ├── 171. Excel Sheet Column Number_test.go │ └── README.md ├── 0172.Factorial-Trailing-Zeroes │ ├── 172. Factorial Trailing Zeroes.go │ ├── 172. Factorial Trailing Zeroes_test.go │ └── README.md ├── 0173.Binary-Search-Tree-Iterator │ ├── 173. Binary Search Tree Iterator.go │ ├── 173. Binary Search Tree Iterator_test.go │ └── README.md ├── 0174.Dungeon-Game │ ├── 174. Dungeon Game.go │ ├── 174. Dungeon Game_test.go │ └── README.md ├── 0179.Largest-Number │ ├── 179. Largest Number.go │ ├── 179. Largest Number_test.go │ └── README.md ├── 0187.Repeated-DNA-Sequences │ ├── 187. Repeated DNA Sequences.go │ ├── 187. Repeated DNA Sequences_test.go │ └── README.md ├── 0190.Reverse-Bits │ ├── 190. Reverse Bits.go │ ├── 190. Reverse Bits_test.go │ └── README.md ├── 0191.Number-of-1-Bits │ ├── 191. Number of 1 Bits.go │ ├── 191. Number of 1 Bits_test.go │ └── README.md ├── 0198.House-Robber │ ├── 198. House Robber.go │ ├── 198. House Robber_test.go │ └── README.md ├── 0199.Binary-Tree-Right-Side-View │ ├── 199. Binary Tree Right Side View.go │ ├── 199. Binary Tree Right Side View_test.go │ └── README.md ├── 0200.Number-of-Islands │ ├── 200. Number of Islands.go │ ├── 200. Number of Islands_test.go │ └── README.md ├── 0201.Bitwise-AND-of-Numbers-Range │ ├── 201. Bitwise AND of Numbers Range.go │ ├── 201. Bitwise AND of Numbers Range_test.go │ └── README.md ├── 0202.Happy-Number │ ├── 202. Happy Number.go │ ├── 202. Happy Number_test.go │ └── README.md ├── 0203.Remove-Linked-List-Elements │ ├── 203. Remove Linked List Elements.go │ ├── 203. Remove Linked List Elements_test.go │ └── README.md ├── 0204.Count-Primes │ ├── 204. Count Primes.go │ ├── 204. Count Primes_test.go │ └── README.md ├── 0205.Isomorphic-Strings │ ├── 205. Isomorphic Strings.go │ ├── 205. Isomorphic Strings_test.go │ └── README.md ├── 0206.Reverse-Linked-List │ ├── 206. Reverse Linked List.go │ ├── 206. Reverse Linked List_test.go │ └── README.md ├── 0207.Course-Schedule │ ├── 207. Course Schedule.go │ ├── 207. Course Schedule_test.go │ └── README.md ├── 0208.Implement-Trie-Prefix-Tree │ ├── 208. Implement Trie (Prefix Tree).go │ ├── 208. Implement Trie (Prefix Tree)_test.go │ └── README.md ├── 0209.Minimum-Size-Subarray-Sum │ ├── 209. Minimum Size Subarray Sum.go │ ├── 209. Minimum Size Subarray Sum_test.go │ └── README.md ├── 0210.Course-Schedule-II │ ├── 210. Course Schedule II.go │ ├── 210. Course Schedule II_test.go │ └── README.md ├── 0211.Add-and-Search-Word---Data-structure-design │ ├── 211. Add and Search Word - Data structure design.go │ ├── 211. Add and Search Word - Data structure design_test.go │ └── README.md ├── 0212.Word-Search-II │ ├── 212. Word Search II.go │ ├── 212. Word Search II_test.go │ └── README.md ├── 0213.House-Robber-II │ ├── 213. House Robber II.go │ ├── 213. House Robber II_test.go │ └── README.md ├── 0215.Kth-Largest-Element-in-an-Array │ ├── 215. Kth Largest Element in an Array.go │ ├── 215. Kth Largest Element in an Array_test.go │ └── README.md ├── 0216.Combination-Sum-III │ ├── 216. Combination Sum III.go │ ├── 216. Combination Sum III_test.go │ └── README.md ├── 0217.Contains-Duplicate │ ├── 217. Contains Duplicate.go │ ├── 217. Contains Duplicate_test.go │ └── README.md ├── 0218.The-Skyline-Problem │ ├── 218. The Skyline Problem.go │ ├── 218. The Skyline Problem_test.go │ └── README.md ├── 0219.Contains-Duplicate-II │ ├── 219. Contains Duplicate II.go │ ├── 219. Contains Duplicate II_test.go │ └── README.md ├── 0220.Contains-Duplicate-III │ ├── 220. Contains Duplicate III.go │ ├── 220. Contains Duplicate III_test.go │ └── README.md ├── 0222.Count-Complete-Tree-Nodes │ ├── 222. Count Complete Tree Nodes.go │ ├── 222. Count Complete Tree Nodes_test.go │ └── README.md ├── 0223.Rectangle-Area │ ├── 223. Rectangle Area.go │ ├── 223. Rectangle Area_test.go │ └── README.md ├── 0224.Basic-Calculator │ ├── 224. Basic Calculator.go │ ├── 224. Basic Calculator_test.go │ └── README.md ├── 0225.Implement-Stack-using-Queues │ ├── 225. Implement Stack using Queues.go │ ├── 225. Implement Stack using Queues_test.go │ └── README.md ├── 0226.Invert-Binary-Tree │ ├── 226. Invert Binary Tree.go │ ├── 226. Invert Binary Tree_test.go │ └── README.md ├── 0229.Majority-Element-II │ ├── 229. Majority Element II.go │ ├── 229. Majority Element II_test.go │ └── README.md ├── 0230.Kth-Smallest-Element-in-a-BST │ ├── 230. Kth Smallest Element in a BST.go │ ├── 230. Kth Smallest Element in a BST_test.go │ └── README.md ├── 0231.Power-of-Two │ ├── 231. Power of Two.go │ ├── 231. Power of Two_test.go │ └── README.md ├── 0232.Implement-Queue-using-Stacks │ ├── 232. Implement Queue using Stacks.go │ ├── 232. Implement Queue using Stacks_test.go │ └── README.md ├── 0234.Palindrome-Linked-List │ ├── 234. Palindrome Linked List.go │ ├── 234. Palindrome Linked List_test.go │ └── README.md ├── 0235.Lowest-Common-Ancestor-of-a-Binary-Search-Tree │ ├── 235. Lowest Common Ancestor of a Binary Search Tree.go │ ├── 235. Lowest Common Ancestor of a Binary Search Tree_test.go │ └── README.md ├── 0236.Lowest-Common-Ancestor-of-a-Binary-Tree │ ├── 236. Lowest Common Ancestor of a Binary Tree.go │ ├── 236. Lowest Common Ancestor of a Binary Tree_test.go │ └── README.md ├── 0237.Delete-Node-in-a-Linked-List │ ├── 237. Delete Node in a Linked List.go │ ├── 237. Delete Node in a Linked List_test.go │ └── README.md ├── 0239.Sliding-Window-Maximum │ ├── 239. Sliding Window Maximum.go │ ├── 239. Sliding Window Maximum_test.go │ └── README.md ├── 0240.Search-a-2D-Matrix-II │ ├── 240. Search a 2D Matrix II.go │ ├── 240. Search a 2D Matrix II_test.go │ └── README.md ├── 0242.Valid-Anagram │ ├── 242. Valid Anagram.go │ ├── 242. Valid Anagram_test.go │ └── README.md ├── 0257.Binary-Tree-Paths │ ├── 257. Binary Tree Paths.go │ ├── 257. Binary Tree Paths_test.go │ └── README.md ├── 0258.Add-Digits │ ├── 258. Add Digits.go │ ├── 258. Add Digits_test.go │ └── README.md ├── 0260.Single-Number-III │ ├── 260. Single Number III.go │ ├── 260. Single Number III_test.go │ └── README.md ├── 0263.Ugly-Number │ ├── 263. Ugly Number.go │ ├── 263. Ugly Number_test.go │ └── README.md ├── 0268.Missing-Number │ ├── 268. Missing Number.go │ ├── 268. Missing Number_test.go │ └── README.md ├── 0274.H-Index │ ├── 274. H-Index.go │ ├── 274. H-Index_test.go │ └── README.md ├── 0275.H-Index-II │ ├── 275. H-Index II.go │ ├── 275. H-Index II_test.go │ └── README.md ├── 0283.Move-Zeroes │ ├── 283. Move Zeroes.go │ ├── 283. Move Zeroes_test.go │ └── README.md ├── 0287.Find-the-Duplicate-Number │ ├── 287. Find the Duplicate Number.go │ ├── 287. Find the Duplicate Number_test.go │ └── README.md ├── 0290.Word-Pattern │ ├── 290. Word Pattern.go │ ├── 290. Word Pattern_test.go │ └── README.md ├── 0300.Longest-Increasing-Subsequence │ ├── 300. Longest Increasing Subsequence.go │ ├── 300. Longest Increasing Subsequence_test.go │ └── README.md ├── 0303.Range-Sum-Query---Immutable │ ├── 303. Range Sum Query - Immutable.go │ ├── 303. Range Sum Query - Immutable_test.go │ └── README.md ├── 0306.Additive-Number │ ├── 306. Additive Number.go │ ├── 306. Additive Number_test.go │ └── README.md ├── 0307.Range-Sum-Query---Mutable │ ├── 307. Range Sum Query - Mutable.go │ ├── 307. Range Sum Query - Mutable_test.go │ └── README.md ├── 0309.Best-Time-to-Buy-and-Sell-Stock-with-Cooldown │ ├── 309. Best Time to Buy and Sell Stock with Cooldown.go │ ├── 309. Best Time to Buy and Sell Stock with Cooldown_test.go │ └── README.md ├── 0315.Count-of-Smaller-Numbers-After-Self │ ├── 315. Count of Smaller Numbers After Self.go │ ├── 315. Count of Smaller Numbers After Self_test.go │ └── README.md ├── 0318.Maximum-Product-of-Word-Lengths │ ├── 318. Maximum Product of Word Lengths.go │ ├── 318. Maximum Product of Word Lengths_test.go │ └── README.md ├── 0322.Coin-Change │ ├── 322. Coin Change.go │ ├── 322. Coin Change_test.go │ └── README.md ├── 0324.Wiggle-Sort-II │ ├── 324. Wiggle Sort II.go │ ├── 324. Wiggle Sort II_test.go │ └── README.md ├── 0326.Power-of-Three │ ├── 326. Power of Three.go │ ├── 326. Power of Three_test.go │ └── README.md ├── 0327.Count-of-Range-Sum │ ├── 327. Count of Range Sum.go │ ├── 327. Count of Range Sum_test.go │ └── README.md ├── 0328.Odd-Even-Linked-List │ ├── 328. Odd Even Linked List.go │ ├── 328. Odd Even Linked List_test.go │ └── README.md ├── 0329.Longest-Increasing-Path-in-a-Matrix │ ├── 329. Longest Increasing Path in a Matrix.go │ ├── 329. Longest Increasing Path in a Matrix_test.go │ └── README.md ├── 0331.Verify-Preorder-Serialization-of-a-Binary-Tree │ ├── 331. Verify Preorder Serialization of a Binary Tree.go │ ├── 331. Verify Preorder Serialization of a Binary Tree_test.go │ └── README.md ├── 0337.House-Robber-III │ ├── 337. House Robber III.go │ ├── 337. House Robber III_test.go │ └── README.md ├── 0338.Counting-Bits │ ├── 338. Counting Bits.go │ ├── 338. Counting Bits_test.go │ └── README.md ├── 0342.Power-of-Four │ ├── 342. Power of Four.go │ ├── 342. Power of Four_test.go │ └── README.md ├── 0343.Integer-Break │ ├── 343. Integer Break.go │ ├── 343. Integer Break_test.go │ └── README.md ├── 0344.Reverse-String │ ├── 344. Reverse String.go │ ├── 344. Reverse String_test.go │ └── README.md ├── 0345.Reverse-Vowels-of-a-String │ ├── 345. Reverse Vowels of a String.go │ ├── 345. Reverse Vowels of a String_test.go │ └── README.md ├── 0347.Top-K-Frequent-Elements │ ├── 347. Top K Frequent Elements.go │ ├── 347. Top K Frequent Elements_test.go │ └── README.md ├── 0349.Intersection-of-Two-Arrays │ ├── 349. Intersection of Two Arrays.go │ ├── 349. Intersection of Two Arrays_test.go │ └── README.md ├── 0350.Intersection-of-Two-Arrays-II │ ├── 350. Intersection of Two Arrays II.go │ ├── 350. Intersection of Two Arrays II_test.go │ └── README.md ├── 0354.Russian-Doll-Envelopes │ ├── 354. Russian Doll Envelopes.go │ ├── 354. Russian Doll Envelopes_test.go │ └── README.md ├── 0357.Count-Numbers-with-Unique-Digits │ ├── 357. Count Numbers with Unique Digits.go │ ├── 357. Count Numbers with Unique Digits_test.go │ └── README.md ├── 0367.Valid-Perfect-Square │ ├── 367. Valid Perfect Square.go │ ├── 367. Valid Perfect Square_test.go │ └── README.md ├── 0371.Sum-of-Two-Integers │ ├── 371. Sum of Two Integers.go │ ├── 371. Sum of Two Integers_test.go │ └── README.md ├── 0372.Super-Pow │ ├── 372. Super Pow.go │ ├── 372. Super Pow_test.go │ └── README.md ├── 0373.Find-K-Pairs-with-Smallest-Sums │ ├── 373. Find K Pairs with Smallest Sums.go │ ├── 373. Find K Pairs with Smallest Sums_test.go │ └── README.md ├── 0378.Kth-Smallest-Element-in-a-Sorted-Matrix │ ├── 378. Kth Smallest Element in a Sorted Matrix.go │ ├── 378. Kth Smallest Element in a Sorted Matrix_test.go │ └── README.md ├── 0385.Mini-Parser │ ├── 385. Mini Parser.go │ ├── 385. Mini Parser_test.go │ └── README.md ├── 0386.Lexicographical-Numbers │ ├── 386. Lexicographical Numbers.go │ ├── 386. Lexicographical Numbers_test.go │ └── README.md ├── 0387.First-Unique-Character-in-a-String │ ├── 387. First Unique Character in a String.go │ ├── 387. First Unique Character in a String_test.go │ └── README.md ├── 0389.Find-the-Difference │ ├── 389. Find the Difference.go │ ├── 389. Find the Difference_test.go │ └── README.md ├── 0392.Is-Subsequence │ ├── 392. Is Subsequence.go │ ├── 392. Is Subsequence_test.go │ └── README.md ├── 0393.UTF-8-Validation │ ├── 393. UTF-8 Validation.go │ ├── 393. UTF-8 Validation_test.go │ └── README.md ├── 0394.Decode-String │ ├── 394. Decode String.go │ ├── 394. Decode String_test.go │ └── README.md ├── 0397.Integer-Replacement │ ├── 397. Integer Replacement.go │ ├── 397. Integer Replacement_test.go │ └── README.md ├── 0399.Evaluate-Division │ ├── 399. Evaluate Division.go │ ├── 399. Evaluate Division_test.go │ └── README.md ├── 0401.Binary-Watch │ ├── 401. Binary Watch.go │ ├── 401. Binary Watch_test.go │ └── README.md ├── 0402.Remove-K-Digits │ ├── 402. Remove K Digits.go │ ├── 402. Remove K Digits_test.go │ └── README.md ├── 0404.Sum-of-Left-Leaves │ ├── 404. Sum of Left Leaves.go │ ├── 404. Sum of Left Leaves_test.go │ └── README.md ├── 0405.Convert-a-Number-to-Hexadecimal │ ├── 405. Convert a Number to Hexadecimal.go │ ├── 405. Convert a Number to Hexadecimal_test.go │ └── README.md ├── 0409.Longest-Palindrome │ ├── 409. Longest Palindrome.go │ ├── 409. Longest Palindrome_test.go │ └── README.md ├── 0410.Split-Array-Largest-Sum │ ├── 410. Split Array Largest Sum.go │ ├── 410. Split Array Largest Sum_test.go │ └── README.md ├── 0412.Fizz-Buzz │ ├── 412. Fizz Buzz.go │ ├── 412. Fizz Buzz_test.go │ └── README.md ├── 0414.Third-Maximum-Number │ ├── 414. Third Maximum Number.go │ ├── 414. Third Maximum Number_test.go │ └── README.md ├── 0416.Partition-Equal-Subset-Sum │ ├── 416. Partition Equal Subset Sum.go │ ├── 416. Partition Equal Subset Sum_test.go │ └── README.md ├── 0421.Maximum-XOR-of-Two-Numbers-in-an-Array │ ├── 421. Maximum XOR of Two Numbers in an Array.go │ ├── 421. Maximum XOR of Two Numbers in an Array_test.go │ └── README.md ├── 0424.Longest-Repeating-Character-Replacement │ ├── 424. Longest Repeating Character Replacement.go │ ├── 424. Longest Repeating Character Replacement_test.go │ └── README.md ├── 0433.Minimum-Genetic-Mutation │ ├── 433. Minimum Genetic Mutation.go │ ├── 433. Minimum Genetic Mutation_test.go │ └── README.md ├── 0435.Non-overlapping-Intervals │ ├── 435. Non-overlapping Intervals.go │ ├── 435. Non-overlapping Intervals_test.go │ └── README.md ├── 0436.Find-Right-Interval │ ├── 436. Find Right Interval.go │ ├── 436. Find Right Interval_test.go │ └── README.md ├── 0437.Path-Sum-III │ ├── 437. Path Sum III.go │ ├── 437. Path Sum III_test.go │ └── README.md ├── 0438.Find-All-Anagrams-in-a-String │ ├── 438. Find All Anagrams in a String.go │ ├── 438. Find All Anagrams in a String_test.go │ └── README.md ├── 0441.Arranging-Coins │ ├── 441. Arranging Coins.go │ ├── 441. Arranging Coins_test.go │ └── README.md ├── 0445.Add-Two-Numbers-II │ ├── 445. Add Two Numbers II.go │ ├── 445. Add Two Numbers II_test.go │ └── README.md ├── 0447.Number-of-Boomerangs │ ├── 447. Number of Boomerangs.go │ ├── 447. Number of Boomerangs_test.go │ └── README.md ├── 0448.Find-All-Numbers-Disappeared-in-an-Array │ ├── 448. Find All Numbers Disappeared in an Array.go │ ├── 448. Find All Numbers Disappeared in an Array_test.go │ └── README.md ├── 0451.Sort-Characters-By-Frequency │ ├── 451. Sort Characters By Frequency.go │ ├── 451. Sort Characters By Frequency_test.go │ └── README.md ├── 0453.Minimum-Moves-to-Equal-Array-Elements │ ├── 453. Minimum Moves to Equal Array Elements.go │ ├── 453. Minimum Moves to Equal Array Elements_test.go │ └── README.md ├── 0454.4Sum-II │ ├── 454. 4Sum II.go │ ├── 454. 4Sum II_test.go │ └── README.md ├── 0455.Assign-Cookies │ ├── 455. Assign Cookies.go │ ├── 455. Assign Cookies_test.go │ └── README.md ├── 0456.132-Pattern │ ├── 456. 132 Pattern.go │ ├── 456. 132 Pattern_test.go │ └── README.md ├── 0457.Circular-Array-Loop │ ├── 457. Circular Array Loop.go │ ├── 457. Circular Array Loop_test.go │ └── README.md ├── 0461.Hamming-Distance │ ├── 461. Hamming Distance.go │ ├── 461. Hamming Distance_test.go │ └── README.md ├── 0463.Island-Perimeter │ ├── 463. Island Perimeter.go │ ├── 463. Island Perimeter_test.go │ └── README.md ├── 0470.Implement-Rand10-Using-Rand7 │ ├── 470. Implement Rand10() Using Rand7().go │ ├── 470. Implement Rand10() Using Rand7()_test.go │ └── README.md ├── 0474.Ones-and-Zeroes │ ├── 474. Ones and Zeroes.go │ ├── 474. Ones and Zeroes_test.go │ └── README.md ├── 0475.Heaters │ ├── 475. Heaters.go │ ├── 475. Heaters_test.go │ └── README.md ├── 0476.Number-Complement │ ├── 476. Number Complement.go │ ├── 476. Number Complement_test.go │ └── README.md ├── 0477.Total-Hamming-Distance │ ├── 477. Total Hamming Distance.go │ ├── 477. Total Hamming Distance_test.go │ └── README.md ├── 0480.Sliding-Window-Median │ ├── 480. Sliding Window Median.go │ ├── 480. Sliding Window Median_test.go │ └── README.md ├── 0483.Smallest-Good-Base │ ├── 483. Smallest Good Base.go │ ├── 483. Smallest Good Base_test.go │ └── README.md ├── 0485.Max-Consecutive-Ones │ ├── 485. Max Consecutive Ones.go │ ├── 485. Max Consecutive Ones_test.go │ └── README.md ├── 0491.Increasing-Subsequences │ ├── 491. Increasing Subsequences.go │ ├── 491. Increasing Subsequences_test.go │ └── README.md ├── 0493.Reverse-Pairs │ ├── 493. Reverse Pairs.go │ ├── 493. Reverse Pairs_test.go │ └── README.md ├── 0494.Target-Sum │ ├── 494. Target Sum.go │ ├── 494. Target Sum_test.go │ └── README.md ├── 0496.Next-Greater-Element-I │ ├── 496. Next Greater Element I.go │ ├── 496. Next Greater Element I_test.go │ └── README.md ├── 0497.Random-Point-in-Non-overlapping-Rectangles │ ├── 497. Random Point in Non-overlapping Rectangles.go │ ├── 497. Random Point in Non-overlapping Rectangles_test.go │ └── README.md ├── 0498.Diagonal-Traverse │ ├── 498. Diagonal Traverse.go │ ├── 498. Diagonal Traverse_test.go │ └── README.md ├── 0500.Keyboard-Row │ ├── 500. Keyboard Row.go │ ├── 500. Keyboard Row_test.go │ └── README.md ├── 0503.Next-Greater-Element-II │ ├── 503. Next Greater Element II.go │ ├── 503. Next Greater Element II_test.go │ └── README.md ├── 0507.Perfect-Number │ ├── 507. Perfect Number.go │ ├── 507. Perfect Number_test.go │ └── README.md ├── 0508.Most-Frequent-Subtree-Sum │ ├── 508. Most Frequent Subtree Sum.go │ ├── 508. Most Frequent Subtree Sum_test.go │ └── README.md ├── 0509.Fibonacci-Number │ ├── 509. Fibonacci Number.go │ ├── 509. Fibonacci Number_test.go │ └── README.md ├── 0513.Find-Bottom-Left-Tree-Value │ ├── 513. Find Bottom Left Tree Value.go │ ├── 513. Find Bottom Left Tree Value_test.go │ └── README.md ├── 0515.Find-Largest-Value-in-Each-Tree-Row │ ├── 515. Find Largest Value in Each Tree Row.go │ ├── 515. Find Largest Value in Each Tree Row_test.go │ └── README.md ├── 0524.Longest-Word-in-Dictionary-through-Deleting │ ├── 524. Longest Word in Dictionary through Deleting.go │ ├── 524. Longest Word in Dictionary through Deleting_test.go │ └── README.md ├── 0526.Beautiful-Arrangement │ ├── 526. Beautiful Arrangement.go │ ├── 526. Beautiful Arrangement_test.go │ └── README.md ├── 0528.Random-Pick-with-Weight │ ├── 528. Random Pick with Weight.go │ ├── 528. Random Pick with Weight_test.go │ └── README.md ├── 0529.Minesweeper │ ├── 529. Minesweeper.go │ ├── 529. Minesweeper_test.go │ └── README.md ├── 0532.K-diff-Pairs-in-an-Array │ ├── 532. K-diff Pairs in an Array.go │ ├── 532. K-diff Pairs in an Array_test.go │ └── README.md ├── 0537.Complex-Number-Multiplication │ ├── 537. Complex Number Multiplication.go │ ├── 537. Complex Number Multiplication_test.go │ └── README.md ├── 0541.Reverse-String-II │ ├── 541. Reverse String II.go │ ├── 541. Reverse String II_test.go │ └── README.md ├── 0542.01-Matrix │ ├── 542. 01 Matrix.go │ ├── 542. 01 Matrix_test.go │ └── README.md ├── 0547.Friend-Circles │ ├── 547. Friend Circles.go │ ├── 547. Friend Circles_test.go │ └── README.md ├── 0557.Reverse-Words-in-a-String-III │ ├── 557. Reverse Words in a String III.go │ ├── 557. Reverse Words in a String III_test.go │ └── README.md ├── 0561.Array-Partition-I │ ├── 561. Array Partition I.go │ ├── 561. Array Partition I_test.go │ └── README.md ├── 0563.Binary-Tree-Tilt │ ├── 563. Binary Tree Tilt.go │ ├── 563. Binary Tree Tilt_test.go │ └── README.md ├── 0566.Reshape-the-Matrix │ ├── 566. Reshape the Matrix.go │ ├── 566. Reshape the Matrix_test.go │ └── README.md ├── 0567.Permutation-in-String │ ├── 567. Permutation in String.go │ ├── 567. Permutation in String_test.go │ └── README.md ├── 0572.Subtree-of-Another-Tree │ ├── 572. Subtree of Another Tree.go │ ├── 572. Subtree of Another Tree_test.go │ └── README.md ├── 0575.Distribute-Candies │ ├── 575. Distribute Candies.go │ ├── 575. Distribute Candies_test.go │ └── README.md ├── 0594.Longest-Harmonious-Subsequence │ ├── 594. Longest Harmonious Subsequence.go │ ├── 594. Longest Harmonious Subsequence_test.go │ └── README.md ├── 0598.Range-Addition-II │ ├── 598. Range Addition II.go │ ├── 598. Range Addition II_test.go │ └── README.md ├── 0599.Minimum-Index-Sum-of-Two-Lists │ ├── 599. Minimum Index Sum of Two Lists.go │ ├── 599. Minimum Index Sum of Two Lists_test.go │ └── README.md ├── 0628.Maximum-Product-of-Three-Numbers │ ├── 628. Maximum Product of Three Numbers.go │ ├── 628. Maximum Product of Three Numbers_test.go │ └── README.md ├── 0632.Smallest-Range-Covering-Elements-from-K-Lists │ ├── 632. Smallest Range Covering Elements from K Lists.go │ ├── 632. Smallest Range Covering Elements from K Lists_test.go │ └── README.md ├── 0633.Sum-of-Square-Numbers │ ├── 633. Sum of Square Numbers.go │ ├── 633. Sum of Square Numbers_test.go │ └── README.md ├── 0636.Exclusive-Time-of-Functions │ ├── 636. Exclusive Time of Functions.go │ ├── 636. Exclusive Time of Functions_test.go │ └── README.md ├── 0637.Average-of-Levels-in-Binary-Tree │ ├── 637. Average of Levels in Binary Tree.go │ ├── 637. Average of Levels in Binary Tree_test.go │ └── README.md ├── 0638.Shopping-Offers │ ├── 638. Shopping Offers.go │ ├── 638. Shopping Offers_test.go │ └── README.md ├── 0645.Set-Mismatch │ ├── 645. Set Mismatch.go │ ├── 645. Set Mismatch_test.go │ └── README.md ├── 0648.Replace-Words │ ├── 648. Replace Words.go │ ├── 648. Replace Words_test.go │ └── README.md ├── 0653.Two-Sum-IV---Input-is-a-BST │ ├── 653. Two Sum IV - Input is a BST.go │ ├── 653. Two Sum IV - Input is a BST_test.go │ └── README.md ├── 0658.Find-K-Closest-Elements │ ├── 658. Find K Closest Elements.go │ ├── 658. Find K Closest Elements_test.go │ └── README.md ├── 0661.Image-Smoother │ ├── 661. Image Smoother.go │ ├── 661. Image Smoother_test.go │ └── README.md ├── 0662.Maximum-Width-of-Binary-Tree │ ├── 662. Maximum Width of Binary Tree.go │ ├── 662. Maximum Width of Binary Tree_test.go │ └── README.md ├── 0668.Kth-Smallest-Number-in-Multiplication-Table │ ├── 668. Kth Smallest Number in Multiplication Table.go │ ├── 668. Kth Smallest Number in Multiplication Table_test.go │ └── README.md ├── 0676.Implement-Magic-Dictionary │ ├── 676. Implement Magic Dictionary.go │ ├── 676. Implement Magic Dictionary_test.go │ └── README.md ├── 0682.Baseball-Game │ ├── 682. Baseball Game.go │ ├── 682. Baseball Game_test.go │ └── README.md ├── 0684.Redundant-Connection │ ├── 684. Redundant Connection.go │ ├── 684. Redundant Connection_test.go │ └── README.md ├── 0685.Redundant-Connection-II │ ├── 685. Redundant Connection II.go │ ├── 685. Redundant Connection II_test.go │ └── README.md ├── 0693.Binary-Number-with-Alternating-Bits │ ├── 693. Binary Number with Alternating Bits.go │ ├── 693. Binary Number with Alternating Bits_test.go │ └── README.md ├── 0695.Max-Area-of-Island │ ├── 695. Max Area of Island.go │ ├── 695. Max Area of Island_test.go │ └── README.md ├── 0697.Degree-of-an-Array │ ├── 697. Degree of an Array.go │ ├── 697. Degree of an Array_test.go │ └── README.md ├── 0699.Falling-Squares │ ├── 699. Falling Squares.go │ ├── 699. Falling Squares_test.go │ └── README.md ├── 0704.Binary-Search │ ├── 704. Binary Search.go │ ├── 704. Binary Search_test.go │ └── README.md ├── 0705.Design-HashSet │ ├── 705. Design HashSet.go │ ├── 705. Design HashSet_test.go │ └── README.md ├── 0706.Design-HashMap │ ├── 706. Design HashMap.go │ ├── 706. Design HashMap_test.go │ └── README.md ├── 0707.Design-Linked-List │ ├── 707. Design Linked List.go │ ├── 707. Design Linked List_test.go │ └── README.md ├── 0710.Random-Pick-with-Blacklist │ ├── 710. Random Pick with Blacklist.go │ ├── 710. Random Pick with Blacklist_test.go │ └── README.md ├── 0713.Subarray-Product-Less-Than-K │ ├── 713. Subarray Product Less Than K.go │ ├── 713. Subarray Product Less Than K_test.go │ └── README.md ├── 0714.Best-Time-to-Buy-and-Sell-Stock-with-Transaction-Fee │ ├── 714. Best Time to Buy and Sell Stock with Transaction Fee.go │ ├── 714. Best Time to Buy and Sell Stock with Transaction Fee_test.go │ └── README.md ├── 0715.Range-Module │ ├── 715. Range Module.go │ ├── 715. Range Module_test.go │ └── README.md ├── 0717.1-bit-and-2-bit-Characters │ ├── 717. 1-bit and 2-bit Characters.go │ ├── 717. 1-bit and 2-bit Characters_test.go │ └── README.md ├── 0718.Maximum-Length-of-Repeated-Subarray │ ├── 718. Maximum Length of Repeated Subarray.go │ ├── 718. Maximum Length of Repeated Subarray_test.go │ └── README.md ├── 0719.Find-K-th-Smallest-Pair-Distance │ ├── 719. Find K-th Smallest Pair Distance.go │ ├── 719. Find K-th Smallest Pair Distance_test.go │ └── README.md ├── 0720.Longest-Word-in-Dictionary │ ├── 720. Longest Word in Dictionary.go │ ├── 720. Longest Word in Dictionary_test.go │ └── README.md ├── 0721.Accounts-Merge │ ├── 721. Accounts Merge.go │ ├── 721. Accounts Merge_test.go │ └── README.md ├── 0724.Find-Pivot-Index │ ├── 724. Find Pivot Index.go │ ├── 724. Find Pivot Index_test.go │ └── README.md ├── 0725.Split-Linked-List-in-Parts │ ├── 725. Split Linked List in Parts.go │ ├── 725. Split Linked List in Parts_test.go │ └── README.md ├── 0726.Number-of-Atoms │ ├── 726. Number of Atoms.go │ ├── 726. Number of Atoms_test.go │ └── README.md ├── 0729.My-Calendar-I │ ├── 729. My Calendar I.go │ ├── 729. My Calendar I_test.go │ └── README.md ├── 0732.My-Calendar-III │ ├── 732. My Calendar III.go │ ├── 732. My Calendar III_test.go │ └── README.md ├── 0733.Flood-Fill │ ├── 733. Flood Fill.go │ ├── 733. Flood Fill_test.go │ └── README.md ├── 0735.Asteroid-Collision │ ├── 735. Asteroid Collision.go │ ├── 735. Asteroid Collision_test.go │ └── README.md ├── 0739.Daily-Temperatures │ ├── 739. Daily Temperatures.go │ ├── 739. Daily Temperatures_test.go │ └── README.md ├── 0744.Find-Smallest-Letter-Greater-Than-Target │ ├── 744. Find Smallest Letter Greater Than Target.go │ ├── 744. Find Smallest Letter Greater Than Target_test.go │ └── README.md ├── 0745.Prefix-and-Suffix-Search │ ├── 745. Prefix and Suffix Search.go │ ├── 745. Prefix and Suffix Search_test.go │ └── README.md ├── 0746.Min-Cost-Climbing-Stairs │ ├── 746. Min Cost Climbing Stairs.go │ ├── 746. Min Cost Climbing Stairs_test.go │ └── README.md ├── 0748.Shortest-Completing-Word │ ├── 748. Shortest Completing Word.go │ ├── 748. Shortest Completing Word_test.go │ └── README.md ├── 0753.Cracking-the-Safe │ ├── 753. Cracking the Safe.go │ ├── 753. Cracking the Safe_test.go │ └── README.md ├── 0756.Pyramid-Transition-Matrix │ ├── 756. Pyramid Transition Matrix.go │ ├── 756. Pyramid Transition Matrix_test.go │ └── README.md ├── 0762.Prime-Number-of-Set-Bits-in-Binary-Representation │ ├── 762. Prime Number of Set Bits in Binary Representation.go │ ├── 762. Prime Number of Set Bits in Binary Representation_test.go │ └── README.md ├── 0763.Partition-Labels │ ├── 763. Partition Labels.go │ ├── 763. Partition Labels_test.go │ └── README.md ├── 0765.Couples-Holding-Hands │ ├── 765. Couples Holding Hands.go │ ├── 765. Couples Holding Hands_test.go │ └── README.md ├── 0766.Toeplitz-Matrix │ ├── 766. Toeplitz Matrix.go │ ├── 766. Toeplitz Matrix_test.go │ └── README.md ├── 0767.Reorganize-String │ ├── 767. Reorganize String.go │ ├── 767. Reorganize String_test.go │ └── README.md ├── 0771.Jewels-and-Stones │ ├── 771. Jewels and Stones.go │ ├── 771. Jewels and Stones_test.go │ └── README.md ├── 0778.Swim-in-Rising-Water │ ├── 778. Swim in Rising Water.go │ ├── 778. Swim in Rising Water_test.go │ └── README.md ├── 0781.Rabbits-in-Forest │ ├── 781. Rabbits in Forest.go │ ├── 781. Rabbits in Forest_test.go │ └── README.md ├── 0784.Letter-Case-Permutation │ ├── 784. Letter Case Permutation.go │ ├── 784. Letter Case Permutation_test.go │ └── README.md ├── 0786.K-th-Smallest-Prime-Fraction │ ├── 786. K-th Smallest Prime Fraction.go │ ├── 786. K-th Smallest Prime Fraction_test.go │ └── README.md ├── 0793.Preimage-Size-of-Factorial-Zeroes-Function │ ├── 793. Preimage Size of Factorial Zeroes Function.go │ ├── 793. Preimage Size of Factorial Zeroes Function_test.go │ └── README.md ├── 0802.Find-Eventual-Safe-States │ ├── 802. Find Eventual Safe States.go │ ├── 802. Find Eventual Safe States_test.go │ └── README.md ├── 0803.Bricks-Falling-When-Hit │ ├── 803. Bricks Falling When Hit.go │ ├── 803. Bricks Falling When Hit_test.go │ └── README.md ├── 0811.Subdomain-Visit-Count │ ├── 811. Subdomain Visit Count.go │ ├── 811. Subdomain Visit Count_test.go │ └── README.md ├── 0812.Largest-Triangle-Area │ ├── 812. Largest Triangle Area.go │ ├── 812. Largest Triangle Area_test.go │ └── README.md ├── 0815.Bus-Routes │ ├── 815. Bus Routes.go │ ├── 815. Bus Routes_test.go │ └── README.md ├── 0817.Linked-List-Components │ ├── 817. Linked List Components.go │ ├── 817. Linked List Components_test.go │ └── README.md ├── 0819.Most-Common-Word │ ├── 819. Most Common Word.go │ ├── 819. Most Common Word_test.go │ └── README.md ├── 0826.Most-Profit-Assigning-Work │ ├── 826. Most Profit Assigning Work.go │ ├── 826. Most Profit Assigning Work_test.go │ └── README.md ├── 0828.COPYRIGHT-PROBLEM-XXX │ ├── 828. Unique Letter String.go │ ├── 828. Unique Letter String_test.go │ └── README.md ├── 0832.Flipping-an-Image │ ├── 832. Flipping an Image.go │ ├── 832. Flipping an Image_test.go │ └── README.md ├── 0834.Sum-of-Distances-in-Tree │ ├── 834. Sum of Distances in Tree.go │ ├── 834. Sum of Distances in Tree_test.go │ └── README.md ├── 0836.Rectangle-Overlap │ ├── 836. Rectangle Overlap.go │ ├── 836. Rectangle Overlap_test.go │ └── README.md ├── 0838.Push-Dominoes │ ├── 838. Push Dominoes.go │ ├── 838. Push Dominoes_test.go │ └── README.md ├── 0839.Similar-String-Groups │ ├── 839. Similar String Groups.go │ ├── 839. Similar String Groups_test.go │ └── README.md ├── 0841.Keys-and-Rooms │ ├── 841. Keys and Rooms.go │ ├── 841. Keys and Rooms_test.go │ └── README.md ├── 0842.Split-Array-into-Fibonacci-Sequence │ ├── 842. Split Array into Fibonacci Sequence.go │ ├── 842. Split Array into Fibonacci Sequence_test.go │ └── README.md ├── 0844.Backspace-String-Compare │ ├── 844. Backspace String Compare.go │ ├── 844. Backspace String Compare_test.go │ └── README.md ├── 0845.Longest-Mountain-in-Array │ ├── 845. Longest Mountain in Array.go │ ├── 845. Longest Mountain in Array_test.go │ └── README.md ├── 0850.Rectangle-Area-II │ ├── 850. Rectangle Area II.go │ ├── 850. Rectangle Area II_test.go │ └── README.md ├── 0851.Loud-and-Rich │ ├── 851. Loud and Rich.go │ ├── 851. Loud and Rich_test.go │ └── README.md ├── 0852.Peak-Index-in-a-Mountain-Array │ ├── 852. Peak Index in a Mountain Array.go │ ├── 852. Peak Index in a Mountain Array_test.go │ └── README.md ├── 0853.Car-Fleet │ ├── 853. Car Fleet.go │ ├── 853. Car Fleet_test.go │ └── README.md ├── 0856.Score-of-Parentheses │ ├── 856. Score of Parentheses.go │ ├── 856. Score of Parentheses_test.go │ └── README.md ├── 0862.Shortest-Subarray-with-Sum-at-Least-K │ ├── 862. Shortest Subarray with Sum at Least K.go │ ├── 862. Shortest Subarray with Sum at Least K_test.go │ └── README.md ├── 0863.All-Nodes-Distance-K-in-Binary-Tree │ ├── 863. All Nodes Distance K in Binary Tree.go │ ├── 863. All Nodes Distance K in Binary Tree_test.go │ └── README.md ├── 0864.Shortest-Path-to-Get-All-Keys │ ├── 864. Shortest Path to Get All Keys.go │ ├── 864. Shortest Path to Get All Keys_test.go │ └── README.md ├── 0867.Transpose-Matrix │ ├── 867. Transpose Matrix.go │ ├── 867. Transpose Matrix_test.go │ └── README.md ├── 0872.Leaf-Similar-Trees │ ├── 872. Leaf-Similar Trees.go │ ├── 872. Leaf-Similar Trees_test.go │ └── README.md ├── 0875.Koko-Eating-Bananas │ ├── 875. Koko Eating Bananas.go │ ├── 875. Koko Eating Bananas_test.go │ └── README.md ├── 0876.Middle-of-the-Linked-List │ ├── 876. Middle of the Linked List.go │ ├── 876. Middle of the Linked List_test.go │ └── README.md ├── 0878.Nth-Magical-Number │ ├── 878. Nth Magical Number.go │ ├── 878. Nth Magical Number_test.go │ └── README.md ├── 0880.Decoded-String-at-Index │ ├── 880. Decoded String at Index.go │ ├── 880. Decoded String at Index_test.go │ └── README.md ├── 0881.Boats-to-Save-People │ ├── 881. Boats to Save People.go │ ├── 881. Boats to Save People_test.go │ └── README.md ├── 0884.Uncommon-Words-from-Two-Sentences │ ├── 884. Uncommon Words from Two Sentences.go │ ├── 884. Uncommon Words from Two Sentences_test.go │ └── README.md ├── 0885.Spiral-Matrix-III │ ├── 885. Spiral Matrix III.go │ ├── 885. Spiral Matrix III_test.go │ └── README.md ├── 0887.Super-Egg-Drop │ ├── 887. Super Egg Drop.go │ ├── 887. Super Egg Drop_test.go │ └── README.md ├── 0888.Fair-Candy-Swap │ ├── 888. Fair Candy Swap.go │ ├── 888. Fair Candy Swap_test.go │ └── README.md ├── 0891.Sum-of-Subsequence-Widths │ ├── 891. Sum of Subsequence Widths.go │ ├── 891. Sum of Subsequence Widths_test.go │ └── README.md ├── 0892.Surface-Area-of-3D-Shapes │ ├── 892. Surface Area of 3D Shapes.go │ ├── 892. Surface Area of 3D Shapes_test.go │ └── README.md ├── 0895.Maximum-Frequency-Stack │ ├── 895. Maximum Frequency Stack.go │ ├── 895. Maximum Frequency Stack_test.go │ └── README.md ├── 0896.Monotonic-Array │ ├── 896. Monotonic Array.go │ ├── 896. Monotonic Array_test.go │ └── README.md ├── 0897.Increasing-Order-Search-Tree │ ├── 897. Increasing Order Search Tree.go │ ├── 897. Increasing Order Search Tree_test.go │ └── README.md ├── 0898.Bitwise-ORs-of-Subarrays │ ├── 898. Bitwise ORs of Subarrays.go │ ├── 898. Bitwise ORs of Subarrays_test.go │ └── README.md ├── 0901.Online-Stock-Span │ ├── 901. Online Stock Span.go │ ├── 901. Online Stock Span_test.go │ └── README.md ├── 0904.Fruit-Into-Baskets │ ├── 904. Fruit Into Baskets.go │ ├── 904. Fruit Into Baskets_test.go │ └── README.md ├── 0907.Sum-of-Subarray-Minimums │ ├── 907. Sum of Subarray Minimums.go │ ├── 907. Sum of Subarray Minimums_test.go │ └── README.md ├── 0911.Online-Election │ ├── 911. Online Election.go │ ├── 911. Online Election_test.go │ └── README.md ├── 0914.X-of-a-Kind-in-a-Deck-of-Cards │ ├── 914. X of a Kind in a Deck of Cards.go │ ├── 914. X of a Kind in a Deck of Cards_test.go │ └── README.md ├── 0918.Maximum-Sum-Circular-Subarray │ ├── 918. Maximum Sum Circular Subarray.go │ ├── 918. Maximum Sum Circular Subarray_test.go │ └── README.md ├── 0920.Number-of-Music-Playlists │ ├── 920. Number of Music Playlists.go │ ├── 920. Number of Music Playlists_test.go │ └── README.md ├── 0921.Minimum-Add-to-Make-Parentheses-Valid │ ├── 921. Minimum Add to Make Parentheses Valid.go │ ├── 921. Minimum Add to Make Parentheses Valid_test.go │ └── README.md ├── 0922.Sort-Array-By-Parity-II │ ├── 922. Sort Array By Parity II.go │ ├── 922. Sort Array By Parity II_test.go │ └── README.md ├── 0923.3Sum-With-Multiplicity │ ├── 923. 3Sum With Multiplicity.go │ ├── 923. 3Sum With Multiplicity_test.go │ └── README.md ├── 0924.Minimize-Malware-Spread │ ├── 924. Minimize Malware Spread.go │ ├── 924. Minimize Malware Spread_test.go │ └── README.md ├── 0925.Long-Pressed-Name │ ├── 925. Long Pressed Name.go │ ├── 925. Long Pressed Name_test.go │ └── README.md ├── 0927.Three-Equal-Parts │ ├── 927. Three Equal Parts.go │ ├── 927. Three Equal Parts_test.go │ └── README.md ├── 0928.Minimize-Malware-Spread-II │ ├── 928. Minimize Malware Spread II.go │ ├── 928. Minimize Malware Spread II_test.go │ └── README.md ├── 0930.Binary-Subarrays-With-Sum │ ├── 930. Binary Subarrays With Sum.go │ ├── 930. Binary Subarrays With Sum_test.go │ └── README.md ├── 0933.Number-of-Recent-Calls │ ├── 933. Number of Recent Calls.go │ ├── 933. Number of Recent Calls_test.go │ └── README.md ├── 0942.DI-String-Match │ ├── 942. DI String Match.go │ ├── 942. DI String Match_test.go │ └── README.md ├── 0946.Validate-Stack-Sequences │ ├── 946. Validate Stack Sequences.go │ ├── 946. Validate Stack Sequences_test.go │ └── README.md ├── 0947.Most-Stones-Removed-with-Same-Row-or-Column │ ├── 947. Most Stones Removed with Same Row or Column.go │ ├── 947. Most Stones Removed with Same Row or Column_test.go │ └── README.md ├── 0949.Largest-Time-for-Given-Digits │ ├── 949. Largest Time for Given Digits.go │ ├── 949. Largest Time for Given Digits_test.go │ └── README.md ├── 0952.Largest-Component-Size-by-Common-Factor │ ├── 952. Largest Component Size by Common Factor.go │ ├── 952. Largest Component Size by Common Factor_test.go │ └── README.md ├── 0953.Verifying-an-Alien-Dictionary │ ├── 953. Verifying an Alien Dictionary.go │ ├── 953. Verifying an Alien Dictionary_test.go │ └── README.md ├── 0959.Regions-Cut-By-Slashes │ ├── 959. Regions Cut By Slashes.go │ ├── 959. Regions Cut By Slashes_test.go │ └── README.md ├── 0961.N-Repeated-Element-in-Size-2N-Array │ ├── 961. N-Repeated Element in Size 2N Array.go │ ├── 961. N-Repeated Element in Size 2N Array_test.go │ └── README.md ├── 0968.Binary-Tree-Cameras │ ├── 968. Binary Tree Cameras.go │ ├── 968. Binary Tree Cameras_test.go │ └── README.md ├── 0969.Pancake-Sorting │ ├── 969. Pancake Sorting.go │ ├── 969. Pancake Sorting_test.go │ └── README.md ├── 0970.Powerful-Integers │ ├── 970. Powerful Integers.go │ ├── 970. Powerful Integers_test.go │ └── README.md ├── 0973.K-Closest-Points-to-Origin │ ├── 973. K Closest Points to Origin.go │ ├── 973. K Closest Points to Origin_test.go │ └── README.md ├── 0976.Largest-Perimeter-Triangle │ ├── 976. Largest Perimeter Triangle.go │ ├── 976. Largest Perimeter Triangle_test.go │ └── README.md ├── 0977.Squares-of-a-Sorted-Array │ ├── 977. Squares of a Sorted Array.go │ ├── 977. Squares of a Sorted Array_test.go │ └── README.md ├── 0978.Longest-Turbulent-Subarray │ ├── 978. Longest Turbulent Subarray.go │ ├── 978. Longest Turbulent Subarray_test.go │ └── README.md ├── 0979.Distribute-Coins-in-Binary-Tree │ ├── 979. Distribute Coins in Binary Tree.go │ ├── 979. Distribute Coins in Binary Tree_test.go │ └── README.md ├── 0980.Unique-Paths-III │ ├── 980. Unique Paths III.go │ ├── 980. Unique Paths III_test.go │ └── README.md ├── 0981.Time-Based-Key-Value-Store │ ├── 981. Time Based Key-Value Store.go │ ├── 981. Time Based Key-Value Store_test.go │ └── README.md ├── 0984.String-Without-AAA-or-BBB │ ├── 984. String Without AAA or BBB.go │ ├── 984. String Without AAA or BBB_test.go │ └── README.md ├── 0985.Sum-of-Even-Numbers-After-Queries │ ├── 985. Sum of Even Numbers After Queries.go │ ├── 985. Sum of Even Numbers After Queries_test.go │ └── README.md ├── 0986.Interval-List-Intersections │ ├── 986. Interval List Intersections.go │ ├── 986. Interval List Intersections_test.go │ └── README.md ├── 0990.Satisfiability-of-Equality-Equations │ ├── 990. Satisfiability of Equality Equations.go │ ├── 990. Satisfiability of Equality Equations_test.go │ └── README.md ├── 0992.Subarrays-with-K-Different-Integers │ ├── 992. Subarrays with K Different Integers.go │ ├── 992. Subarrays with K Different Integers_test.go │ └── README.md ├── 0993.Cousins-in-Binary-Tree │ ├── 993. Cousins in Binary Tree.go │ ├── 993. Cousins in Binary Tree_test.go │ └── README.md ├── 0995.Minimum-Number-of-K-Consecutive-Bit-Flips │ ├── 995. Minimum Number of K Consecutive Bit Flips.go │ ├── 995. Minimum Number of K Consecutive Bit Flips_test.go │ └── README.md ├── 0996.Number-of-Squareful-Arrays │ ├── 996. Number of Squareful Arrays.go │ ├── 996. Number of Squareful Arrays_test.go │ └── README.md ├── 0999.Available-Captures-for-Rook │ ├── 999. Available Captures for Rook.go │ ├── 999. Available Captures for Rook_test.go │ └── README.md ├── 1002.Find-Common-Characters │ ├── 1002. Find Common Characters.go │ ├── 1002. Find Common Characters_test.go │ └── README.md ├── 1003.Check-If-Word-Is-Valid-After-Substitutions │ ├── 1003. Check If Word Is Valid After Substitutions.go │ ├── 1003. Check If Word Is Valid After Substitutions_test.go │ └── README.md ├── 1004.Max-Consecutive-Ones-III │ ├── 1004. Max Consecutive Ones III.go │ ├── 1004. Max Consecutive Ones III_test.go │ └── README.md ├── 1005.Maximize-Sum-Of-Array-After-K-Negations │ ├── 1005. Maximize Sum Of Array After K Negations.go │ ├── 1005. Maximize Sum Of Array After K Negations_test.go │ └── README.md ├── 1011.Capacity-To-Ship-Packages-Within-D-Days │ ├── 1011. Capacity To Ship Packages Within D Days.go │ ├── 1011. Capacity To Ship Packages Within D Days_test.go │ └── README.md ├── 1017.Convert-to-Base--2 │ ├── 1017. Convert to Base -2.go │ ├── 1017. Convert to Base -2_test.go │ └── README.md ├── 1019.Next-Greater-Node-In-Linked-List │ ├── 1019. Next Greater Node In Linked List.go │ ├── 1019. Next Greater Node In Linked List_test.go │ └── README.md ├── 1020.Number-of-Enclaves │ ├── 1020. Number of Enclaves.go │ ├── 1020. Number of Enclaves_test.go │ └── README.md ├── 1021.Remove-Outermost-Parentheses │ ├── 1021. Remove Outermost Parentheses.go │ ├── 1021. Remove Outermost Parentheses_test.go │ └── README.md ├── 1025.Divisor-Game │ ├── 1025. Divisor Game.go │ ├── 1025. Divisor Game_test.go │ └── README.md ├── 1026.Maximum-Difference-Between-Node-and-Ancestor │ ├── 1026. Maximum Difference Between Node and Ancestor.go │ ├── 1026. Maximum Difference Between Node and Ancestor_test.go │ └── README.md ├── 1028.Recover-a-Tree-From-Preorder-Traversal │ ├── 1028. Recover a Tree From Preorder Traversal.go │ ├── 1028. Recover a Tree From Preorder Traversal_test.go │ └── README.md ├── 1030.Matrix-Cells-in-Distance-Order │ ├── 1030. Matrix Cells in Distance Order.go │ ├── 1030. Matrix Cells in Distance Order_test.go │ └── README.md ├── 1037.Valid-Boomerang │ ├── 1037. Valid Boomerang.go │ ├── 1037. Valid Boomerang_test.go │ └── README.md ├── 1040.Moving-Stones-Until-Consecutive-II │ ├── 1040. Moving Stones Until Consecutive II.go │ ├── 1040. Moving Stones Until Consecutive II_test.go │ └── README.md ├── 1047.Remove-All-Adjacent-Duplicates-In-String │ ├── 1047. Remove All Adjacent Duplicates In String.go │ ├── 1047. Remove All Adjacent Duplicates In String_test.go │ └── README.md ├── 1049.Last-Stone-Weight-II │ ├── 1049. Last Stone Weight II.go │ ├── 1049. Last Stone Weight II_test.go │ └── README.md ├── 1051.Height-Checker │ ├── 1051. Height Checker.go │ ├── 1051. Height Checker_test.go │ └── README.md ├── 1052.Grumpy-Bookstore-Owner │ ├── 1052. Grumpy Bookstore Owner.go │ ├── 1052. Grumpy Bookstore Owner_test.go │ └── README.md ├── 1054.Distant-Barcodes │ ├── 1054. Distant Barcodes.go │ ├── 1054. Distant Barcodes_test.go │ └── README.md ├── 1073.Adding-Two-Negabinary-Numbers │ ├── 1073. Adding Two Negabinary Numbers.go │ ├── 1073. Adding Two Negabinary Numbers_test.go │ └── README.md ├── 1074.Number-of-Submatrices-That-Sum-to-Target │ ├── 1074. Number of Submatrices That Sum to Target.go │ ├── 1074. Number of Submatrices That Sum to Target_test.go │ └── README.md ├── 1078.Occurrences-After-Bigram │ ├── 1078. Occurrences After Bigram.go │ ├── 1078. Occurrences After Bigram_test.go │ └── README.md ├── 1079.Letter-Tile-Possibilities │ ├── 1079. Letter Tile Possibilities.go │ ├── 1079. Letter Tile Possibilities_test.go │ └── README.md ├── 1089.Duplicate-Zeros │ ├── 1089. Duplicate Zeros.go │ ├── 1089. Duplicate Zeros_test.go │ └── README.md ├── 1093.Statistics-from-a-Large-Sample │ ├── 1093. Statistics from a Large Sample.go │ ├── 1093. Statistics from a Large Sample_test.go │ └── README.md ├── 1105.Filling-Bookcase-Shelves │ ├── 1105. Filling Bookcase Shelves.go │ ├── 1105. Filling Bookcase Shelves_test.go │ └── README.md ├── 1108.Defanging-an-IP-Address │ ├── 1108. Defanging an IP Address.go │ ├── 1108. Defanging an IP Address_test.go │ └── README.md ├── 1110.Delete-Nodes-And-Return-Forest │ ├── 1110. Delete Nodes And Return Forest.go │ ├── 1110. Delete Nodes And Return Forest_test.go │ └── README.md ├── 1111.Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings │ ├── 1111. Maximum Nesting Depth of Two Valid Parentheses Strings.go │ ├── 1111. Maximum Nesting Depth of Two Valid Parentheses Strings_test.go │ └── README.md ├── 1122.Relative-Sort-Array │ ├── 1122. Relative Sort Array.go │ ├── 1122. Relative Sort Array_test.go │ └── README.md ├── 1123.Lowest-Common-Ancestor-of-Deepest-Leaves │ ├── 1123. Lowest Common Ancestor of Deepest Leaves.go │ ├── 1123. Lowest Common Ancestor of Deepest Leaves_test.go │ └── README.md ├── 1128.Number-of-Equivalent-Domino-Pairs │ ├── 1128. Number of Equivalent Domino Pairs.go │ ├── 1128. Number of Equivalent Domino Pairs_test.go │ └── README.md ├── 1137.N-th-Tribonacci-Number │ ├── 1137. N-th Tribonacci Number.go │ ├── 1137. N-th Tribonacci Number_test.go │ └── README.md ├── 1145.Binary-Tree-Coloring-Game │ ├── 1145. Binary Tree Coloring Game.go │ ├── 1145. Binary Tree Coloring Game_test.go │ └── README.md ├── 1154.Day-of-the-Year │ ├── 1154. Day of the Year.go │ ├── 1154. Day of the Year_test.go │ └── README.md ├── 1157.Online-Majority-Element-In-Subarray │ ├── 1157. Online Majority Element In Subarray.go │ ├── 1157. Online Majority Element In Subarray_test.go │ └── README.md ├── 1160.Find-Words-That-Can-Be-Formed-by-Characters │ ├── 1160. Find Words That Can Be Formed by Characters.go │ ├── 1160. Find Words That Can Be Formed by Characters_test.go │ └── README.md ├── 1170.Compare-Strings-by-Frequency-of-the-Smallest-Character │ ├── 1170. Compare Strings by Frequency of the Smallest Character.go │ ├── 1170. Compare Strings by Frequency of the Smallest Character_test.go │ └── README.md ├── 1171.Remove-Zero-Sum-Consecutive-Nodes-from-Linked-List │ ├── 1171. Remove Zero Sum Consecutive Nodes from Linked List.go │ ├── 1171. Remove Zero Sum Consecutive Nodes from Linked List_test.go │ └── README.md ├── 1175.Prime-Arrangements │ ├── 1175. Prime Arrangements.go │ ├── 1175. Prime Arrangements_test.go │ └── README.md ├── 1184.Distance-Between-Bus-Stops │ ├── 1184. Distance Between Bus Stops.go │ ├── 1184. Distance Between Bus Stops_test.go │ └── README.md ├── 1185.Day-of-the-Week │ ├── 1185. Day of the Week.go │ ├── 1185. Day of the Week_test.go │ └── README.md ├── 1189.Maximum-Number-of-Balloons │ ├── 1189. Maximum Number of Balloons.go │ ├── 1189. Maximum Number of Balloons_test.go │ └── README.md ├── 1200.Minimum-Absolute-Difference │ ├── 1200. Minimum Absolute Difference.go │ ├── 1200. Minimum Absolute Difference_test.go │ └── README.md ├── 1201.Ugly-Number-III │ ├── 1201. Ugly Number III.go │ ├── 1201. Ugly Number III_test.go │ └── README.md ├── 1202.Smallest-String-With-Swaps │ ├── 1202. Smallest String With Swaps.go │ ├── 1202. Smallest String With Swaps_test.go │ └── README.md ├── 1207.Unique-Number-of-Occurrences │ ├── 1207. Unique Number of Occurrences.go │ ├── 1207. Unique Number of Occurrences_test.go │ └── README.md ├── 1208.Get-Equal-Substrings-Within-Budget │ ├── 1208. Get Equal Substrings Within Budget.go │ ├── 1208. Get Equal Substrings Within Budget_test.go │ └── README.md ├── 1217.Play-with-Chips │ ├── 1217. Play with Chips.go │ ├── 1217. Play with Chips_test.go │ └── README.md ├── 1221.Split-a-String-in-Balanced-Strings │ ├── 1221. Split a String in Balanced Strings.go │ ├── 1221. Split a String in Balanced Strings_test.go │ └── README.md ├── 1232.Check-If-It-Is-a-Straight-Line │ ├── 1232. Check If It Is a Straight Line.go │ ├── 1232. Check If It Is a Straight Line_test.go │ └── README.md ├── 1234.Replace-the-Substring-for-Balanced-String │ ├── 1234. Replace the Substring for Balanced String.go │ ├── 1234. Replace the Substring for Balanced String_test.go │ └── README.md ├── 1235.Maximum-Profit-in-Job-Scheduling │ ├── 1235. Maximum Profit in Job Scheduling.go │ ├── 1235. Maximum Profit in Job Scheduling_test.go │ └── README.md ├── 1252.Cells-with-Odd-Values-in-a-Matrix │ ├── 1252. Cells with Odd Values in a Matrix.go │ ├── 1252. Cells with Odd Values in a Matrix_test.go │ └── README.md ├── 1254.Number-of-Closed-Islands │ ├── 1254. Number of Closed Islands.go │ ├── 1254. Number of Closed Islands_test.go │ └── README.md ├── 1260.Shift-2D-Grid │ ├── 1260. Shift 2D Grid.go │ ├── 1260. Shift 2D Grid_test.go │ └── README.md ├── 1266.Minimum-Time-Visiting-All-Points │ ├── 1266. Minimum Time Visiting All Points.go │ ├── 1266. Minimum Time Visiting All Points_test.go │ └── README.md ├── 1275.Find-Winner-on-a-Tic-Tac-Toe-Game │ ├── 1275. Find Winner on a Tic Tac Toe Game.go │ ├── 1275. Find Winner on a Tic Tac Toe Game_test.go │ └── README.md ├── 1281.Subtract-the-Product-and-Sum-of-Digits-of-an-Integer │ ├── 1281. Subtract the Product and Sum of Digits of an Integer.go │ ├── 1281. Subtract the Product and Sum of Digits of an Integer_test.go │ └── README.md ├── 1283.Find-the-Smallest-Divisor-Given-a-Threshold │ ├── 1283. Find the Smallest Divisor Given a Threshold.go │ ├── 1283. Find the Smallest Divisor Given a Threshold_test.go │ └── README.md ├── 1287.Element-Appearing-More-Than-In-Sorted-Array │ ├── 1287. Element Appearing More Than 25% In Sorted Array.go │ ├── 1287. Element Appearing More Than 25% In Sorted Array_test.go │ └── README.md ├── 1290.Convert-Binary-Number-in-a-Linked-List-to-Integer │ ├── 1290. Convert Binary Number in a Linked List to Integer.go │ ├── 1290. Convert Binary Number in a Linked List to Integer_test.go │ └── README.md ├── 1295.Find-Numbers-with-Even-Number-of-Digits │ ├── 1295. Find Numbers with Even Number of Digits.go │ ├── 1295. Find Numbers with Even Number of Digits_test.go │ └── README.md ├── 1299.Replace-Elements-with-Greatest-Element-on-Right-Side │ ├── 1299. Replace Elements with Greatest Element on Right Side.go │ ├── 1299. Replace Elements with Greatest Element on Right Side_test.go │ └── README.md ├── 1300.Sum-of-Mutated-Array-Closest-to-Target │ ├── 1300. Sum of Mutated Array Closest to Target.go │ ├── 1300. Sum of Mutated Array Closest to Target_test.go │ └── README.md ├── 1302.Deepest-Leaves-Sum │ ├── 1302. Deepest Leaves Sum.go │ ├── 1302. Deepest Leaves Sum_test.go │ └── README.md ├── 1304.Find-N-Unique-Integers-Sum-up-to-Zero │ ├── 1304. Find N Unique Integers Sum up to Zero.go │ ├── 1304. Find N Unique Integers Sum up to Zero_test.go │ └── README.md ├── 1305.All-Elements-in-Two-Binary-Search-Trees │ ├── 1305. All Elements in Two Binary Search Trees.go │ ├── 1305. All Elements in Two Binary Search Trees_test.go │ └── README.md ├── 1306.Jump-Game-III │ ├── 1306. Jump Game III.go │ ├── 1306. Jump Game III_test.go │ └── README.md ├── 1313.Decompress-Run-Length-Encoded-List │ ├── 1313. Decompress Run-Length Encoded List.go │ ├── 1313. Decompress Run-Length Encoded List_test.go │ └── README.md ├── 1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers │ ├── 1317. Convert Integer to the Sum of Two No-Zero Integers.go │ ├── 1317. Convert Integer to the Sum of Two No-Zero Integers_test.go │ └── README.md ├── 1380.Lucky-Numbers-in-a-Matrix │ ├── 1380. Lucky Numbers in a Matrix.go │ ├── 1380. Lucky Numbers in a Matrix_test.go │ └── README.md ├── 1385.Find-the-Distance-Value-Between-Two-Arrays │ ├── 1385. Find the Distance Value Between Two Arrays.go │ ├── 1385. Find the Distance Value Between Two Arrays_test.go │ └── README.md ├── 1389.Create-Target-Array-in-the-Given-Order │ ├── 1389. Create Target Array in the Given Order.go │ ├── 1389. Create Target Array in the Given Order_test.go │ └── README.md ├── 1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence │ ├── 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence.go │ ├── 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence_test.go │ └── README.md ├── 1464.Maximum-Product-of-Two-Elements-in-an-Array │ ├── 1464. Maximum Product of Two Elements in an Array.go │ ├── 1464. Maximum Product of Two Elements in an Array_test.go │ └── README.md ├── 1470.Shuffle-the-Array │ ├── 1470. Shuffle the Array.go │ ├── 1470. Shuffle the Array_test.go │ └── README.md ├── 9990085.Maximal-Rectangle │ ├── 85. Maximal Rectangle.go │ └── 85. Maximal Rectangle_test.go ├── 9990132.Palindrome-Partitioning-II │ ├── 132. Palindrome Partitioning II.go │ └── 132. Palindrome Partitioning II_test.go ├── 9990316.Remove-Duplicate-Letters │ ├── 316. Remove Duplicate Letters.go │ └── 316. Remove Duplicate Letters_test.go ├── 9990352.Data-Stream-as-Disjoint-Intervals │ ├── 352. Data Stream as Disjoint Intervals.go │ └── 352. Data Stream as Disjoint Intervals_test.go ├── 9990363.Max-Sum-of-Rectangle-No-Larger-Than-K │ ├── 363. Max Sum of Rectangle No Larger Than K.go │ └── 363. Max Sum of Rectangle No Larger Than K_test.go ├── 9990377.Combination-Sum-IV │ ├── 377. Combination Sum IV.go │ └── 377. Combination Sum IV_test.go ├── 9990975.Odd-Even-Jump │ ├── 975. Odd Even Jump.go │ └── 975. Odd Even Jump_test.go ├── 9991044.Longest-Duplicate-Substring │ ├── 1044. Longest Duplicate Substring.go │ └── 1044. Longest Duplicate Substring_test.go └── 9991292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold │ ├── 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold.go │ └── 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_test.go ├── logo.png ├── note ├── grokking_algorithms.md └── 时间复杂度.md ├── structures ├── Heap.go ├── Heap_test.go ├── Interval.go ├── Interval_test.go ├── ListNode.go ├── ListNode_test.go ├── NestedInteger.go ├── NestedInterger_test.go ├── Point.go ├── Point_test.go ├── PriorityQueue.go ├── PriorityQueue_test.go ├── Queue.go ├── Queue_test.go ├── Stack.go ├── Stack_test.go ├── TreeNode.go └── TreeNode_test.go ├── template ├── SegmentTree.go └── UnionFind.go ├── topic ├── Backtracking.png ├── Binary_Indexed_Tree.png ├── Bit_Manipulation.png ├── Linked_List.png ├── Segment_Tree.png ├── Sliding_Window.png ├── Sort.png ├── Stack.png ├── Two_pointers.png └── Union_Find.png └── website ├── archetypes └── default.md ├── config.toml ├── content ├── ChapterFour │ ├── 0001.Two-Sum.md │ ├── 0002.Add-Two-Numbers.md │ ├── 0003.Longest-Substring-Without-Repeating-Characters.md │ ├── 0004.Median-of-Two-Sorted-Arrays.md │ ├── 0007.Reverse-Integer.md │ ├── 0009.Palindrome-Number.md │ ├── 0011.Container-With-Most-Water.md │ ├── 0013.Roman-to-Integer.md │ ├── 0015.3Sum.md │ ├── 0016.3Sum-Closest.md │ ├── 0017.Letter-Combinations-of-a-Phone-Number.md │ ├── 0018.4Sum.md │ ├── 0019.Remove-Nth-Node-From-End-of-List.md │ ├── 0020.Valid-Parentheses.md │ ├── 0021.Merge-Two-Sorted-Lists.md │ ├── 0022.Generate-Parentheses.md │ ├── 0023.Merge-k-Sorted-Lists.md │ ├── 0024.Swap-Nodes-in-Pairs.md │ ├── 0025.Reverse-Nodes-in-k-Group.md │ ├── 0026.Remove-Duplicates-from-Sorted-Array.md │ ├── 0027.Remove-Element.md │ ├── 0028.Implement-strStr.md │ ├── 0029.Divide-Two-Integers.md │ ├── 0030.Substring-with-Concatenation-of-All-Words.md │ ├── 0033.Search-in-Rotated-Sorted-Array.md │ ├── 0034.Find-First-and-Last-Position-of-Element-in-Sorted-Array.md │ ├── 0035.Search-Insert-Position.md │ ├── 0036.Valid-Sudoku.md │ ├── 0037.Sudoku-Solver.md │ ├── 0039.Combination-Sum.md │ ├── 0040.Combination-Sum-II.md │ ├── 0041.First-Missing-Positive.md │ ├── 0042.Trapping-Rain-Water.md │ ├── 0046.Permutations.md │ ├── 0047.Permutations-II.md │ ├── 0048.Rotate-Image.md │ ├── 0049.Group-Anagrams.md │ ├── 0050.Powx-n.md │ ├── 0051.N-Queens.md │ ├── 0052.N-Queens-II.md │ ├── 0053.Maximum-Subarray.md │ ├── 0054.Spiral-Matrix.md │ ├── 0055.Jump-Game.md │ ├── 0056.Merge-Intervals.md │ ├── 0057.Insert-Interval.md │ ├── 0059.Spiral-Matrix-II.md │ ├── 0060.Permutation-Sequence.md │ ├── 0061.Rotate-List.md │ ├── 0062.Unique-Paths.md │ ├── 0063.Unique-Paths-II.md │ ├── 0064.Minimum-Path-Sum.md │ ├── 0066.Plus-One.md │ ├── 0067.Add-Binary.md │ ├── 0069.Sqrtx.md │ ├── 0070.Climbing-Stairs.md │ ├── 0071.Simplify-Path.md │ ├── 0074.Search-a-2D-Matrix.md │ ├── 0075.Sort-Colors.md │ ├── 0076.Minimum-Window-Substring.md │ ├── 0077.Combinations.md │ ├── 0078.Subsets.md │ ├── 0079.Word-Search.md │ ├── 0080.Remove-Duplicates-from-Sorted-Array-II.md │ ├── 0081.Search-in-Rotated-Sorted-Array-II.md │ ├── 0082.Remove-Duplicates-from-Sorted-List-II.md │ ├── 0083.Remove-Duplicates-from-Sorted-List.md │ ├── 0084.Largest-Rectangle-in-Histogram.md │ ├── 0086.Partition-List.md │ ├── 0088.Merge-Sorted-Array.md │ ├── 0089.Gray-Code.md │ ├── 0090.Subsets-II.md │ ├── 0091.Decode-Ways.md │ ├── 0092.Reverse-Linked-List-II.md │ ├── 0093.Restore-IP-Addresses.md │ ├── 0094.Binary-Tree-Inorder-Traversal.md │ ├── 0095.Unique-Binary-Search-Trees-II.md │ ├── 0096.Unique-Binary-Search-Trees.md │ ├── 0098.Validate-Binary-Search-Tree.md │ ├── 0099.Recover-Binary-Search-Tree.md │ ├── 0100.Same-Tree.md │ ├── 0101.Symmetric-Tree.md │ ├── 0102.Binary-Tree-Level-Order-Traversal.md │ ├── 0103.Binary-Tree-Zigzag-Level-Order-Traversal.md │ ├── 0104.Maximum-Depth-of-Binary-Tree.md │ ├── 0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.md │ ├── 0106.Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal.md │ ├── 0107.Binary-Tree-Level-Order-Traversal-II.md │ ├── 0108.Convert-Sorted-Array-to-Binary-Search-Tree.md │ ├── 0109.Convert-Sorted-List-to-Binary-Search-Tree.md │ ├── 0110.Balanced-Binary-Tree.md │ ├── 0111.Minimum-Depth-of-Binary-Tree.md │ ├── 0112.Path-Sum.md │ ├── 0113.Path-Sum-II.md │ ├── 0114.Flatten-Binary-Tree-to-Linked-List.md │ ├── 0118.Pascals-Triangle.md │ ├── 0120.Triangle.md │ ├── 0121.Best-Time-to-Buy-and-Sell-Stock.md │ ├── 0122.Best-Time-to-Buy-and-Sell-Stock-II.md │ ├── 0124.Binary-Tree-Maximum-Path-Sum.md │ ├── 0125.Valid-Palindrome.md │ ├── 0126.Word-Ladder-II.md │ ├── 0127.Word-Ladder.md │ ├── 0128.Longest-Consecutive-Sequence.md │ ├── 0129.Sum-Root-to-Leaf-Numbers.md │ ├── 0130.Surrounded-Regions.md │ ├── 0131.Palindrome-Partitioning.md │ ├── 0136.Single-Number.md │ ├── 0137.Single-Number-II.md │ ├── 0141.Linked-List-Cycle.md │ ├── 0142.Linked-List-Cycle-II.md │ ├── 0143.Reorder-List.md │ ├── 0144.Binary-Tree-Preorder-Traversal.md │ ├── 0145.Binary-Tree-Postorder-Traversal.md │ ├── 0147.Insertion-Sort-List.md │ ├── 0148.Sort-List.md │ ├── 0150.Evaluate-Reverse-Polish-Notation.md │ ├── 0151.Reverse-Words-in-a-String.md │ ├── 0152.Maximum-Product-Subarray.md │ ├── 0153.Find-Minimum-in-Rotated-Sorted-Array.md │ ├── 0154.Find-Minimum-in-Rotated-Sorted-Array-II.md │ ├── 0155.Min-Stack.md │ ├── 0160.Intersection-of-Two-Linked-Lists.md │ ├── 0162.Find-Peak-Element.md │ ├── 0164.Maximum-Gap.md │ ├── 0167.Two-Sum-II---Input-array-is-sorted.md │ ├── 0168.Excel-Sheet-Column-Title.md │ ├── 0169.Majority-Element.md │ ├── 0171.Excel-Sheet-Column-Number.md │ ├── 0172.Factorial-Trailing-Zeroes.md │ ├── 0173.Binary-Search-Tree-Iterator.md │ ├── 0174.Dungeon-Game.md │ ├── 0179.Largest-Number.md │ ├── 0187.Repeated-DNA-Sequences.md │ ├── 0190.Reverse-Bits.md │ ├── 0191.Number-of-1-Bits.md │ ├── 0198.House-Robber.md │ ├── 0199.Binary-Tree-Right-Side-View.md │ ├── 0200.Number-of-Islands.md │ ├── 0201.Bitwise-AND-of-Numbers-Range.md │ ├── 0202.Happy-Number.md │ ├── 0203.Remove-Linked-List-Elements.md │ ├── 0204.Count-Primes.md │ ├── 0205.Isomorphic-Strings.md │ ├── 0206.Reverse-Linked-List.md │ ├── 0207.Course-Schedule.md │ ├── 0208.Implement-Trie-Prefix-Tree.md │ ├── 0209.Minimum-Size-Subarray-Sum.md │ ├── 0210.Course-Schedule-II.md │ ├── 0211.Add-and-Search-Word---Data-structure-design.md │ ├── 0212.Word-Search-II.md │ ├── 0213.House-Robber-II.md │ ├── 0215.Kth-Largest-Element-in-an-Array.md │ ├── 0216.Combination-Sum-III.md │ ├── 0217.Contains-Duplicate.md │ ├── 0218.The-Skyline-Problem.md │ ├── 0219.Contains-Duplicate-II.md │ ├── 0220.Contains-Duplicate-III.md │ ├── 0222.Count-Complete-Tree-Nodes.md │ ├── 0223.Rectangle-Area.md │ ├── 0224.Basic-Calculator.md │ ├── 0225.Implement-Stack-using-Queues.md │ ├── 0226.Invert-Binary-Tree.md │ ├── 0229.Majority-Element-II.md │ ├── 0230.Kth-Smallest-Element-in-a-BST.md │ ├── 0231.Power-of-Two.md │ ├── 0232.Implement-Queue-using-Stacks.md │ ├── 0234.Palindrome-Linked-List.md │ ├── 0235.Lowest-Common-Ancestor-of-a-Binary-Search-Tree.md │ ├── 0236.Lowest-Common-Ancestor-of-a-Binary-Tree.md │ ├── 0237.Delete-Node-in-a-Linked-List.md │ ├── 0239.Sliding-Window-Maximum.md │ ├── 0240.Search-a-2D-Matrix-II.md │ ├── 0242.Valid-Anagram.md │ ├── 0257.Binary-Tree-Paths.md │ ├── 0258.Add-Digits.md │ ├── 0260.Single-Number-III.md │ ├── 0263.Ugly-Number.md │ ├── 0268.Missing-Number.md │ ├── 0274.H-Index.md │ ├── 0275.H-Index-II.md │ ├── 0283.Move-Zeroes.md │ ├── 0287.Find-the-Duplicate-Number.md │ ├── 0290.Word-Pattern.md │ ├── 0300.Longest-Increasing-Subsequence.md │ ├── 0303.Range-Sum-Query---Immutable.md │ ├── 0306.Additive-Number.md │ ├── 0307.Range-Sum-Query---Mutable.md │ ├── 0309.Best-Time-to-Buy-and-Sell-Stock-with-Cooldown.md │ ├── 0315.Count-of-Smaller-Numbers-After-Self.md │ ├── 0318.Maximum-Product-of-Word-Lengths.md │ ├── 0322.Coin-Change.md │ ├── 0324.Wiggle-Sort-II.md │ ├── 0326.Power-of-Three.md │ ├── 0327.Count-of-Range-Sum.md │ ├── 0328.Odd-Even-Linked-List.md │ ├── 0329.Longest-Increasing-Path-in-a-Matrix.md │ ├── 0331.Verify-Preorder-Serialization-of-a-Binary-Tree.md │ ├── 0337.House-Robber-III.md │ ├── 0338.Counting-Bits.md │ ├── 0342.Power-of-Four.md │ ├── 0343.Integer-Break.md │ ├── 0344.Reverse-String.md │ ├── 0345.Reverse-Vowels-of-a-String.md │ ├── 0347.Top-K-Frequent-Elements.md │ ├── 0349.Intersection-of-Two-Arrays.md │ ├── 0350.Intersection-of-Two-Arrays-II.md │ ├── 0354.Russian-Doll-Envelopes.md │ ├── 0357.Count-Numbers-with-Unique-Digits.md │ ├── 0367.Valid-Perfect-Square.md │ ├── 0371.Sum-of-Two-Integers.md │ ├── 0372.Super-Pow.md │ ├── 0373.Find-K-Pairs-with-Smallest-Sums.md │ ├── 0378.Kth-Smallest-Element-in-a-Sorted-Matrix.md │ ├── 0385.Mini-Parser.md │ ├── 0386.Lexicographical-Numbers.md │ ├── 0387.First-Unique-Character-in-a-String.md │ ├── 0389.Find-the-Difference.md │ ├── 0392.Is-Subsequence.md │ ├── 0393.UTF-8-Validation.md │ ├── 0394.Decode-String.md │ ├── 0397.Integer-Replacement.md │ ├── 0399.Evaluate-Division.md │ ├── 0401.Binary-Watch.md │ ├── 0402.Remove-K-Digits.md │ ├── 0404.Sum-of-Left-Leaves.md │ ├── 0405.Convert-a-Number-to-Hexadecimal.md │ ├── 0409.Longest-Palindrome.md │ ├── 0410.Split-Array-Largest-Sum.md │ ├── 0412.Fizz-Buzz.md │ ├── 0414.Third-Maximum-Number.md │ ├── 0416.Partition-Equal-Subset-Sum.md │ ├── 0421.Maximum-XOR-of-Two-Numbers-in-an-Array.md │ ├── 0424.Longest-Repeating-Character-Replacement.md │ ├── 0433.Minimum-Genetic-Mutation.md │ ├── 0435.Non-overlapping-Intervals.md │ ├── 0436.Find-Right-Interval.md │ ├── 0437.Path-Sum-III.md │ ├── 0438.Find-All-Anagrams-in-a-String.md │ ├── 0441.Arranging-Coins.md │ ├── 0445.Add-Two-Numbers-II.md │ ├── 0447.Number-of-Boomerangs.md │ ├── 0448.Find-All-Numbers-Disappeared-in-an-Array.md │ ├── 0451.Sort-Characters-By-Frequency.md │ ├── 0453.Minimum-Moves-to-Equal-Array-Elements.md │ ├── 0454.4Sum-II.md │ ├── 0455.Assign-Cookies.md │ ├── 0456.132-Pattern.md │ ├── 0457.Circular-Array-Loop.md │ ├── 0461.Hamming-Distance.md │ ├── 0463.Island-Perimeter.md │ ├── 0470.Implement-Rand10-Using-Rand7.md │ ├── 0474.Ones-and-Zeroes.md │ ├── 0475.Heaters.md │ ├── 0476.Number-Complement.md │ ├── 0477.Total-Hamming-Distance.md │ ├── 0480.Sliding-Window-Median.md │ ├── 0483.Smallest-Good-Base.md │ ├── 0485.Max-Consecutive-Ones.md │ ├── 0491.Increasing-Subsequences.md │ ├── 0493.Reverse-Pairs.md │ ├── 0494.Target-Sum.md │ ├── 0496.Next-Greater-Element-I.md │ ├── 0497.Random-Point-in-Non-overlapping-Rectangles.md │ ├── 0498.Diagonal-Traverse.md │ ├── 0500.Keyboard-Row.md │ ├── 0503.Next-Greater-Element-II.md │ ├── 0507.Perfect-Number.md │ ├── 0508.Most-Frequent-Subtree-Sum.md │ ├── 0509.Fibonacci-Number.md │ ├── 0513.Find-Bottom-Left-Tree-Value.md │ ├── 0515.Find-Largest-Value-in-Each-Tree-Row.md │ ├── 0524.Longest-Word-in-Dictionary-through-Deleting.md │ ├── 0526.Beautiful-Arrangement.md │ ├── 0528.Random-Pick-with-Weight.md │ ├── 0529.Minesweeper.md │ ├── 0532.K-diff-Pairs-in-an-Array.md │ ├── 0537.Complex-Number-Multiplication.md │ ├── 0541.Reverse-String-II.md │ ├── 0542.01-Matrix.md │ ├── 0547.Friend-Circles.md │ ├── 0557.Reverse-Words-in-a-String-III.md │ ├── 0561.Array-Partition-I.md │ ├── 0563.Binary-Tree-Tilt.md │ ├── 0566.Reshape-the-Matrix.md │ ├── 0567.Permutation-in-String.md │ ├── 0572.Subtree-of-Another-Tree.md │ ├── 0575.Distribute-Candies.md │ ├── 0594.Longest-Harmonious-Subsequence.md │ ├── 0598.Range-Addition-II.md │ ├── 0599.Minimum-Index-Sum-of-Two-Lists.md │ ├── 0628.Maximum-Product-of-Three-Numbers.md │ ├── 0632.Smallest-Range-Covering-Elements-from-K-Lists.md │ ├── 0633.Sum-of-Square-Numbers.md │ ├── 0636.Exclusive-Time-of-Functions.md │ ├── 0637.Average-of-Levels-in-Binary-Tree.md │ ├── 0638.Shopping-Offers.md │ ├── 0645.Set-Mismatch.md │ ├── 0648.Replace-Words.md │ ├── 0653.Two-Sum-IV---Input-is-a-BST.md │ ├── 0658.Find-K-Closest-Elements.md │ ├── 0661.Image-Smoother.md │ ├── 0662.Maximum-Width-of-Binary-Tree.md │ ├── 0668.Kth-Smallest-Number-in-Multiplication-Table.md │ ├── 0676.Implement-Magic-Dictionary.md │ ├── 0682.Baseball-Game.md │ ├── 0684.Redundant-Connection.md │ ├── 0685.Redundant-Connection-II.md │ ├── 0693.Binary-Number-with-Alternating-Bits.md │ ├── 0695.Max-Area-of-Island.md │ ├── 0697.Degree-of-an-Array.md │ ├── 0699.Falling-Squares.md │ ├── 0704.Binary-Search.md │ ├── 0705.Design-HashSet.md │ ├── 0706.Design-HashMap.md │ ├── 0707.Design-Linked-List.md │ ├── 0710.Random-Pick-with-Blacklist.md │ ├── 0713.Subarray-Product-Less-Than-K.md │ ├── 0714.Best-Time-to-Buy-and-Sell-Stock-with-Transaction-Fee.md │ ├── 0715.Range-Module.md │ ├── 0717.1-bit-and-2-bit-Characters.md │ ├── 0718.Maximum-Length-of-Repeated-Subarray.md │ ├── 0719.Find-K-th-Smallest-Pair-Distance.md │ ├── 0720.Longest-Word-in-Dictionary.md │ ├── 0721.Accounts-Merge.md │ ├── 0724.Find-Pivot-Index.md │ ├── 0725.Split-Linked-List-in-Parts.md │ ├── 0726.Number-of-Atoms.md │ ├── 0729.My-Calendar-I.md │ ├── 0732.My-Calendar-III.md │ ├── 0733.Flood-Fill.md │ ├── 0735.Asteroid-Collision.md │ ├── 0739.Daily-Temperatures.md │ ├── 0744.Find-Smallest-Letter-Greater-Than-Target.md │ ├── 0745.Prefix-and-Suffix-Search.md │ ├── 0746.Min-Cost-Climbing-Stairs.md │ ├── 0748.Shortest-Completing-Word.md │ ├── 0753.Cracking-the-Safe.md │ ├── 0756.Pyramid-Transition-Matrix.md │ ├── 0762.Prime-Number-of-Set-Bits-in-Binary-Representation.md │ ├── 0763.Partition-Labels.md │ ├── 0765.Couples-Holding-Hands.md │ ├── 0766.Toeplitz-Matrix.md │ ├── 0767.Reorganize-String.md │ ├── 0771.Jewels-and-Stones.md │ ├── 0778.Swim-in-Rising-Water.md │ ├── 0781.Rabbits-in-Forest.md │ ├── 0784.Letter-Case-Permutation.md │ ├── 0786.K-th-Smallest-Prime-Fraction.md │ ├── 0793.Preimage-Size-of-Factorial-Zeroes-Function.md │ ├── 0802.Find-Eventual-Safe-States.md │ ├── 0803.Bricks-Falling-When-Hit.md │ ├── 0811.Subdomain-Visit-Count.md │ ├── 0812.Largest-Triangle-Area.md │ ├── 0815.Bus-Routes.md │ ├── 0817.Linked-List-Components.md │ ├── 0819.Most-Common-Word.md │ ├── 0826.Most-Profit-Assigning-Work.md │ ├── 0828.COPYRIGHT-PROBLEM-XXX.md │ ├── 0832.Flipping-an-Image.md │ ├── 0834.Sum-of-Distances-in-Tree.md │ ├── 0836.Rectangle-Overlap.md │ ├── 0838.Push-Dominoes.md │ ├── 0839.Similar-String-Groups.md │ ├── 0841.Keys-and-Rooms.md │ ├── 0842.Split-Array-into-Fibonacci-Sequence.md │ ├── 0844.Backspace-String-Compare.md │ ├── 0845.Longest-Mountain-in-Array.md │ ├── 0850.Rectangle-Area-II.md │ ├── 0851.Loud-and-Rich.md │ ├── 0852.Peak-Index-in-a-Mountain-Array.md │ ├── 0853.Car-Fleet.md │ ├── 0856.Score-of-Parentheses.md │ ├── 0862.Shortest-Subarray-with-Sum-at-Least-K.md │ ├── 0863.All-Nodes-Distance-K-in-Binary-Tree.md │ ├── 0864.Shortest-Path-to-Get-All-Keys.md │ ├── 0867.Transpose-Matrix.md │ ├── 0872.Leaf-Similar-Trees.md │ ├── 0875.Koko-Eating-Bananas.md │ ├── 0876.Middle-of-the-Linked-List.md │ ├── 0878.Nth-Magical-Number.md │ ├── 0880.Decoded-String-at-Index.md │ ├── 0881.Boats-to-Save-People.md │ ├── 0884.Uncommon-Words-from-Two-Sentences.md │ ├── 0885.Spiral-Matrix-III.md │ ├── 0887.Super-Egg-Drop.md │ ├── 0888.Fair-Candy-Swap.md │ ├── 0891.Sum-of-Subsequence-Widths.md │ ├── 0892.Surface-Area-of-3D-Shapes.md │ ├── 0895.Maximum-Frequency-Stack.md │ ├── 0896.Monotonic-Array.md │ ├── 0897.Increasing-Order-Search-Tree.md │ ├── 0898.Bitwise-ORs-of-Subarrays.md │ ├── 0901.Online-Stock-Span.md │ ├── 0904.Fruit-Into-Baskets.md │ ├── 0907.Sum-of-Subarray-Minimums.md │ ├── 0911.Online-Election.md │ ├── 0914.X-of-a-Kind-in-a-Deck-of-Cards.md │ ├── 0918.Maximum-Sum-Circular-Subarray.md │ ├── 0920.Number-of-Music-Playlists.md │ ├── 0921.Minimum-Add-to-Make-Parentheses-Valid.md │ ├── 0922.Sort-Array-By-Parity-II.md │ ├── 0923.3Sum-With-Multiplicity.md │ ├── 0924.Minimize-Malware-Spread.md │ ├── 0925.Long-Pressed-Name.md │ ├── 0927.Three-Equal-Parts.md │ ├── 0928.Minimize-Malware-Spread-II.md │ ├── 0930.Binary-Subarrays-With-Sum.md │ ├── 0933.Number-of-Recent-Calls.md │ ├── 0942.DI-String-Match.md │ ├── 0946.Validate-Stack-Sequences.md │ ├── 0947.Most-Stones-Removed-with-Same-Row-or-Column.md │ ├── 0949.Largest-Time-for-Given-Digits.md │ ├── 0952.Largest-Component-Size-by-Common-Factor.md │ ├── 0953.Verifying-an-Alien-Dictionary.md │ ├── 0959.Regions-Cut-By-Slashes.md │ ├── 0961.N-Repeated-Element-in-Size-2N-Array.md │ ├── 0968.Binary-Tree-Cameras.md │ ├── 0969.Pancake-Sorting.md │ ├── 0970.Powerful-Integers.md │ ├── 0973.K-Closest-Points-to-Origin.md │ ├── 0976.Largest-Perimeter-Triangle.md │ ├── 0977.Squares-of-a-Sorted-Array.md │ ├── 0978.Longest-Turbulent-Subarray.md │ ├── 0979.Distribute-Coins-in-Binary-Tree.md │ ├── 0980.Unique-Paths-III.md │ ├── 0981.Time-Based-Key-Value-Store.md │ ├── 0984.String-Without-AAA-or-BBB.md │ ├── 0985.Sum-of-Even-Numbers-After-Queries.md │ ├── 0986.Interval-List-Intersections.md │ ├── 0990.Satisfiability-of-Equality-Equations.md │ ├── 0992.Subarrays-with-K-Different-Integers.md │ ├── 0993.Cousins-in-Binary-Tree.md │ ├── 0995.Minimum-Number-of-K-Consecutive-Bit-Flips.md │ ├── 0996.Number-of-Squareful-Arrays.md │ ├── 0999.Available-Captures-for-Rook.md │ ├── 1002.Find-Common-Characters.md │ ├── 1003.Check-If-Word-Is-Valid-After-Substitutions.md │ ├── 1004.Max-Consecutive-Ones-III.md │ ├── 1005.Maximize-Sum-Of-Array-After-K-Negations.md │ ├── 1011.Capacity-To-Ship-Packages-Within-D-Days.md │ ├── 1017.Convert-to-Base--2.md │ ├── 1019.Next-Greater-Node-In-Linked-List.md │ ├── 1020.Number-of-Enclaves.md │ ├── 1021.Remove-Outermost-Parentheses.md │ ├── 1025.Divisor-Game.md │ ├── 1026.Maximum-Difference-Between-Node-and-Ancestor.md │ ├── 1028.Recover-a-Tree-From-Preorder-Traversal.md │ ├── 1030.Matrix-Cells-in-Distance-Order.md │ ├── 1037.Valid-Boomerang.md │ ├── 1040.Moving-Stones-Until-Consecutive-II.md │ ├── 1047.Remove-All-Adjacent-Duplicates-In-String.md │ ├── 1049.Last-Stone-Weight-II.md │ ├── 1051.Height-Checker.md │ ├── 1052.Grumpy-Bookstore-Owner.md │ ├── 1054.Distant-Barcodes.md │ ├── 1073.Adding-Two-Negabinary-Numbers.md │ ├── 1074.Number-of-Submatrices-That-Sum-to-Target.md │ ├── 1078.Occurrences-After-Bigram.md │ ├── 1079.Letter-Tile-Possibilities.md │ ├── 1089.Duplicate-Zeros.md │ ├── 1093.Statistics-from-a-Large-Sample.md │ ├── 1105.Filling-Bookcase-Shelves.md │ ├── 1108.Defanging-an-IP-Address.md │ ├── 1110.Delete-Nodes-And-Return-Forest.md │ ├── 1111.Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings.md │ ├── 1122.Relative-Sort-Array.md │ ├── 1123.Lowest-Common-Ancestor-of-Deepest-Leaves.md │ ├── 1128.Number-of-Equivalent-Domino-Pairs.md │ ├── 1137.N-th-Tribonacci-Number.md │ ├── 1145.Binary-Tree-Coloring-Game.md │ ├── 1154.Day-of-the-Year.md │ ├── 1157.Online-Majority-Element-In-Subarray.md │ ├── 1160.Find-Words-That-Can-Be-Formed-by-Characters.md │ ├── 1170.Compare-Strings-by-Frequency-of-the-Smallest-Character.md │ ├── 1171.Remove-Zero-Sum-Consecutive-Nodes-from-Linked-List.md │ ├── 1175.Prime-Arrangements.md │ ├── 1184.Distance-Between-Bus-Stops.md │ ├── 1185.Day-of-the-Week.md │ ├── 1189.Maximum-Number-of-Balloons.md │ ├── 1200.Minimum-Absolute-Difference.md │ ├── 1201.Ugly-Number-III.md │ ├── 1202.Smallest-String-With-Swaps.md │ ├── 1207.Unique-Number-of-Occurrences.md │ ├── 1208.Get-Equal-Substrings-Within-Budget.md │ ├── 1217.Play-with-Chips.md │ ├── 1221.Split-a-String-in-Balanced-Strings.md │ ├── 1232.Check-If-It-Is-a-Straight-Line.md │ ├── 1234.Replace-the-Substring-for-Balanced-String.md │ ├── 1235.Maximum-Profit-in-Job-Scheduling.md │ ├── 1252.Cells-with-Odd-Values-in-a-Matrix.md │ ├── 1254.Number-of-Closed-Islands.md │ ├── 1260.Shift-2D-Grid.md │ ├── 1266.Minimum-Time-Visiting-All-Points.md │ ├── 1275.Find-Winner-on-a-Tic-Tac-Toe-Game.md │ ├── 1281.Subtract-the-Product-and-Sum-of-Digits-of-an-Integer.md │ ├── 1283.Find-the-Smallest-Divisor-Given-a-Threshold.md │ ├── 1287.Element-Appearing-More-Than-25-In-Sorted-Array.md │ ├── 1290.Convert-Binary-Number-in-a-Linked-List-to-Integer.md │ ├── 1295.Find-Numbers-with-Even-Number-of-Digits.md │ ├── 1299.Replace-Elements-with-Greatest-Element-on-Right-Side.md │ ├── 1300.Sum-of-Mutated-Array-Closest-to-Target.md │ ├── 1302.Deepest-Leaves-Sum.md │ ├── 1304.Find-N-Unique-Integers-Sum-up-to-Zero.md │ ├── 1305.All-Elements-in-Two-Binary-Search-Trees.md │ ├── 1306.Jump-Game-III.md │ ├── 1313.Decompress-Run-Length-Encoded-List.md │ ├── 1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers.md │ ├── 1380.Lucky-Numbers-in-a-Matrix.md │ ├── 1385.Find-the-Distance-Value-Between-Two-Arrays.md │ ├── 1389.Create-Target-Array-in-the-Given-Order.md │ ├── 1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence.md │ ├── 1464.Maximum-Product-of-Two-Elements-in-an-Array.md │ ├── 1470.Shuffle-the-Array.md │ ├── _index.md │ └── pytool │ │ ├── AddTOC.py │ │ ├── GenerateIndex.py │ │ ├── GenerateOne.py │ │ └── GetFile.py ├── ChapterOne │ ├── Algorithm.md │ ├── Data_Structure.md │ └── _index.md ├── ChapterThree │ ├── Segment_Tree.md │ ├── UnionFind.md │ └── _index.md ├── ChapterTwo │ ├── Array.md │ ├── Backtracking.md │ ├── Binary_Indexed_Tree.md │ ├── Binary_Search.md │ ├── Bit_Manipulation.md │ ├── Breadth_First_Search.md │ ├── Depth_First_Search.md │ ├── Dynamic_Programming.md │ ├── Hash_Table.md │ ├── Linked_List.md │ ├── Math.md │ ├── Segment_Tree.md │ ├── Sliding_Window.md │ ├── Sort.md │ ├── Stack.md │ ├── String.md │ ├── Tree.md │ ├── Two_Pointers.md │ ├── Union_Find.md │ └── _index.md ├── _index.md ├── docs │ ├── example │ │ ├── _index.md │ │ ├── collapsed │ │ │ ├── 3rd-level │ │ │ │ ├── 4th-level.md │ │ │ │ └── _index.md │ │ │ └── _index.md │ │ ├── hidden.md │ │ └── table-of-contents │ │ │ ├── _index.md │ │ │ ├── with-toc.md │ │ │ └── without-toc.md │ └── shortcodes │ │ ├── _index.md │ │ ├── buttons.md │ │ ├── columns.md │ │ ├── details.md │ │ ├── expand.md │ │ ├── hints.md │ │ ├── katex.md │ │ ├── mermaid.md │ │ ├── section │ │ ├── _index.md │ │ ├── page1.md │ │ └── page2.md │ │ └── tabs.md └── menu │ └── index.md ├── resources └── _gen │ └── assets │ └── scss │ ├── book.scss_50fc8c04e12a2f59027287995557ceff.content │ ├── book.scss_50fc8c04e12a2f59027287995557ceff.json │ └── leetcode │ ├── book.scss_50fc8c04e12a2f59027287995557ceff.content │ └── book.scss_50fc8c04e12a2f59027287995557ceff.json ├── static ├── logo.png ├── prism.css └── prism.js └── themes └── book ├── .github └── workflows │ └── main.yml ├── .gitignore ├── LICENSE ├── README.md ├── archetypes ├── docs.md └── posts.md ├── assets ├── _custom.scss ├── _defaults.scss ├── _fonts.scss ├── _main.scss ├── _markdown.scss ├── _print.scss ├── _shortcodes.scss ├── _utils.scss ├── _variables.scss ├── book.scss ├── manifest.json ├── menu-reset.js ├── normalize.css ├── plugins │ ├── _dark.scss │ ├── _numbered.scss │ └── _scrollbars.scss ├── search-data.js ├── search.js ├── sw-register.js └── sw.js ├── exampleSite ├── assets │ ├── _custom.scss │ └── _variables.scss ├── config.toml ├── config.yaml ├── content.ru │ └── _index.md ├── content.zh │ └── _index.md ├── content │ ├── _index.md │ ├── docs │ │ ├── example │ │ │ ├── _index.md │ │ │ ├── collapsed │ │ │ │ ├── 3rd-level │ │ │ │ │ ├── 4th-level.md │ │ │ │ │ └── _index.md │ │ │ │ └── _index.md │ │ │ ├── hidden.md │ │ │ └── table-of-contents │ │ │ │ ├── _index.md │ │ │ │ ├── with-toc.md │ │ │ │ └── without-toc.md │ │ └── shortcodes │ │ │ ├── _index.md │ │ │ ├── buttons.md │ │ │ ├── columns.md │ │ │ ├── details.md │ │ │ ├── expand.md │ │ │ ├── hints.md │ │ │ ├── katex.md │ │ │ ├── mermaid.md │ │ │ ├── section │ │ │ ├── _index.md │ │ │ ├── page1.md │ │ │ └── page2.md │ │ │ └── tabs.md │ ├── menu │ │ └── index.md │ └── posts │ │ ├── _index.md │ │ ├── creating-a-new-theme.md │ │ ├── goisforlovers.md │ │ ├── hugoisforlovers.md │ │ └── migrate-from-jekyll.md └── resources │ └── _gen │ └── assets │ └── scss │ ├── book.scss_50fc8c04e12a2f59027287995557ceff.content │ ├── book.scss_50fc8c04e12a2f59027287995557ceff.json │ └── example │ ├── book.scss_50fc8c04e12a2f59027287995557ceff.content │ └── book.scss_50fc8c04e12a2f59027287995557ceff.json ├── i18n ├── cn.yaml ├── cs.yaml ├── de.yaml ├── en.yaml ├── es.yaml ├── fr.yaml ├── ja.yaml ├── jp.yaml ├── ko.yaml ├── nb.yaml ├── pt.yaml ├── ru.yaml ├── sv.yaml ├── uk.yaml └── zh.yaml ├── images ├── screenshot.png └── tn.png ├── layouts ├── 404.html ├── _default │ ├── _markup │ │ ├── render-heading.html │ │ ├── render-image.html │ │ └── render-link.html │ ├── baseof.html │ ├── list.html │ └── single.html ├── partials │ └── docs │ │ ├── brand.html │ │ ├── comments.html │ │ ├── date.html │ │ ├── footer.html │ │ ├── gitalk.html │ │ ├── header.html │ │ ├── html-head.html │ │ ├── inject │ │ ├── body.html │ │ ├── content-after.html │ │ ├── content-before.html │ │ ├── footer.html │ │ ├── head.html │ │ ├── menu-after.html │ │ └── menu-before.html │ │ ├── languages.html │ │ ├── menu-bundle.html │ │ ├── menu-filetree.html │ │ ├── menu-hugo.html │ │ ├── menu.html │ │ ├── post-meta.html │ │ ├── search.html │ │ ├── taxonomy.html │ │ ├── title.html │ │ └── toc.html ├── posts │ ├── list.html │ └── single.html ├── shortcodes │ ├── button.html │ ├── columns.html │ ├── details.html │ ├── expand.html │ ├── hint.html │ ├── katex.html │ ├── mermaid.html │ ├── section.html │ ├── tab.html │ └── tabs.html └── taxonomy │ ├── list.html │ └── taxonomy.html ├── static ├── LeetCode_Icon.png ├── LeetCode_logo.png ├── LeetCode_logo2048.png ├── apple-touch-icon-1024x1024.png ├── apple-touch-icon-120x120.png ├── apple-touch-icon-152x152.png ├── apple-touch-icon-180x180.png ├── apple-touch-icon-512x512.png ├── apple-touch-icon-60x60.png ├── apple-touch-icon-76x76.png ├── favicon.png ├── favicon.svg ├── flexsearch.min.js ├── fonts │ ├── roboto-mono-v6-latin-regular.woff │ ├── roboto-mono-v6-latin-regular.woff2 │ ├── roboto-v19-latin-300italic.woff │ ├── roboto-v19-latin-300italic.woff2 │ ├── roboto-v19-latin-700.woff │ ├── roboto-v19-latin-700.woff2 │ ├── roboto-v19-latin-regular.woff │ └── roboto-v19-latin-regular.woff2 ├── katex │ ├── auto-render.min.js │ ├── fonts │ │ ├── KaTeX_AMS-Regular.ttf │ │ ├── KaTeX_AMS-Regular.woff │ │ ├── KaTeX_AMS-Regular.woff2 │ │ ├── KaTeX_Caligraphic-Bold.ttf │ │ ├── KaTeX_Caligraphic-Bold.woff │ │ ├── KaTeX_Caligraphic-Bold.woff2 │ │ ├── KaTeX_Caligraphic-Regular.ttf │ │ ├── KaTeX_Caligraphic-Regular.woff │ │ ├── KaTeX_Caligraphic-Regular.woff2 │ │ ├── KaTeX_Fraktur-Bold.ttf │ │ ├── KaTeX_Fraktur-Bold.woff │ │ ├── KaTeX_Fraktur-Bold.woff2 │ │ ├── KaTeX_Fraktur-Regular.ttf │ │ ├── KaTeX_Fraktur-Regular.woff │ │ ├── KaTeX_Fraktur-Regular.woff2 │ │ ├── KaTeX_Main-Bold.ttf │ │ ├── KaTeX_Main-Bold.woff │ │ ├── KaTeX_Main-Bold.woff2 │ │ ├── KaTeX_Main-BoldItalic.ttf │ │ ├── KaTeX_Main-BoldItalic.woff │ │ ├── KaTeX_Main-BoldItalic.woff2 │ │ ├── KaTeX_Main-Italic.ttf │ │ ├── KaTeX_Main-Italic.woff │ │ ├── KaTeX_Main-Italic.woff2 │ │ ├── KaTeX_Main-Regular.ttf │ │ ├── KaTeX_Main-Regular.woff │ │ ├── KaTeX_Main-Regular.woff2 │ │ ├── KaTeX_Math-BoldItalic.ttf │ │ ├── KaTeX_Math-BoldItalic.woff │ │ ├── KaTeX_Math-BoldItalic.woff2 │ │ ├── KaTeX_Math-Italic.ttf │ │ ├── KaTeX_Math-Italic.woff │ │ ├── KaTeX_Math-Italic.woff2 │ │ ├── KaTeX_SansSerif-Bold.ttf │ │ ├── KaTeX_SansSerif-Bold.woff │ │ ├── KaTeX_SansSerif-Bold.woff2 │ │ ├── KaTeX_SansSerif-Italic.ttf │ │ ├── KaTeX_SansSerif-Italic.woff │ │ ├── KaTeX_SansSerif-Italic.woff2 │ │ ├── KaTeX_SansSerif-Regular.ttf │ │ ├── KaTeX_SansSerif-Regular.woff │ │ ├── KaTeX_SansSerif-Regular.woff2 │ │ ├── KaTeX_Script-Regular.ttf │ │ ├── KaTeX_Script-Regular.woff │ │ ├── KaTeX_Script-Regular.woff2 │ │ ├── KaTeX_Size1-Regular.ttf │ │ ├── KaTeX_Size1-Regular.woff │ │ ├── KaTeX_Size1-Regular.woff2 │ │ ├── KaTeX_Size2-Regular.ttf │ │ ├── KaTeX_Size2-Regular.woff │ │ ├── KaTeX_Size2-Regular.woff2 │ │ ├── KaTeX_Size3-Regular.ttf │ │ ├── KaTeX_Size3-Regular.woff │ │ ├── KaTeX_Size3-Regular.woff2 │ │ ├── KaTeX_Size4-Regular.ttf │ │ ├── KaTeX_Size4-Regular.woff │ │ ├── KaTeX_Size4-Regular.woff2 │ │ ├── KaTeX_Typewriter-Regular.ttf │ │ ├── KaTeX_Typewriter-Regular.woff │ │ └── KaTeX_Typewriter-Regular.woff2 │ ├── katex.min.css │ └── katex.min.js ├── mermaid.min.js ├── prism.css ├── prism.js └── svg │ ├── calendar.svg │ ├── edit.svg │ ├── menu.svg │ ├── toc.svg │ └── translate.svg └── theme.toml /gotest.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | echo "" > coverage.txt 5 | 6 | for d in $(go list ./leetcode/... | grep -v vendor); do 7 | echo $d 8 | go test -coverprofile=profile.out -covermode=atomic $d 9 | if [ -f profile.out ]; then 10 | cat profile.out >> coverage.txt 11 | rm profile.out 12 | fi 13 | done 14 | -------------------------------------------------------------------------------- /leetcode/0001.Two-Sum/1. Two Sum.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func twoSum(nums []int, target int) []int { 4 | m := make(map[int]int) 5 | for k, v := range nums { 6 | if idx, ok := m[target-v]; ok { 7 | return []int{idx, k} 8 | } 9 | m[v] = k 10 | } 11 | return nil 12 | } 13 | -------------------------------------------------------------------------------- /leetcode/0007.Reverse-Integer/7. Reverse Integer.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func reverse7(x int) int { 4 | tmp := 0 5 | for x != 0 { 6 | tmp = tmp*10 + x%10 7 | x = x / 10 8 | } 9 | if tmp > 1<<31-1 || tmp < -(1<<31) { 10 | return 0 11 | } 12 | return tmp 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/0009.Palindrome-Number/9. Palindrome Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strconv" 4 | 5 | func isPalindrome(x int) bool { 6 | if x < 0 { 7 | return false 8 | } 9 | if x < 10 { 10 | return true 11 | } 12 | s := strconv.Itoa(x) 13 | length := len(s) 14 | for i := 0; i <= length/2; i++ { 15 | if s[i] != s[length-1-i] { 16 | return false 17 | } 18 | } 19 | return true 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0011.Container-With-Most-Water/11. Container With Most Water.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maxArea(height []int) int { 4 | max, start, end := 0, 0, len(height)-1 5 | for start < end { 6 | width := end - start 7 | high := 0 8 | if height[start] < height[end] { 9 | high = height[start] 10 | start++ 11 | } else { 12 | high = height[end] 13 | end-- 14 | } 15 | 16 | temp := width * high 17 | if temp > max { 18 | max = temp 19 | } 20 | } 21 | return max 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0013.Roman-to-Integer/13. Roman to Integer.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | var roman = map[string]int{ 4 | "I": 1, 5 | "V": 5, 6 | "X": 10, 7 | "L": 50, 8 | "C": 100, 9 | "D": 500, 10 | "M": 1000, 11 | } 12 | 13 | func romanToInt(s string) int { 14 | if s == "" { 15 | return 0 16 | } 17 | num, lastint, total := 0, 0, 0 18 | for i := 0; i < len(s); i++ { 19 | char := s[len(s)-(i+1) : len(s)-i] 20 | num = roman[char] 21 | if num < lastint { 22 | total = total - num 23 | } else { 24 | total = total + num 25 | } 26 | lastint = num 27 | } 28 | return total 29 | } 30 | -------------------------------------------------------------------------------- /leetcode/0020.Valid-Parentheses/20. Valid Parentheses.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isValid(s string) bool { 4 | // 空字符串直接返回 true 5 | if len(s) == 0 { 6 | return true 7 | } 8 | stack := make([]rune, 0) 9 | for _, v := range s { 10 | if (v == '[') || (v == '(') || (v == '{') { 11 | stack = append(stack, v) 12 | } else if ((v == ']') && len(stack) > 0 && stack[len(stack)-1] == '[') || 13 | ((v == ')') && len(stack) > 0 && stack[len(stack)-1] == '(') || 14 | ((v == '}') && len(stack) > 0 && stack[len(stack)-1] == '{') { 15 | stack = stack[:len(stack)-1] 16 | } else { 17 | return false 18 | } 19 | } 20 | return len(stack) == 0 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0021.Merge-Two-Sorted-Lists/21. Merge Two Sorted Lists.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // ListNode define 8 | type ListNode = structures.ListNode 9 | 10 | /** 11 | * Definition for singly-linked list. 12 | * type ListNode struct { 13 | * Val int 14 | * Next *ListNode 15 | * } 16 | */ 17 | func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { 18 | if l1 == nil { 19 | return l2 20 | } 21 | if l2 == nil { 22 | return l1 23 | } 24 | if l1.Val < l2.Val { 25 | l1.Next = mergeTwoLists(l1.Next, l2) 26 | return l1 27 | } 28 | l2.Next = mergeTwoLists(l1, l2.Next) 29 | return l2 30 | } 31 | -------------------------------------------------------------------------------- /leetcode/0021.Merge-Two-Sorted-Lists/README.md: -------------------------------------------------------------------------------- 1 | # [21. Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) 2 | 3 | ## 题目 4 | 5 | Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 6 | 7 | Example : 8 | 9 | ``` 10 | Input: 1->2->4, 1->3->4 11 | Output: 1->1->2->3->4->4 12 | 13 | ``` 14 | 15 | ## 题目大意 16 | 17 | 合并 2 个有序链表 18 | 19 | ## 解题思路 20 | 21 | 按照题意做即可。 -------------------------------------------------------------------------------- /leetcode/0022.Generate-Parentheses/22. Generate Parentheses.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func generateParenthesis(n int) []string { 4 | if n == 0 { 5 | return []string{} 6 | } 7 | res := []string{} 8 | findGenerateParenthesis(n, n, "", &res) 9 | return res 10 | } 11 | 12 | func findGenerateParenthesis(lindex, rindex int, str string, res *[]string) { 13 | if lindex == 0 && rindex == 0 { 14 | *res = append(*res, str) 15 | return 16 | } 17 | if lindex > 0 { 18 | findGenerateParenthesis(lindex-1, rindex, str+"(", res) 19 | } 20 | if rindex > 0 && lindex < rindex { 21 | findGenerateParenthesis(lindex, rindex-1, str+")", res) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/0023.Merge-k-Sorted-Lists/README.md: -------------------------------------------------------------------------------- 1 | # [23. Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) 2 | 3 | ## 题目 4 | 5 | Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 6 | 7 | 8 | 9 | Example : 10 | 11 | ``` 12 | Input: 13 | [ 14 | 1->4->5, 15 | 1->3->4, 16 | 2->6 17 | ] 18 | Output: 1->1->2->3->4->4->5->6 19 | 20 | ``` 21 | 22 | ## 题目大意 23 | 24 | 合并 K 个有序链表 25 | 26 | ## 解题思路 27 | 28 | 借助分治的思想,把 K 个有序链表两两合并即可。相当于是第 21 题的加强版。 -------------------------------------------------------------------------------- /leetcode/0024.Swap-Nodes-in-Pairs/README.md: -------------------------------------------------------------------------------- 1 | # [24. Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/description/) 2 | 3 | ## 题目 4 | 5 | Given a linked list, swap every two adjacent nodes and return its head. 6 | 7 | You may not modify the values in the list's nodes, only nodes itself may be changed. 8 | 9 | 10 | 11 | Example: 12 | 13 | ```c 14 | Given 1->2->3->4, you should return the list as 2->1->4->3. 15 | ``` 16 | 17 | ## 题目大意 18 | 19 | 两两相邻的元素,翻转链表 20 | 21 | ## 解题思路 22 | 23 | 按照题意做即可。 -------------------------------------------------------------------------------- /leetcode/0027.Remove-Element/27. Remove Element.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func removeElement(nums []int, val int) int { 4 | if len(nums) == 0 { 5 | return 0 6 | } 7 | j := 0 8 | for i := 0; i < len(nums); i++ { 9 | if nums[i] != val { 10 | if i != j { 11 | nums[i], nums[j] = nums[j], nums[i] 12 | j++ 13 | } else { 14 | j++ 15 | } 16 | } 17 | } 18 | return j 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0028.Implement-strStr/28. Implement strStr().go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | // 解法一 6 | func strStr(haystack string, needle string) int { 7 | for i := 0; ; i++ { 8 | for j := 0; ; j++ { 9 | if j == len(needle) { 10 | return i 11 | } 12 | if i+j == len(haystack) { 13 | return -1 14 | } 15 | if needle[j] != haystack[i+j] { 16 | break 17 | } 18 | } 19 | } 20 | } 21 | 22 | // 解法二 23 | func strStr1(haystack string, needle string) int { 24 | return strings.Index(haystack, needle) 25 | } 26 | -------------------------------------------------------------------------------- /leetcode/0035.Search-Insert-Position/35. Search Insert Position.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func searchInsert(nums []int, target int) int { 4 | low, high := 0, len(nums)-1 5 | for low <= high { 6 | mid := low + (high-low)>>1 7 | if nums[mid] >= target { 8 | high = mid - 1 9 | } else { 10 | if (mid == len(nums)-1) || (nums[mid+1] >= target) { 11 | return mid + 1 12 | } 13 | low = mid + 1 14 | } 15 | } 16 | return 0 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0041.First-Missing-Positive/41. First Missing Positive.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func firstMissingPositive(nums []int) int { 4 | numMap := make(map[int]int, len(nums)) 5 | for _, v := range nums { 6 | numMap[v] = v 7 | } 8 | for index := 1; index < len(nums)+1; index++ { 9 | if _, ok := numMap[index]; !ok { 10 | return index 11 | } 12 | } 13 | return len(nums) + 1 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/0042.Trapping-Rain-Water/42. Trapping Rain Water.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func trap(height []int) int { 4 | res, left, right, maxLeft, maxRight := 0, 0, len(height)-1, 0, 0 5 | for left <= right { 6 | if height[left] <= height[right] { 7 | if height[left] > maxLeft { 8 | maxLeft = height[left] 9 | } else { 10 | res += maxLeft - height[left] 11 | } 12 | left++ 13 | } else { 14 | if height[right] >= maxRight { 15 | maxRight = height[right] 16 | } else { 17 | res += maxRight - height[right] 18 | } 19 | right-- 20 | } 21 | } 22 | return res 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/0046.Permutations/README.md: -------------------------------------------------------------------------------- 1 | # [46. Permutations](https://leetcode.com/problems/permutations/) 2 | 3 | 4 | ## 题目 5 | 6 | Given a collection of **distinct** integers, return all possible permutations. 7 | 8 | **Example:** 9 | 10 | 11 | Input: [1,2,3] 12 | Output: 13 | [ 14 | [1,2,3], 15 | [1,3,2], 16 | [2,1,3], 17 | [2,3,1], 18 | [3,1,2], 19 | [3,2,1] 20 | ] 21 | 22 | 23 | ## 题目大意 24 | 25 | 给定一个没有重复数字的序列,返回其所有可能的全排列。 26 | 27 | 28 | ## 解题思路 29 | 30 | - 求出一个数组的排列组合中的所有排列,用 DFS 深搜即可。 31 | -------------------------------------------------------------------------------- /leetcode/0047.Permutations-II/README.md: -------------------------------------------------------------------------------- 1 | # [47. Permutations II](https://leetcode.com/problems/permutations-ii/) 2 | 3 | 4 | ## 题目 5 | 6 | Given a collection of numbers that might contain duplicates, return all possible unique permutations. 7 | 8 | **Example:** 9 | 10 | 11 | Input: [1,1,2] 12 | Output: 13 | [ 14 | [1,1,2], 15 | [1,2,1], 16 | [2,1,1] 17 | ] 18 | 19 | 20 | ## 题目大意 21 | 22 | 给定一个可包含重复数字的序列,返回所有不重复的全排列。 23 | 24 | ## 解题思路 25 | 26 | - 这一题是第 46 题的加强版,第 46 题中求数组的排列,数组中元素不重复,但是这一题中,数组元素会重复,所以需要最终排列出来的结果需要去重。 27 | - 去重的方法是经典逻辑,将数组排序以后,判断重复元素再做逻辑判断。 28 | - 其他思路和第 46 题完全一致,DFS 深搜即可。 29 | -------------------------------------------------------------------------------- /leetcode/0048.Rotate-Image/48. Rotate Image.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func rotate(matrix [][]int) { 4 | row := len(matrix) 5 | if row <= 0 { 6 | return 7 | } 8 | column := len(matrix[0]) 9 | // rotate by diagonal 对角线变换 10 | for i := 0; i < row; i++ { 11 | for j := i + 1; j < column; j++ { 12 | tmp := matrix[i][j] 13 | matrix[i][j] = matrix[j][i] 14 | matrix[j][i] = tmp 15 | } 16 | } 17 | // rotate by vertical centerline 竖直轴对称翻转 18 | halfColumn := column / 2 19 | for i := 0; i < row; i++ { 20 | for j := 0; j < halfColumn; j++ { 21 | tmp := matrix[i][j] 22 | matrix[i][j] = matrix[i][column-j-1] 23 | matrix[i][column-j-1] = tmp 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/0050.Powx-n/50. Pow(x, n).go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 时间复杂度 O(log n),空间复杂度 O(1) 4 | func myPow(x float64, n int) float64 { 5 | if n == 0 { 6 | return 1 7 | } 8 | if n == 1 { 9 | return x 10 | } 11 | if n < 0 { 12 | n = -n 13 | x = 1 / x 14 | } 15 | tmp := myPow(x, n/2) 16 | if n%2 == 0 { 17 | return tmp * tmp 18 | } 19 | return tmp * tmp * x 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0055.Jump-Game/55. Jump Game.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func canJump(nums []int) bool { 4 | n := len(nums) 5 | if n == 0 { 6 | return false 7 | } 8 | if n == 1 { 9 | return true 10 | } 11 | maxJump := 0 12 | for i, v := range nums { 13 | if i > maxJump { 14 | return false 15 | } 16 | maxJump = max(maxJump, i+v) 17 | } 18 | return true 19 | } 20 | 21 | func max(a int, b int) int { 22 | if a > b { 23 | return a 24 | } 25 | return b 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/0056.Merge-Intervals/README.md: -------------------------------------------------------------------------------- 1 | # [56. Merge Intervals](https://leetcode.com/problems/merge-intervals/) 2 | 3 | ## 题目 4 | 5 | Given a collection of intervals, merge all overlapping intervals. 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: [[1,3],[2,6],[8,10],[15,18]] 11 | Output: [[1,6],[8,10],[15,18]] 12 | Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. 13 | ``` 14 | 15 | Example 2: 16 | 17 | ``` 18 | Input: [[1,4],[4,5]] 19 | Output: [[1,5]] 20 | Explanation: Intervals [1,4] and [4,5] are considered overlapping. 21 | ``` 22 | 23 | ## 题目大意 24 | 25 | 合并给的多个区间,区间有重叠的要进行区间合并。 26 | 27 | 28 | ## 解题思路 29 | 30 | 先按照区间起点进行排序。然后从区间起点小的开始扫描,依次合并每个有重叠的区间。 -------------------------------------------------------------------------------- /leetcode/0059.Spiral-Matrix-II/README.md: -------------------------------------------------------------------------------- 1 | # [59. Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) 2 | 3 | 4 | ## 题目 5 | 6 | Given a positive integer *n*, generate a square matrix filled with elements from 1 to *n*2 in spiral order. 7 | 8 | **Example:** 9 | 10 | 11 | Input: 3 12 | Output: 13 | [ 14 | [ 1, 2, 3 ], 15 | [ 8, 9, 4 ], 16 | [ 7, 6, 5 ] 17 | ] 18 | 19 | 20 | ## 题目大意 21 | 22 | 给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。 23 | 24 | 25 | ## 解题思路 26 | 27 | - 给出一个数组 n,要求输出一个 n * n 的二维数组,里面元素是 1 - n*n,且数组排列顺序是螺旋排列的 28 | - 这一题是第 54 题的加强版,没有需要注意的特殊情况,直接模拟即可。 29 | 30 | -------------------------------------------------------------------------------- /leetcode/0062.Unique-Paths/62. Unique Paths.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func uniquePaths(m int, n int) int { 4 | dp := make([][]int, n) 5 | for i := 0; i < n; i++ { 6 | dp[i] = make([]int, m) 7 | } 8 | for i := 0; i < m; i++ { 9 | dp[0][i] = 1 10 | } 11 | for i := 0; i < n; i++ { 12 | dp[i][0] = 1 13 | } 14 | for i := 1; i < n; i++ { 15 | for j := 1; j < m; j++ { 16 | dp[i][j] = dp[i-1][j] + dp[i][j-1] 17 | } 18 | } 19 | return dp[n-1][m-1] 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0066.Plus-One/66. Plus One.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func plusOne(digits []int) []int { 4 | if len(digits) == 0 { 5 | return []int{} 6 | } 7 | carry := 1 8 | for i := len(digits) - 1; i >= 0; i-- { 9 | if digits[i]+carry > 9 { 10 | digits[i] = 0 11 | carry = 1 12 | } else { 13 | digits[i] += carry 14 | carry = 0 15 | } 16 | } 17 | if digits[0] == 0 && carry == 1 { 18 | digits = append([]int{1}, digits...) 19 | } 20 | return digits 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0070.Climbing-Stairs/70. Climbing Stairs.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func climbStairs(n int) int { 4 | dp := make([]int, n+1) 5 | dp[0], dp[1] = 1, 1 6 | for i := 2; i <= n; i++ { 7 | dp[i] = dp[i-1] + dp[i-2] 8 | } 9 | return dp[n] 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/0074.Search-a-2D-Matrix/74. Search a 2D Matrix.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func searchMatrix(matrix [][]int, target int) bool { 4 | if len(matrix) == 0 { 5 | return false 6 | } 7 | m, low, high := len(matrix[0]), 0, len(matrix[0])*len(matrix)-1 8 | for low <= high { 9 | mid := low + (high-low)>>1 10 | if matrix[mid/m][mid%m] == target { 11 | return true 12 | } else if matrix[mid/m][mid%m] > target { 13 | high = mid - 1 14 | } else { 15 | low = mid + 1 16 | } 17 | } 18 | return false 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0075.Sort-Colors/75. Sort Colors.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func sortColors(nums []int) { 4 | if len(nums) == 0 { 5 | return 6 | } 7 | 8 | r := 0 9 | w := 0 10 | b := 0 // label the end of different colors; 11 | for _, num := range nums { 12 | if num == 0 { 13 | nums[b] = 2 14 | b++ 15 | nums[w] = 1 16 | w++ 17 | nums[r] = 0 18 | r++ 19 | } else if num == 1 { 20 | nums[b] = 2 21 | b++ 22 | nums[w] = 1 23 | w++ 24 | } else if num == 2 { 25 | b++ 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/0077.Combinations/77. Combinations.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func combine(n int, k int) [][]int { 4 | if n <= 0 || k <= 0 || k > n { 5 | return [][]int{} 6 | } 7 | c, res := []int{}, [][]int{} 8 | generateCombinations(n, k, 1, c, &res) 9 | return res 10 | } 11 | 12 | func generateCombinations(n, k, start int, c []int, res *[][]int) { 13 | if len(c) == k { 14 | b := make([]int, len(c)) 15 | copy(b, c) 16 | *res = append(*res, b) 17 | return 18 | } 19 | // i will at most be n - (k - c.size()) + 1 20 | for i := start; i <= n-(k-len(c))+1; i++ { 21 | c = append(c, i) 22 | generateCombinations(n, k, i+1, c, res) 23 | c = c[:len(c)-1] 24 | } 25 | return 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/0077.Combinations/README.md: -------------------------------------------------------------------------------- 1 | # [77. Combinations](https://leetcode.com/problems/combinations/) 2 | 3 | 4 | ## 题目 5 | 6 | Given two integers *n* and *k*, return all possible combinations of *k* numbers out of 1 ... *n*. 7 | 8 | **Example:** 9 | 10 | Input: n = 4, k = 2 11 | Output: 12 | [ 13 | [2,4], 14 | [3,4], 15 | [2,3], 16 | [1,2], 17 | [1,3], 18 | [1,4], 19 | ] 20 | 21 | ## 题目大意 22 | 23 | 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。 24 | 25 | ## 解题思路 26 | 27 | - 计算排列组合中的组合,用 DFS 深搜即可,注意剪枝 28 | -------------------------------------------------------------------------------- /leetcode/0082.Remove-Duplicates-from-Sorted-List-II/README.md: -------------------------------------------------------------------------------- 1 | # [82. Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) 2 | 3 | ## 题目 4 | 5 | Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: 1->2->3->3->4->4->5 11 | Output: 1->2->5 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: 1->1->1->2->3 18 | Output: 2->3 19 | ``` 20 | 21 | ## 题目大意 22 | 23 | 删除链表中重复的结点,只要是有重复过的结点,全部删除。 24 | 25 | ## 解题思路 26 | 27 | 按照题意做即可。 -------------------------------------------------------------------------------- /leetcode/0083.Remove-Duplicates-from-Sorted-List/README.md: -------------------------------------------------------------------------------- 1 | # [83. Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) 2 | 3 | ## 题目 4 | 5 | Given a sorted linked list, delete all duplicates such that each element appear only once. 6 | 7 | Example 1: 8 | 9 | ``` 10 | Input: 1->1->2 11 | Output: 1->2 12 | ``` 13 | 14 | Example 2: 15 | 16 | ``` 17 | Input: 1->1->2->3->3 18 | Output: 1->2->3 19 | ``` 20 | 21 | ## 题目大意 22 | 23 | 删除链表中重复的结点,以保障每个结点只出现一次。 24 | 25 | 26 | ## 解题思路 27 | 28 | 按照题意做即可。 -------------------------------------------------------------------------------- /leetcode/0088.Merge-Sorted-Array/88. Merge Sorted Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func merge(nums1 []int, m int, nums2 []int, n int) { 4 | if m == 0 { 5 | copy(nums1, nums2) 6 | return 7 | } 8 | // 这里不需要,因为测试数据考虑到了第一个数组的空间问题 9 | // for index := 0; index < n; index++ { 10 | // nums1 = append(nums1, nums2[index]) 11 | // } 12 | i := m - 1 13 | j := n - 1 14 | k := m + n - 1 15 | // 从后面往前放,只需要循环一次即可 16 | for ; i >= 0 && j >= 0; k-- { 17 | if nums1[i] > nums2[j] { 18 | nums1[k] = nums1[i] 19 | i-- 20 | } else { 21 | nums1[k] = nums2[j] 22 | j-- 23 | } 24 | } 25 | for ; j >= 0; k-- { 26 | nums1[k] = nums2[j] 27 | j-- 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /leetcode/0088.Merge-Sorted-Array/README.md: -------------------------------------------------------------------------------- 1 | # [88. Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/) 2 | 3 | ## 题目 4 | 5 | Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 6 | 7 | Note: 8 | 9 | You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively. 10 | 11 | ## 题目大意 12 | 13 | 合并两个已经有序的数组,结果放在第一个数组中,第一个数组假设空间足够大。要求算法时间复杂度足够低。 14 | 15 | ## 解题思路 16 | 17 | 为了不大量移动元素,就要从2个数组长度之和的最后一个位置开始,依次选取两个数组中大的数,从第一个数组的尾巴开始往头放,只要循环一次以后,就生成了合并以后的数组了。 -------------------------------------------------------------------------------- /leetcode/0091.Decode-Ways/91. Decode Ways.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "strconv" 5 | ) 6 | 7 | func numDecodings(s string) int { 8 | if len(s) == 0 { 9 | return 0 10 | } 11 | dp := make([]int, len(s)+1) 12 | dp[0] = 1 13 | if s[:1] == "0" { 14 | dp[1] = 0 15 | } else { 16 | dp[1] = 1 17 | } 18 | for i := 2; i <= len(s); i++ { 19 | lastNum, _ := strconv.Atoi(s[i-1 : i]) 20 | if lastNum >= 1 && lastNum <= 9 { 21 | dp[i] += dp[i-1] 22 | } 23 | lastNum, _ = strconv.Atoi(s[i-2 : i]) 24 | if lastNum >= 10 && lastNum <= 26 { 25 | dp[i] += dp[i-2] 26 | } 27 | } 28 | return dp[len(s)] 29 | } 30 | -------------------------------------------------------------------------------- /leetcode/0093.Restore-IP-Addresses/README.md: -------------------------------------------------------------------------------- 1 | # [93. Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/) 2 | 3 | 4 | ## 题目 5 | 6 | Given a string containing only digits, restore it by returning all possible valid IP address combinations. 7 | 8 | **Example:** 9 | 10 | Input: "25525511135" 11 | Output: ["255.255.11.135", "255.255.111.35"] 12 | 13 | ## 题目大意 14 | 15 | 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。 16 | 17 | ## 解题思路 18 | 19 | - DFS 深搜 20 | - 需要注意的点是 IP 的规则,以 0 开头的数字和超过 255 的数字都为非法的。 21 | 22 | -------------------------------------------------------------------------------- /leetcode/0094.Binary-Tree-Inorder-Traversal/README.md: -------------------------------------------------------------------------------- 1 | # [94. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) 2 | 3 | ## 题目 4 | 5 | 6 | Given a binary tree, return the inorder traversal of its nodes' values. 7 | 8 | 9 | 10 | Example : 11 | 12 | ```c 13 | Input: [1,null,2,3] 14 | 1 15 | \ 16 | 2 17 | / 18 | 3 19 | 20 | Output: [1,3,2] 21 | ``` 22 | 23 | 24 | Follow up: Recursive solution is trivial, could you do it iteratively? 25 | 26 | 27 | 28 | 29 | 30 | 31 | ## 题目大意 32 | 33 | 中根遍历一颗树。 34 | 35 | ## 解题思路 36 | 37 | 递归的实现方法,见代码。 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /leetcode/0096.Unique-Binary-Search-Trees/96. Unique Binary Search Trees.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func numTrees(n int) int { 4 | dp := make([]int, n+1) 5 | dp[0], dp[1] = 1, 1 6 | for i := 2; i <= n; i++ { 7 | for j := 1; j <= i; j++ { 8 | dp[i] += dp[j-1] * dp[i-j] 9 | } 10 | } 11 | return dp[n] 12 | } 13 | -------------------------------------------------------------------------------- /leetcode/0104.Maximum-Depth-of-Binary-Tree/104. Maximum Depth of Binary Tree.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // TreeNode define 8 | type TreeNode = structures.TreeNode 9 | 10 | /** 11 | * Definition for a binary tree node. 12 | * type TreeNode struct { 13 | * Val int 14 | * Left *TreeNode 15 | * Right *TreeNode 16 | * } 17 | */ 18 | 19 | func maxDepth(root *TreeNode) int { 20 | if root == nil { 21 | return 0 22 | } 23 | return max(maxDepth(root.Left), maxDepth(root.Right)) + 1 24 | } 25 | 26 | func max(a int, b int) int { 27 | if a > b { 28 | return a 29 | } 30 | return b 31 | } 32 | -------------------------------------------------------------------------------- /leetcode/0108.Convert-Sorted-Array-to-Binary-Search-Tree/108. Convert Sorted Array to Binary Search Tree.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // TreeNode define 8 | type TreeNode = structures.TreeNode 9 | 10 | /** 11 | * Definition for a binary tree node. 12 | * type TreeNode struct { 13 | * Val int 14 | * Left *TreeNode 15 | * Right *TreeNode 16 | * } 17 | */ 18 | 19 | func sortedArrayToBST(nums []int) *TreeNode { 20 | if len(nums) == 0 { 21 | return nil 22 | } 23 | return &TreeNode{Val: nums[len(nums)/2], Left: sortedArrayToBST(nums[:len(nums)/2]), Right: sortedArrayToBST(nums[len(nums)/2+1:])} 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0112.Path-Sum/112. Path Sum.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // TreeNode define 8 | type TreeNode = structures.TreeNode 9 | 10 | /** 11 | * Definition for a binary tree node. 12 | * type TreeNode struct { 13 | * Val int 14 | * Left *TreeNode 15 | * Right *TreeNode 16 | * } 17 | */ 18 | 19 | func hasPathSum(root *TreeNode, sum int) bool { 20 | if root == nil { 21 | return false 22 | } 23 | if root.Left == nil && root.Right == nil { 24 | return sum == root.Val 25 | } 26 | return hasPathSum(root.Left, sum-root.Val) || hasPathSum(root.Right, sum-root.Val) 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0118.Pascals-Triangle/118. Pascal's Triangle.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func generate(numRows int) [][]int { 4 | result := [][]int{} 5 | for i := 0; i < numRows; i++ { 6 | row := []int{} 7 | for j := 0; j < i+1; j++ { 8 | if j == 0 || j == i { 9 | row = append(row, 1) 10 | } else if i > 1 { 11 | row = append(row, result[i-1][j-1]+result[i-1][j]) 12 | } 13 | } 14 | result = append(result, row) 15 | } 16 | return result 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0122.Best-Time-to-Buy-and-Sell-Stock-II/122. Best Time to Buy and Sell Stock II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maxProfit122(prices []int) int { 4 | profit := 0 5 | for i := 0; i < len(prices)-1; i++ { 6 | if prices[i+1] > prices[i] { 7 | profit += prices[i+1] - prices[i] 8 | } 9 | } 10 | return profit 11 | } 12 | -------------------------------------------------------------------------------- /leetcode/0125.Valid-Palindrome/125. Valid Palindrome.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "strings" 5 | ) 6 | 7 | func isPalindrome(s string) bool { 8 | 9 | s = strings.ToLower(s) 10 | 11 | i, j := 0, len(s)-1 12 | for i < j { 13 | for i < j && !isChar(s[i]) { 14 | i++ 15 | } 16 | for i < j && !isChar(s[j]) { 17 | j-- 18 | } 19 | if s[i] != s[j] { 20 | return false 21 | } 22 | i++ 23 | j-- 24 | } 25 | 26 | return true 27 | } 28 | 29 | // 判断 c 是否是字符或者数字 30 | func isChar(c byte) bool { 31 | if ('a' <= c && c <= 'z') || ('0' <= c && c <= '9') { 32 | return true 33 | } 34 | return false 35 | } 36 | -------------------------------------------------------------------------------- /leetcode/0131.Palindrome-Partitioning/README.md: -------------------------------------------------------------------------------- 1 | # [131. Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/) 2 | 3 | 4 | ## 题目 5 | 6 | Given a string *s*, partition *s* such that every substring of the partition is a palindrome. 7 | 8 | Return all possible palindrome partitioning of *s*. 9 | 10 | **Example:** 11 | 12 | Input: "aab" 13 | Output: 14 | [ 15 | ["aa","b"], 16 | ["a","a","b"] 17 | ] 18 | 19 | ## 题目大意 20 | 21 | 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。返回 s 所有可能的分割方案。 22 | 23 | ## 解题思路 24 | 25 | - 要求输出一个字符串可以被拆成回文串的所有解,DFS 递归求解即可。 26 | -------------------------------------------------------------------------------- /leetcode/0136.Single-Number/136. Single Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func singleNumber(nums []int) int { 4 | result := 0 5 | for i := 0; i < len(nums); i++ { 6 | result ^= nums[i] 7 | } 8 | return result 9 | } 10 | -------------------------------------------------------------------------------- /leetcode/0141.Linked-List-Cycle/141. Linked List Cycle_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "testing" 4 | 5 | func Test_Problem141(t *testing.T) { 6 | } 7 | -------------------------------------------------------------------------------- /leetcode/0141.Linked-List-Cycle/README.md: -------------------------------------------------------------------------------- 1 | # [141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/description/) 2 | 3 | ## 题目 4 | 5 | Given a linked list, determine if it has a cycle in it. 6 | 7 | Follow up: 8 | Can you solve it without using extra space? 9 | 10 | 11 | 12 | ## 题目大意 13 | 14 | 判断链表是否有环,不能使用额外的空间。 15 | 16 | ## 解题思路 17 | 18 | 给 2 个指针,一个指针是另外一个指针的下一个指针。快指针一次走 2 格,慢指针一次走 1 格。如果存在环,那么前一个指针一定会经过若干圈之后追上慢的指针。 -------------------------------------------------------------------------------- /leetcode/0142.Linked-List-Cycle-II/142. Linked List Cycle II_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "testing" 4 | 5 | func Test_Problem142(t *testing.T) { 6 | } 7 | -------------------------------------------------------------------------------- /leetcode/0144.Binary-Tree-Preorder-Traversal/README.md: -------------------------------------------------------------------------------- 1 | # [144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) 2 | 3 | ## 题目 4 | 5 | Given a binary tree, return the preorder traversal of its nodes' values. 6 | 7 | 8 | 9 | Example : 10 | 11 | ```c 12 | Input: [1,null,2,3] 13 | 1 14 | \ 15 | 2 16 | / 17 | 3 18 | 19 | Output: [1,2,3] 20 | ``` 21 | 22 | 23 | Follow up: Recursive solution is trivial, could you do it iteratively? 24 | 25 | 26 | 27 | 28 | ## 题目大意 29 | 30 | 先根遍历一颗树。 31 | 32 | ## 解题思路 33 | 34 | 两种递归的实现方法,见代码。 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /leetcode/0145.Binary-Tree-Postorder-Traversal/README.md: -------------------------------------------------------------------------------- 1 | # [145. Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) 2 | 3 | ## 题目 4 | 5 | 6 | Given a binary tree, return the postorder traversal of its nodes' values. 7 | 8 | 9 | 10 | Example : 11 | 12 | ```c 13 | Input: [1,null,2,3] 14 | 1 15 | \ 16 | 2 17 | / 18 | 3 19 | 20 | Output: [3,2,1] 21 | ``` 22 | 23 | 24 | Follow up: Recursive solution is trivial, could you do it iteratively? 25 | 26 | 27 | 28 | 29 | ## 题目大意 30 | 31 | 后根遍历一颗树。 32 | 33 | ## 解题思路 34 | 35 | 递归的实现方法,见代码。 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /leetcode/0148.Sort-List/README.md: -------------------------------------------------------------------------------- 1 | # [148. Sort List](https://leetcode.com/problems/sort-list/) 2 | 3 | ## 题目 4 | 5 | Sort a linked list in O(n log n) time using constant space complexity. 6 | 7 | Example 1: 8 | 9 | ```c 10 | Input: 4->2->1->3 11 | Output: 1->2->3->4 12 | ``` 13 | 14 | Example 2: 15 | 16 | ```c 17 | Input: -1->5->3->4->0 18 | Output: -1->0->3->4->5 19 | ``` 20 | 21 | ## 题目大意 22 | 23 | 链表的排序,要求时间复杂度必须是 O(n log n),空间复杂度是 O(1) 24 | 25 | ## 解题思路 26 | 27 | 这道题只能用归并排序才能符合要求。归并排序需要的 2 个操作在其他题目已经出现过了,取中间点是第 876 题,合并 2 个有序链表是第 21 题。 -------------------------------------------------------------------------------- /leetcode/0151.Reverse-Words-in-a-String/151. Reverse Words in a String.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func reverseWords151(s string) string { 6 | ss := strings.Fields(s) 7 | reverse151(&ss, 0, len(ss)-1) 8 | return strings.Join(ss, " ") 9 | } 10 | 11 | func reverse151(m *[]string, i int, j int) { 12 | for i <= j { 13 | (*m)[i], (*m)[j] = (*m)[j], (*m)[i] 14 | i++ 15 | j-- 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0152.Maximum-Product-Subarray/152. Maximum Product Subarray.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maxProduct(nums []int) int { 4 | minimum, maximum, res := nums[0], nums[0], nums[0] 5 | for i := 1; i < len(nums); i++ { 6 | if nums[i] < 0 { 7 | maximum, minimum = minimum, maximum 8 | } 9 | maximum = max(nums[i], maximum*nums[i]) 10 | minimum = min(nums[i], minimum*nums[i]) 11 | res = max(res, maximum) 12 | } 13 | return res 14 | } 15 | 16 | func max(a int, b int) int { 17 | if a > b { 18 | return a 19 | } 20 | return b 21 | } 22 | 23 | func min(a int, b int) int { 24 | if a > b { 25 | return b 26 | } 27 | return a 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/0154.Find-Minimum-in-Rotated-Sorted-Array-II/154. Find Minimum in Rotated Sorted Array II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findMin154(nums []int) int { 4 | low, high := 0, len(nums)-1 5 | for low < high { 6 | if nums[low] < nums[high] { 7 | return nums[low] 8 | } 9 | mid := low + (high-low)>>1 10 | if nums[mid] > nums[low] { 11 | low = mid + 1 12 | } else if nums[mid] == nums[low] { 13 | low++ 14 | } else { 15 | high = mid 16 | } 17 | } 18 | return nums[low] 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0155.Min-Stack/155. Min Stack_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem155(t *testing.T) { 9 | obj1 := Constructor155() 10 | obj1.Push(1) 11 | fmt.Printf("obj1 = %v\n", obj1) 12 | obj1.Push(0) 13 | fmt.Printf("obj1 = %v\n", obj1) 14 | obj1.Push(10) 15 | fmt.Printf("obj1 = %v\n", obj1) 16 | obj1.Pop() 17 | fmt.Printf("obj1 = %v\n", obj1) 18 | param3 := obj1.Top() 19 | fmt.Printf("param_3 = %v\n", param3) 20 | param4 := obj1.GetMin() 21 | fmt.Printf("param_4 = %v\n", param4) 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0168.Excel-Sheet-Column-Title/168. Excel Sheet Column Title.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func convertToTitle(n int) string { 4 | result := []byte{} 5 | for n > 0 { 6 | result = append(result, 'A'+byte((n-1)%26)) 7 | n = (n - 1) / 26 8 | } 9 | for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 { 10 | result[i], result[j] = result[j], result[i] 11 | } 12 | return string(result) 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/0169.Majority-Element/169. Majority Element.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 解法一 时间复杂度 O(n) 空间复杂度 O(1) 4 | func majorityElement(nums []int) int { 5 | res, count := nums[0], 0 6 | for i := 0; i < len(nums); i++ { 7 | if count == 0 { 8 | res, count = nums[i], 1 9 | } else { 10 | if nums[i] == res { 11 | count++ 12 | } else { 13 | count-- 14 | } 15 | } 16 | } 17 | return res 18 | } 19 | 20 | // 解法二 时间复杂度 O(n) 空间复杂度 O(n) 21 | func majorityElement1(nums []int) int { 22 | m := make(map[int]int) 23 | for _, v := range nums { 24 | m[v]++ 25 | if m[v] > len(nums)/2 { 26 | return v 27 | } 28 | } 29 | return 0 30 | } 31 | -------------------------------------------------------------------------------- /leetcode/0171.Excel-Sheet-Column-Number/171. Excel Sheet Column Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func titleToNumber(s string) int { 4 | val, res := 0, 0 5 | for i := 0; i < len(s); i++ { 6 | val = int(s[i] - 'A' + 1) 7 | res = res*26 + val 8 | } 9 | return res 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/0172.Factorial-Trailing-Zeroes/172. Factorial Trailing Zeroes.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func trailingZeroes(n int) int { 4 | if n/5 == 0 { 5 | return 0 6 | } 7 | return n/5 + trailingZeroes(n/5) 8 | } 9 | -------------------------------------------------------------------------------- /leetcode/0190.Reverse-Bits/190. Reverse Bits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func reverseBits(num uint32) uint32 { 4 | var res uint32 5 | for i := 0; i < 32; i++ { 6 | res = res<<1 | num&1 7 | num >>= 1 8 | } 9 | return res 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/0191.Number-of-1-Bits/191. Number of 1 Bits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math/bits" 4 | 5 | // 解法一 6 | func hammingWeight(num uint32) int { 7 | return bits.OnesCount(uint(num)) 8 | } 9 | 10 | // 解法二 11 | func hammingWeight1(num uint32) int { 12 | count := 0 13 | for num != 0 { 14 | num = num & (num - 1) 15 | count++ 16 | } 17 | return count 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0201.Bitwise-AND-of-Numbers-Range/201. Bitwise AND of Numbers Range.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 解法一 4 | func rangeBitwiseAnd1(m int, n int) int { 5 | if m == 0 { 6 | return 0 7 | } 8 | moved := 0 9 | for m != n { 10 | m >>= 1 11 | n >>= 1 12 | moved++ 13 | } 14 | return m << uint32(moved) 15 | } 16 | 17 | // 解法二 Brian Kernighan's algorithm 18 | func rangeBitwiseAnd(m int, n int) int { 19 | for n > m { 20 | n &= (n - 1) // 清除最低位的 1 21 | } 22 | return n 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/0202.Happy-Number/202. Happy Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isHappy(n int) bool { 4 | if n == 0 { 5 | return false 6 | } 7 | res := 0 8 | num := n 9 | record := map[int]int{} 10 | for { 11 | for num != 0 { 12 | res += (num % 10) * (num % 10) 13 | num = num / 10 14 | } 15 | if _, ok := record[res]; !ok { 16 | if res == 1 { 17 | return true 18 | } 19 | record[res] = res 20 | num = res 21 | res = 0 22 | continue 23 | } else { 24 | return false 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0203.Remove-Linked-List-Elements/README.md: -------------------------------------------------------------------------------- 1 | # [203. Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) 2 | 3 | ## 题目 4 | 5 | Remove all elements from a linked list of integers that have value val. 6 | 7 | Example : 8 | 9 | ```c 10 | Input: 1->2->6->3->4->5->6, val = 6 11 | Output: 1->2->3->4->5 12 | ``` 13 | 14 | 15 | ## 题目大意 16 | 17 | 删除链表中所有指定值的结点。 18 | 19 | ## 解题思路 20 | 21 | 按照题意做即可。 -------------------------------------------------------------------------------- /leetcode/0204.Count-Primes/204. Count Primes.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func countPrimes(n int) int { 4 | isNotPrime := make([]bool, n) 5 | for i := 2; i*i < n; i++ { 6 | if isNotPrime[i] { 7 | continue 8 | } 9 | for j := i * i; j < n; j = j + i { 10 | isNotPrime[j] = true 11 | } 12 | } 13 | count := 0 14 | for i := 2; i < n; i++ { 15 | if !isNotPrime[i] { 16 | count++ 17 | } 18 | } 19 | return count 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0204.Count-Primes/README.md: -------------------------------------------------------------------------------- 1 | # [204. Count Primes](https://leetcode.com/problems/count-primes/) 2 | 3 | 4 | ## 题目 5 | 6 | Count the number of prime numbers less than a non-negative number, **n**. 7 | 8 | **Example:** 9 | 10 | Input: 10 11 | Output: 4 12 | Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. 13 | 14 | 15 | ## 题目大意 16 | 17 | 统计所有小于非负整数 n 的质数的数量。 18 | 19 | 20 | ## 解题思路 21 | 22 | - 给出一个数字 n,要求输出小于 n 的所有素数的个数总和。简单题。 23 | -------------------------------------------------------------------------------- /leetcode/0206.Reverse-Linked-List/206. Reverse Linked List.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // ListNode define 8 | type ListNode = structures.ListNode 9 | 10 | /** 11 | * Definition for singly-linked list. 12 | * type ListNode struct { 13 | * Val int 14 | * Next *ListNode 15 | * } 16 | */ 17 | 18 | func reverseList(head *ListNode) *ListNode { 19 | var behind *ListNode 20 | for head != nil { 21 | next := head.Next 22 | head.Next = behind 23 | behind = head 24 | head = next 25 | } 26 | return behind 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0206.Reverse-Linked-List/README.md: -------------------------------------------------------------------------------- 1 | # [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/) 2 | 3 | ## 题目 4 | 5 | Reverse a singly linked list. 6 | 7 | ## 题目大意 8 | 9 | 翻转单链表 10 | 11 | 12 | ## 解题思路 13 | 14 | 按照题意做即可。 -------------------------------------------------------------------------------- /leetcode/0207.Course-Schedule/207. Course Schedule.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // AOV 网的拓扑排序 4 | func canFinish(n int, pre [][]int) bool { 5 | in := make([]int, n) 6 | frees := make([][]int, n) 7 | next := make([]int, 0, n) 8 | for _, v := range pre { 9 | in[v[0]]++ 10 | frees[v[1]] = append(frees[v[1]], v[0]) 11 | } 12 | for i := 0; i < n; i++ { 13 | if in[i] == 0 { 14 | next = append(next, i) 15 | } 16 | } 17 | for i := 0; i != len(next); i++ { 18 | c := next[i] 19 | v := frees[c] 20 | for _, vv := range v { 21 | in[vv]-- 22 | if in[vv] == 0 { 23 | next = append(next, vv) 24 | } 25 | } 26 | } 27 | return len(next) == n 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/0209.Minimum-Size-Subarray-Sum/209. Minimum Size Subarray Sum.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func minSubArrayLen(s int, nums []int) int { 4 | n := len(nums) 5 | if n == 0 { 6 | return 0 7 | } 8 | left, right, res, sum := 0, -1, n+1, 0 9 | for left < n { 10 | if (right+1) < n && sum < s { 11 | right++ 12 | sum += nums[right] 13 | } else { 14 | sum -= nums[left] 15 | left++ 16 | } 17 | if sum >= s { 18 | res = min(res, right-left+1) 19 | } 20 | } 21 | if res == n+1 { 22 | return 0 23 | } 24 | return res 25 | } 26 | 27 | func min(a int, b int) int { 28 | if a > b { 29 | return b 30 | } 31 | return a 32 | } 33 | -------------------------------------------------------------------------------- /leetcode/0217.Contains-Duplicate/217. Contains Duplicate.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func containsDuplicate(nums []int) bool { 4 | record := make(map[int]bool, len(nums)) 5 | for _, n := range nums { 6 | if _, found := record[n]; found { 7 | return true 8 | } 9 | record[n] = true 10 | } 11 | return false 12 | } 13 | -------------------------------------------------------------------------------- /leetcode/0219.Contains-Duplicate-II/219. Contains Duplicate II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func containsNearbyDuplicate(nums []int, k int) bool { 4 | if len(nums) <= 1 { 5 | return false 6 | } 7 | if k <= 0 { 8 | return false 9 | } 10 | record := make(map[int]bool, len(nums)) 11 | for i, n := range nums { 12 | if _, found := record[n]; found { 13 | return true 14 | } 15 | record[n] = true 16 | if len(record) == k+1 { 17 | delete(record, nums[i-k]) 18 | } 19 | } 20 | return false 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0223.Rectangle-Area/223. Rectangle Area.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func computeArea(A int, B int, C int, D int, E int, F int, G int, H int) int { 4 | X0, Y0, X1, Y1 := max(A, E), max(B, F), min(C, G), min(D, H) 5 | return area(A, B, C, D) + area(E, F, G, H) - area(X0, Y0, X1, Y1) 6 | } 7 | 8 | func area(x0, y0, x1, y1 int) int { 9 | l, h := x1-x0, y1-y0 10 | if l <= 0 || h <= 0 { 11 | return 0 12 | } 13 | return l * h 14 | } 15 | 16 | func max(a int, b int) int { 17 | if a > b { 18 | return a 19 | } 20 | return b 21 | } 22 | 23 | func min(a int, b int) int { 24 | if a > b { 25 | return b 26 | } 27 | return a 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/0225.Implement-Stack-using-Queues/225. Implement Stack using Queues_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem225(t *testing.T) { 9 | obj := Constructor225() 10 | fmt.Printf("obj = %v\n", obj) 11 | param5 := obj.Empty() 12 | fmt.Printf("param_5 = %v\n", param5) 13 | obj.Push(2) 14 | fmt.Printf("obj = %v\n", obj) 15 | obj.Push(10) 16 | fmt.Printf("obj = %v\n", obj) 17 | param2 := obj.Pop() 18 | fmt.Printf("param_2 = %v\n", param2) 19 | param3 := obj.Top() 20 | fmt.Printf("param_3 = %v\n", param3) 21 | param4 := obj.Empty() 22 | fmt.Printf("param_4 = %v\n", param4) 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/0226.Invert-Binary-Tree/226. Invert Binary Tree.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // TreeNode define 8 | type TreeNode = structures.TreeNode 9 | 10 | /** 11 | * Definition for a binary tree node. 12 | * type TreeNode struct { 13 | * Val int 14 | * Left *TreeNode 15 | * Right *TreeNode 16 | * } 17 | */ 18 | 19 | func invertTree(root *TreeNode) *TreeNode { 20 | if root == nil { 21 | return nil 22 | } 23 | invertTree(root.Left) 24 | invertTree(root.Right) 25 | root.Left, root.Right = root.Right, root.Left 26 | return root 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0231.Power-of-Two/README.md: -------------------------------------------------------------------------------- 1 | # [231. Power of Two](https://leetcode.com/problems/power-of-two/) 2 | 3 | ## 题目 4 | 5 | Given an integer, write a function to determine if it is a power of two. 6 | 7 | **Example 1:** 8 | 9 | Input: 1 10 | Output: true 11 | Explanation: 2^0 = 1 12 | 13 | **Example 2:** 14 | 15 | Input: 16 16 | Output: true 17 | Explanation: 2^4 = 16 18 | 19 | **Example 3:** 20 | 21 | Input: 218 22 | Output: false 23 | 24 | ## 题目大意 25 | 26 | 给定一个整数,编写一个函数来判断它是否是 2 的幂次方。 27 | 28 | 29 | ## 解题思路 30 | 31 | - 判断一个数是不是 2 的 n 次方。 32 | - 这一题最简单的思路是循环,可以通过。但是题目要求不循环就要判断,这就需要用到数论的知识了。这一题和第 326 题是一样的思路。 33 | -------------------------------------------------------------------------------- /leetcode/0232.Implement-Queue-using-Stacks/232. Implement Queue using Stacks_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem232(t *testing.T) { 9 | obj := Constructor232() 10 | fmt.Printf("obj = %v\n", obj) 11 | obj.Push(2) 12 | fmt.Printf("obj = %v\n", obj) 13 | obj.Push(10) 14 | fmt.Printf("obj = %v\n", obj) 15 | param2 := obj.Pop() 16 | fmt.Printf("param_2 = %v\n", param2) 17 | param3 := obj.Peek() 18 | fmt.Printf("param_3 = %v\n", param3) 19 | param4 := obj.Empty() 20 | fmt.Printf("param_4 = %v\n", param4) 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0234.Palindrome-Linked-List/README.md: -------------------------------------------------------------------------------- 1 | # [234. Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) 2 | 3 | ## 题目 4 | 5 | Given a singly linked list, determine if it is a palindrome. 6 | 7 | Example 1: 8 | 9 | ```c 10 | Input: 1->2 11 | Output: false 12 | ``` 13 | 14 | Example 2: 15 | 16 | ```c 17 | Input: 1->2->2->1 18 | Output: true 19 | ``` 20 | 21 | Follow up: 22 | 23 | Could you do it in O(n) time and O(1) space? 24 | 25 | ## 题目大意 26 | 27 | 判断一个链表是否是回文链表。要求时间复杂度 O(n),空间复杂度 O(1)。 28 | 29 | ## 解题思路 30 | 31 | 这道题只需要在第 143 题上面改改就可以了。思路是完全一致的。先找到中间结点,然后反转中间结点后面到结尾的所有结点。最后一一判断头结点开始的结点和中间结点往后开始的结点是否相等。如果一直相等,就是回文链表,如果有不相等的,直接返回不是回文链表。 -------------------------------------------------------------------------------- /leetcode/0237.Delete-Node-in-a-Linked-List/237. Delete Node in a Linked List.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // ListNode define 8 | type ListNode = structures.ListNode 9 | 10 | /** 11 | * Definition for singly-linked list. 12 | * type ListNode struct { 13 | * Val int 14 | * Next *ListNode 15 | * } 16 | */ 17 | func deleteNode(node *ListNode) { 18 | if node == nil { 19 | return 20 | } 21 | cur := node 22 | for cur.Next.Next != nil { 23 | cur.Val = cur.Next.Val 24 | cur = cur.Next 25 | } 26 | cur.Val = cur.Next.Val 27 | cur.Next = nil 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/0257.Binary-Tree-Paths/README.md: -------------------------------------------------------------------------------- 1 | # [257. Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/) 2 | 3 | 4 | ## 题目 5 | 6 | Given a binary tree, return all root-to-leaf paths. 7 | 8 | **Note:** A leaf is a node with no children. 9 | 10 | **Example:** 11 | 12 | Input: 13 | 14 | 1 15 | / \ 16 | 2 3 17 | \ 18 | 5 19 | 20 | Output: ["1->2->5", "1->3"] 21 | 22 | Explanation: All root-to-leaf paths are: 1->2->5, 1->3 23 | 24 | ## 题目大意 25 | 26 | 给定一个二叉树,返回所有从根节点到叶子节点的路径。说明: 叶子节点是指没有子节点的节点。 27 | 28 | ## 解题思路 29 | 30 | - Google 的面试题,考察递归 31 | 32 | -------------------------------------------------------------------------------- /leetcode/0258.Add-Digits/258. Add Digits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func addDigits(num int) int { 4 | for num > 9 { 5 | cur := 0 6 | for num != 0 { 7 | cur += num % 10 8 | num /= 10 9 | } 10 | num = cur 11 | } 12 | return num 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/0260.Single-Number-III/260. Single Number III.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func singleNumberIII(nums []int) []int { 4 | diff := 0 5 | for _, num := range nums { 6 | diff ^= num 7 | } 8 | // Get its last set bit (lsb) 9 | diff &= -diff 10 | res := []int{0, 0} // this array stores the two numbers we will return 11 | for _, num := range nums { 12 | if (num & diff) == 0 { // the bit is not set 13 | res[0] ^= num 14 | } else { // the bit is set 15 | res[1] ^= num 16 | } 17 | } 18 | return res 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0263.Ugly-Number/263. Ugly Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isUgly(num int) bool { 4 | for i := 2; i < 6 && num > 0; i++ { 5 | for num%i == 0 { 6 | num /= i 7 | } 8 | } 9 | return num == 1 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/0268.Missing-Number/268. Missing Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func missingNumber(nums []int) int { 4 | xor, i := 0, 0 5 | for i = 0; i < len(nums); i++ { 6 | xor = xor ^ i ^ nums[i] 7 | } 8 | return xor ^ i 9 | } 10 | -------------------------------------------------------------------------------- /leetcode/0275.H-Index-II/275. H-Index II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func hIndex275(citations []int) int { 4 | low, high := 0, len(citations)-1 5 | for low <= high { 6 | mid := low + (high-low)>>1 7 | if len(citations)-mid > citations[mid] { 8 | low = mid + 1 9 | } else { 10 | high = mid - 1 11 | } 12 | } 13 | return len(citations) - low 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/0283.Move-Zeroes/283. Move Zeroes.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func moveZeroes(nums []int) { 4 | if len(nums) == 0 { 5 | return 6 | } 7 | j := 0 8 | for i := 0; i < len(nums); i++ { 9 | if nums[i] != 0 { 10 | if i != j { 11 | nums[i], nums[j] = nums[j], nums[i] 12 | } 13 | j++ 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0283.Move-Zeroes/README.md: -------------------------------------------------------------------------------- 1 | # [283. Move Zeroes](https://leetcode.com/problems/move-zeroes/) 2 | 3 | ## 题目 4 | 5 | Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. 6 | 7 | Example 1: 8 | 9 | ```c 10 | Input: [0,1,0,3,12] 11 | Output: [1,3,12,0,0] 12 | ``` 13 | 14 | Note: 15 | 16 | - You must do this in-place without making a copy of the array. 17 | - Minimize the total number of operations. 18 | 19 | 20 | 21 | ## 题目大意 22 | 23 | 题目要求不能采用额外的辅助空间,将数组中 0 元素都移动到数组的末尾,并且维持所有非 0 元素的相对位置。 24 | 25 | ## 解题思路 26 | 27 | 这一题可以只扫描数组一遍,不断的用 i,j 标记 0 和非 0 的元素,然后相互交换,最终到达题目的目的。与这一题相近的题目有第 26 题,第 27 题,第 80 题。 28 | -------------------------------------------------------------------------------- /leetcode/0303.Range-Sum-Query---Immutable/303. Range Sum Query - Immutable_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem303(t *testing.T) { 9 | obj := Constructor303([]int{-2, 0, 3, -5, 2, -1}) 10 | fmt.Printf("obj = %v\n", obj) 11 | fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 2)) // return 1 12 | fmt.Printf("SumRange(2,5) = %v\n", obj.SumRange(2, 5)) // return -1 13 | fmt.Printf("SumRange(0,5) = %v\n", obj.SumRange(0, 5)) // return -3 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/0307.Range-Sum-Query---Mutable/307. Range Sum Query - Mutable_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem307(t *testing.T) { 9 | obj := Constructor307([]int{1, 3, 5}) 10 | fmt.Printf("obj = %v\n", obj) 11 | fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 2)) 12 | obj.Update(1, 2) 13 | fmt.Printf("obj = %v\n", obj) 14 | fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 2)) 15 | } 16 | 17 | // SumRange define 18 | func (ma *NumArray) SumRange(i int, j int) int { 19 | return ma.st.Query(i, j) 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0322.Coin-Change/322. Coin Change.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func coinChange(coins []int, amount int) int { 4 | dp := make([]int, amount+1) 5 | dp[0] = 0 6 | for i := 1; i < len(dp); i++ { 7 | dp[i] = amount + 1 8 | } 9 | for i := 1; i <= amount; i++ { 10 | for j := 0; j < len(coins); j++ { 11 | if coins[j] <= i { 12 | dp[i] = min(dp[i], dp[i-coins[j]]+1) 13 | } 14 | } 15 | } 16 | if dp[amount] > amount { 17 | return -1 18 | } 19 | return dp[amount] 20 | } 21 | 22 | func min(a int, b int) int { 23 | if a > b { 24 | return b 25 | } 26 | return a 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/331. Verify Preorder Serialization of a Binary Tree.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func isValidSerialization(preorder string) bool { 6 | nodes, diff := strings.Split(preorder, ","), 1 7 | for _, node := range nodes { 8 | diff-- 9 | if diff < 0 { 10 | return false 11 | } 12 | if node != "#" { 13 | diff += 2 14 | } 15 | } 16 | return diff == 0 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0338.Counting-Bits/338. Counting Bits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func countBits(num int) []int { 4 | bits := make([]int, num+1) 5 | for i := 1; i <= num; i++ { 6 | bits[i] += bits[i&(i-1)] + 1 7 | } 8 | return bits 9 | } 10 | -------------------------------------------------------------------------------- /leetcode/0342.Power-of-Four/342. Power of Four.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 解法一 数论 4 | func isPowerOfFour(num int) bool { 5 | return num > 0 && (num&(num-1)) == 0 && (num-1)%3 == 0 6 | } 7 | 8 | // 解法二 循环 9 | func isPowerOfFour1(num int) bool { 10 | for num >= 4 { 11 | if num%4 == 0 { 12 | num = num / 4 13 | } else { 14 | return false 15 | } 16 | } 17 | return num == 1 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0343.Integer-Break/343. Integer Break.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func integerBreak(n int) int { 4 | dp := make([]int, n+1) 5 | dp[0], dp[1] = 1, 1 6 | for i := 1; i <= n; i++ { 7 | for j := 1; j < i; j++ { 8 | // dp[i] = max(dp[i], j * (i - j), j*dp[i-j]) 9 | dp[i] = max(dp[i], j*max(dp[i-j], i-j)) 10 | } 11 | } 12 | return dp[n] 13 | } 14 | 15 | func max(a int, b int) int { 16 | if a > b { 17 | return a 18 | } 19 | return b 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0344.Reverse-String/344. Reverse String.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func reverseString(s []byte) { 4 | for i, j := 0, len(s)-1; i < j; { 5 | s[i], s[j] = s[j], s[i] 6 | i++ 7 | j-- 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /leetcode/0345.Reverse-Vowels-of-a-String/README.md: -------------------------------------------------------------------------------- 1 | # [345. Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) 2 | 3 | ## 题目 4 | 5 | Write a function that takes a string as input and reverse only the vowels of a string. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | ```c 12 | Input: "hello" 13 | Output: "holle" 14 | ``` 15 | 16 | Example 2: 17 | 18 | ```c 19 | Input: "leetcode" 20 | Output: "leotcede" 21 | ``` 22 | 23 | ## 题目大意 24 | 25 | 题目要求我们反转字符串中的元音字母。需要注意字母大小写。 26 | 27 | ## 解题思路 28 | 29 | 这一题的解题思路是用 2 个指针,指针对撞的思路,来不断交换首尾元素,即可。这一题和第 344 题思路一样。 30 | 31 | -------------------------------------------------------------------------------- /leetcode/0349.Intersection-of-Two-Arrays/349. Intersection of Two Arrays.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func intersection(nums1 []int, nums2 []int) []int { 4 | m := map[int]bool{} 5 | var res []int 6 | for _, n := range nums1 { 7 | m[n] = true 8 | } 9 | for _, n := range nums2 { 10 | if m[n] { 11 | delete(m, n) 12 | res = append(res, n) 13 | } 14 | } 15 | return res 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0350.Intersection-of-Two-Arrays-II/350. Intersection of Two Arrays II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func intersect(nums1 []int, nums2 []int) []int { 4 | m := map[int]int{} 5 | var res []int 6 | for _, n := range nums1 { 7 | m[n]++ 8 | } 9 | for _, n := range nums2 { 10 | if m[n] > 0 { 11 | res = append(res, n) 12 | m[n]-- 13 | } 14 | } 15 | return res 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0357.Count-Numbers-with-Unique-Digits/357. Count Numbers with Unique Digits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 暴力打表法 4 | func countNumbersWithUniqueDigits1(n int) int { 5 | res := []int{1, 10, 91, 739, 5275, 32491, 168571, 712891, 2345851, 5611771, 8877691} 6 | if n >= 10 { 7 | return res[10] 8 | } 9 | return res[n] 10 | } 11 | 12 | // 打表方法 13 | func countNumbersWithUniqueDigits(n int) int { 14 | if n == 0 { 15 | return 1 16 | } 17 | res, uniqueDigits, availableNumber := 10, 9, 9 18 | for n > 1 && availableNumber > 0 { 19 | uniqueDigits = uniqueDigits * availableNumber 20 | res += uniqueDigits 21 | availableNumber-- 22 | n-- 23 | } 24 | return res 25 | } 26 | -------------------------------------------------------------------------------- /leetcode/0367.Valid-Perfect-Square/367. Valid Perfect Square.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isPerfectSquare(num int) bool { 4 | low, high := 1, num 5 | for low <= high { 6 | mid := low + (high-low)>>1 7 | if mid*mid == num { 8 | return true 9 | } else if mid*mid < num { 10 | low = mid + 1 11 | } else { 12 | high = mid - 1 13 | } 14 | } 15 | return false 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0371.Sum-of-Two-Integers/371. Sum of Two Integers.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func getSum(a int, b int) int { 4 | if a == 0 { 5 | return b 6 | } 7 | if b == 0 { 8 | return a 9 | } 10 | // (a & b)<<1 计算的是进位 11 | // a ^ b 计算的是不带进位的加法 12 | return getSum((a&b)<<1, a^b) 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/0386.Lexicographical-Numbers/386. Lexicographical Numbers.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func lexicalOrder(n int) []int { 4 | res := make([]int, 0, n) 5 | dfs386(1, n, &res) 6 | return res 7 | } 8 | 9 | func dfs386(x, n int, res *[]int) { 10 | limit := (x + 10) / 10 * 10 11 | for x <= n && x < limit { 12 | *res = append(*res, x) 13 | if x*10 <= n { 14 | dfs386(x*10, n, res) 15 | } 16 | x++ 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0386.Lexicographical-Numbers/README.md: -------------------------------------------------------------------------------- 1 | # [386. Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/) 2 | 3 | 4 | ## 题目 5 | 6 | Given an integer n, return 1 - n in lexicographical order. 7 | 8 | For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. 9 | 10 | Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000. 11 | 12 | 13 | ## 题目大意 14 | 15 | 给定一个整数 n, 返回从 1 到 n 的字典顺序。例如,给定 n =13,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] 。 16 | 17 | 请尽可能的优化算法的时间复杂度和空间复杂度。 输入的数据 n 小于等于 5,000,000。 18 | 19 | 20 | 21 | ## 解题思路 22 | 23 | 24 | - 给出一个数字 n ,要求按照字典序对 1-n 这 n 个数排序。 25 | - DFS 暴力求解即可。 26 | -------------------------------------------------------------------------------- /leetcode/0387.First-Unique-Character-in-a-String/387. First Unique Character in a String.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func firstUniqChar(s string) int { 4 | result := make([]int, 26) 5 | for i := 0; i < len(s); i++ { 6 | result[s[i]-'a']++ 7 | } 8 | for i := 0; i < len(s); i++ { 9 | if result[s[i]-'a'] == 1 { 10 | return i 11 | } 12 | } 13 | return -1 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/0387.First-Unique-Character-in-a-String/README.md: -------------------------------------------------------------------------------- 1 | # [387. First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) 2 | 3 | ## 题目 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 | 21 | 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 22 | 23 | 24 | ## 解题思路 25 | 26 | - 简单题,要求输出第一个没有重复的字符。 27 | -------------------------------------------------------------------------------- /leetcode/0389.Find-the-Difference/389. Find the Difference.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findTheDifference(s string, t string) byte { 4 | n, ch := len(t), t[len(t)-1] 5 | for i := 0; i < n-1; i++ { 6 | ch ^= s[i] 7 | ch ^= t[i] 8 | } 9 | return ch 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/0392.Is-Subsequence/392. Is Subsequence.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 解法一 O(n^2) 4 | func isSubsequence(s string, t string) bool { 5 | index := 0 6 | for i := 0; i < len(s); i++ { 7 | flag := false 8 | for ; index < len(t); index++ { 9 | if s[i] == t[index] { 10 | flag = true 11 | break 12 | } 13 | } 14 | if flag == true { 15 | index++ 16 | continue 17 | } else { 18 | return false 19 | } 20 | } 21 | return true 22 | } 23 | 24 | // 解法二 O(n) 25 | func isSubsequence1(s string, t string) bool { 26 | for len(s) > 0 && len(t) > 0 { 27 | if s[0] == t[0] { 28 | s = s[1:] 29 | } 30 | t = t[1:] 31 | } 32 | return len(s) == 0 33 | } 34 | -------------------------------------------------------------------------------- /leetcode/0393.UTF-8-Validation/393. UTF-8 Validation.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func validUtf8(data []int) bool { 4 | count := 0 5 | for _, d := range data { 6 | if count == 0 { 7 | if d >= 248 { // 11111000 = 248 8 | return false 9 | } else if d >= 240 { // 11110000 = 240 10 | count = 3 11 | } else if d >= 224 { // 11100000 = 224 12 | count = 2 13 | } else if d >= 192 { // 11000000 = 192 14 | count = 1 15 | } else if d > 127 { // 01111111 = 127 16 | return false 17 | } 18 | } else { 19 | if d <= 127 || d >= 192 { 20 | return false 21 | } 22 | count-- 23 | } 24 | } 25 | return count == 0 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/0397.Integer-Replacement/397. Integer Replacement.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func integerReplacement(n int) int { 4 | res := 0 5 | for n > 1 { 6 | if (n & 1) == 0 { // 判断是否是偶数 7 | n >>= 1 8 | } else if (n+1)%4 == 0 && n != 3 { // 末尾 2 位为 11 9 | n++ 10 | } else { // 末尾 2 位为 01 11 | n-- 12 | } 13 | res++ 14 | } 15 | return res 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0402.Remove-K-Digits/402. Remove K Digits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func removeKdigits(num string, k int) string { 4 | if k == len(num) { 5 | return "0" 6 | } 7 | res := []byte{} 8 | for i := 0; i < len(num); i++ { 9 | c := num[i] 10 | for k > 0 && len(res) > 0 && c < res[len(res)-1] { 11 | res = res[:len(res)-1] 12 | k-- 13 | } 14 | res = append(res, c) 15 | } 16 | res = res[:len(res)-k] 17 | 18 | // trim leading zeros 19 | for len(res) > 1 && res[0] == '0' { 20 | res = res[1:] 21 | } 22 | return string(res) 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/0404.Sum-of-Left-Leaves/README.md: -------------------------------------------------------------------------------- 1 | # [404. Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) 2 | 3 | 4 | ## 题目 5 | 6 | Find the sum of all left leaves in a given binary tree. 7 | 8 | **Example:** 9 | 10 | 3 11 | / \ 12 | 9 20 13 | / \ 14 | 15 7 15 | 16 | There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 17 | 18 | 19 | ## 题目大意 20 | 21 | 计算给定二叉树的所有左叶子之和。 22 | 23 | 24 | ## 解题思路 25 | 26 | 27 | - 这一题是微软的面试题。递归求解即可 28 | 29 | -------------------------------------------------------------------------------- /leetcode/0405.Convert-a-Number-to-Hexadecimal/405. Convert a Number to Hexadecimal.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func toHex(num int) string { 4 | if num == 0 { 5 | return "0" 6 | } 7 | if num < 0 { 8 | num += 1 << 32 9 | } 10 | mp := map[int]string{ 11 | 0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 12 | 10: "a", 11: "b", 12: "c", 13: "d", 14: "e", 15: "f", 13 | } 14 | var bitArr []string 15 | for num > 0 { 16 | bitArr = append(bitArr, mp[num%16]) 17 | num /= 16 18 | } 19 | str := "" 20 | for i := len(bitArr) - 1; i >= 0; i-- { 21 | str += bitArr[i] 22 | } 23 | return str 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0409.Longest-Palindrome/409. Longest Palindrome.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func longestPalindrome(s string) int { 4 | counter := make(map[rune]int) 5 | for _, r := range s { 6 | counter[r]++ 7 | } 8 | answer := 0 9 | for _, v := range counter { 10 | answer += v / 2 * 2 11 | if answer%2 == 0 && v%2 == 1 { 12 | answer++ 13 | } 14 | } 15 | return answer 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0412.Fizz-Buzz/412. Fizz Buzz.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strconv" 4 | 5 | func fizzBuzz(n int) []string { 6 | if n < 0 { 7 | return []string{} 8 | } 9 | solution := make([]string, n) 10 | for i := 1; i <= n; i++ { 11 | if i%3 == 0 && i%5 == 0 { 12 | solution[i-1] = "FizzBuzz" 13 | } else if i%3 == 0 { 14 | solution[i-1] = "Fizz" 15 | } else if i%5 == 0 { 16 | solution[i-1] = "Buzz" 17 | } else { 18 | solution[i-1] = strconv.Itoa(i) 19 | } 20 | } 21 | return solution 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0414.Third-Maximum-Number/414. Third Maximum Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "math" 5 | ) 6 | 7 | func thirdMax(nums []int) int { 8 | a, b, c := math.MinInt64, math.MinInt64, math.MinInt64 9 | for _, v := range nums { 10 | if v > a { 11 | c = b 12 | b = a 13 | a = v 14 | } else if v < a && v > b { 15 | c = b 16 | b = v 17 | } else if v < b && v > c { 18 | c = v 19 | } 20 | } 21 | if c == math.MinInt64 { 22 | return a 23 | } 24 | return c 25 | } 26 | -------------------------------------------------------------------------------- /leetcode/0416.Partition-Equal-Subset-Sum/416. Partition Equal Subset Sum.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func canPartition(nums []int) bool { 4 | sum := 0 5 | for _, v := range nums { 6 | sum += v 7 | } 8 | if sum%2 != 0 { 9 | return false 10 | } 11 | // C = half sum 12 | n, C, dp := len(nums), sum/2, make([]bool, sum/2+1) 13 | for i := 0; i <= C; i++ { 14 | dp[i] = (nums[0] == i) 15 | } 16 | for i := 1; i < n; i++ { 17 | for j := C; j >= nums[i]; j-- { 18 | dp[j] = dp[j] || dp[j-nums[i]] 19 | } 20 | } 21 | return dp[C] 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0424.Longest-Repeating-Character-Replacement/424. Longest Repeating Character Replacement.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func characterReplacement(s string, k int) int { 4 | res, left, counter, freq := 0, 0, 0, make([]int, 26) 5 | for right := 0; right < len(s); right++ { 6 | freq[s[right]-'A']++ 7 | counter = max(counter, freq[s[right]-'A']) 8 | for right-left+1-counter > k { 9 | freq[s[left]-'A']-- 10 | left++ 11 | } 12 | res = max(res, right-left+1) 13 | } 14 | return res 15 | } 16 | 17 | func max(a int, b int) int { 18 | if a > b { 19 | return a 20 | } 21 | return b 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0441.Arranging-Coins/441. Arranging Coins.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math" 4 | 5 | // 解法一 数学公式 6 | func arrangeCoins(n int) int { 7 | if n <= 0 { 8 | return 0 9 | } 10 | x := math.Sqrt(2*float64(n)+0.25) - 0.5 11 | return int(x) 12 | } 13 | 14 | // 解法二 模拟 15 | func arrangeCoins1(n int) int { 16 | k := 1 17 | for n >= k { 18 | n -= k 19 | k++ 20 | } 21 | return k - 1 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0447.Number-of-Boomerangs/447. Number of Boomerangs.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func numberOfBoomerangs(points [][]int) int { 4 | res := 0 5 | for i := 0; i < len(points); i++ { 6 | record := make(map[int]int, len(points)) 7 | for j := 0; j < len(points); j++ { 8 | if j != i { 9 | record[dis(points[i], points[j])]++ 10 | } 11 | } 12 | for _, r := range record { 13 | res += r * (r - 1) 14 | } 15 | } 16 | return res 17 | } 18 | 19 | func dis(pa, pb []int) int { 20 | return (pa[0]-pb[0])*(pa[0]-pb[0]) + (pa[1]-pb[1])*(pa[1]-pb[1]) 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0448.Find-All-Numbers-Disappeared-in-an-Array/448. Find All Numbers Disappeared in an Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findDisappearedNumbers(nums []int) []int { 4 | res := []int{} 5 | for _, v := range nums { 6 | if v < 0 { 7 | v = -v 8 | } 9 | if nums[v-1] > 0 { 10 | nums[v-1] = -nums[v-1] 11 | } 12 | } 13 | for i, v := range nums { 14 | if v > 0 { 15 | res = append(res, i+1) 16 | } 17 | } 18 | return res 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0453.Minimum-Moves-to-Equal-Array-Elements/453. Minimum Moves to Equal Array Elements.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math" 4 | 5 | func minMoves(nums []int) int { 6 | sum, min, l := 0, math.MaxInt32, len(nums) 7 | for _, v := range nums { 8 | sum += v 9 | if min > v { 10 | min = v 11 | } 12 | } 13 | return sum - min*l 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/0454.4Sum-II/454. 4Sum II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func fourSumCount(A []int, B []int, C []int, D []int) int { 4 | m := make(map[int]int, len(A)*len(B)) 5 | for _, a := range A { 6 | for _, b := range B { 7 | m[a+b]++ 8 | } 9 | } 10 | ret := 0 11 | for _, c := range C { 12 | for _, d := range D { 13 | ret += m[0-c-d] 14 | } 15 | } 16 | 17 | return ret 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0455.Assign-Cookies/455. Assign Cookies.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "sort" 4 | 5 | func findContentChildren(g []int, s []int) int { 6 | sort.Ints(g) 7 | sort.Ints(s) 8 | gi, si, res := 0, 0, 0 9 | for gi < len(g) && si < len(s) { 10 | if s[si] >= g[gi] { 11 | res++ 12 | si++ 13 | gi++ 14 | } else { 15 | si++ 16 | } 17 | } 18 | return res 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0461.Hamming-Distance/461. Hamming Distance.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func hammingDistance(x int, y int) int { 4 | distance := 0 5 | for xor := x ^ y; xor != 0; xor &= (xor - 1) { 6 | distance++ 7 | } 8 | return distance 9 | } 10 | -------------------------------------------------------------------------------- /leetcode/0463.Island-Perimeter/463. Island Perimeter.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func islandPerimeter(grid [][]int) int { 4 | counter := 0 5 | for i := 0; i < len(grid); i++ { 6 | for j := 0; j < len(grid[0]); j++ { 7 | if grid[i][j] == 1 { 8 | if i-1 < 0 || grid[i-1][j] == 0 { 9 | counter++ 10 | } 11 | if i+1 >= len(grid) || grid[i+1][j] == 0 { 12 | counter++ 13 | } 14 | if j-1 < 0 || grid[i][j-1] == 0 { 15 | counter++ 16 | } 17 | if j+1 >= len(grid[0]) || grid[i][j+1] == 0 { 18 | counter++ 19 | } 20 | } 21 | } 22 | } 23 | return counter 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0470.Implement-Rand10-Using-Rand7/470. Implement Rand10() Using Rand7().go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math/rand" 4 | 5 | func rand10() int { 6 | rand10 := 10 7 | for rand10 >= 10 { 8 | rand10 = (rand7() - 1) + rand7() 9 | } 10 | return rand10%10 + 1 11 | } 12 | 13 | func rand7() int { 14 | return rand.Intn(7) 15 | } 16 | 17 | func rand101() int { 18 | rand40 := 40 19 | for rand40 >= 40 { 20 | rand40 = (rand7()-1)*7 + rand7() - 1 21 | } 22 | return rand40%10 + 1 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/0474.Ones-and-Zeroes/474. Ones and Zeroes.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func findMaxForm(strs []string, m int, n int) int { 6 | dp := make([][]int, m+1) 7 | for i := 0; i < m+1; i++ { 8 | dp[i] = make([]int, n+1) 9 | } 10 | for _, s := range strs { 11 | zero := strings.Count(s, "0") 12 | one := len(s) - zero 13 | if zero > m || one > n { 14 | continue 15 | } 16 | for i := m; i >= zero; i-- { 17 | for j := n; j >= one; j-- { 18 | dp[i][j] = max(dp[i][j], 1+dp[i-zero][j-one]) 19 | } 20 | } 21 | } 22 | return dp[m][n] 23 | } 24 | 25 | func max(a int, b int) int { 26 | if a > b { 27 | return a 28 | } 29 | return b 30 | } 31 | -------------------------------------------------------------------------------- /leetcode/0476.Number-Complement/476. Number Complement.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 解法一 4 | func findComplement(num int) int { 5 | xx := ^0 // ^0 = 1111111111111111111111 6 | for xx&num > 0 { 7 | xx <<= 1 // 构造出来的 xx = 1111111…000000,0 的个数就是 num 的长度 8 | } 9 | return ^xx ^ num // xx ^ num,结果是前面的 0 全是 1 的num,再取反即是答案 10 | } 11 | 12 | // 解法二 13 | func findComplement1(num int) int { 14 | temp := 1 15 | for temp <= num { 16 | temp <<= 1 // 构造出来的 temp = 00000……10000,末尾 0 的个数是 num 的长度 17 | } 18 | return (temp - 1) ^ num // temp - 1 即是前面都是 0,num 长度的末尾都是 1 的数,再异或 num 即是最终结果 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0485.Max-Consecutive-Ones/485. Max Consecutive Ones.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findMaxConsecutiveOnes(nums []int) int { 4 | maxCount, currentCount := 0, 0 5 | for _, v := range nums { 6 | if v == 1 { 7 | currentCount++ 8 | } else { 9 | currentCount = 0 10 | } 11 | if currentCount > maxCount { 12 | maxCount = currentCount 13 | } 14 | } 15 | return maxCount 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0496.Next-Greater-Element-I/496. Next Greater Element I.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func nextGreaterElement(nums1 []int, nums2 []int) []int { 4 | if len(nums1) == 0 || len(nums2) == 0 { 5 | return []int{} 6 | } 7 | res, reocrd := []int{}, map[int]int{} 8 | for i, v := range nums2 { 9 | reocrd[v] = i 10 | } 11 | for i := 0; i < len(nums1); i++ { 12 | flag := false 13 | for j := reocrd[nums1[i]]; j < len(nums2); j++ { 14 | if nums2[j] > nums1[i] { 15 | res = append(res, nums2[j]) 16 | flag = true 17 | break 18 | } 19 | } 20 | if flag == false { 21 | res = append(res, -1) 22 | } 23 | } 24 | return res 25 | } 26 | -------------------------------------------------------------------------------- /leetcode/0497.Random-Point-in-Non-overlapping-Rectangles/497. Random Point in Non-overlapping Rectangles_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem497(t *testing.T) { 9 | w := [][]int{{1, 1, 5, 5}} 10 | sol := Constructor497(w) 11 | fmt.Printf("1.Pick = %v\n", sol.Pick()) 12 | fmt.Printf("2.Pick = %v\n", sol.Pick()) 13 | fmt.Printf("3.Pick = %v\n", sol.Pick()) 14 | fmt.Printf("4.Pick = %v\n", sol.Pick()) 15 | fmt.Printf("5.Pick = %v\n", sol.Pick()) 16 | fmt.Printf("6.Pick = %v\n", sol.Pick()) 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0500.Keyboard-Row/500. Keyboard Row.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func findWords500(words []string) []string { 6 | rows := []string{"qwertyuiop", "asdfghjkl", "zxcvbnm"} 7 | output := make([]string, 0) 8 | for _, s := range words { 9 | if len(s) == 0 { 10 | continue 11 | } 12 | lowerS := strings.ToLower(s) 13 | oneRow := false 14 | for _, r := range rows { 15 | if strings.ContainsAny(lowerS, r) { 16 | oneRow = !oneRow 17 | if !oneRow { 18 | break 19 | } 20 | } 21 | } 22 | if oneRow { 23 | output = append(output, s) 24 | } 25 | } 26 | return output 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0507.Perfect-Number/507. Perfect Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math" 4 | 5 | // 方法一 6 | func checkPerfectNumber(num int) bool { 7 | if num <= 1 { 8 | return false 9 | } 10 | sum, bound := 1, int(math.Sqrt(float64(num)))+1 11 | for i := 2; i < bound; i++ { 12 | if num%i != 0 { 13 | continue 14 | } 15 | corrDiv := num / i 16 | sum += corrDiv + i 17 | } 18 | return sum == num 19 | } 20 | 21 | // 方法二 打表 22 | func checkPerfectNumber_(num int) bool { 23 | return num == 6 || num == 28 || num == 496 || num == 8128 || num == 33550336 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0515.Find-Largest-Value-in-Each-Tree-Row/README.md: -------------------------------------------------------------------------------- 1 | # [515. Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/) 2 | 3 | 4 | ## 题目 5 | 6 | You need to find the largest value in each row of a binary tree. 7 | 8 | **Example:** 9 | 10 | Input: 11 | 12 | 1 13 | / \ 14 | 3 2 15 | / \ \ 16 | 5 3 9 17 | 18 | Output: [1, 3, 9] 19 | 20 | 21 | ## 题目大意 22 | 23 | 求在二叉树的每一行中找到最大的值。 24 | 25 | 26 | ## 解题思路 27 | 28 | 29 | - 给出一个二叉树,要求依次输出每行的最大值 30 | - 用 BFS 层序遍历,将每层排序取出最大值。改进的做法是遍历中不断更新每层的最大值。 31 | 32 | -------------------------------------------------------------------------------- /leetcode/0524.Longest-Word-in-Dictionary-through-Deleting/524. Longest Word in Dictionary through Deleting.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findLongestWord(s string, d []string) string { 4 | res := "" 5 | for i := 0; i < len(d); i++ { 6 | pointS := 0 7 | pointD := 0 8 | for pointS < len(s) && pointD < len(d[i]) { 9 | if s[pointS] == d[i][pointD] { 10 | pointD++ 11 | } 12 | pointS++ 13 | } 14 | if pointD == len(d[i]) && (len(res) < len(d[i]) || (len(res) == len(d[i]) && res > d[i])) { 15 | res = d[i] 16 | } 17 | } 18 | return res 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0528.Random-Pick-with-Weight/528. Random Pick with Weight_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem528(t *testing.T) { 9 | w := []int{1, 3} 10 | sol := Constructor528(w) 11 | fmt.Printf("1.PickIndex = %v\n", sol.PickIndex()) 12 | fmt.Printf("2.PickIndex = %v\n", sol.PickIndex()) 13 | fmt.Printf("3.PickIndex = %v\n", sol.PickIndex()) 14 | fmt.Printf("4.PickIndex = %v\n", sol.PickIndex()) 15 | fmt.Printf("5.PickIndex = %v\n", sol.PickIndex()) 16 | fmt.Printf("6.PickIndex = %v\n", sol.PickIndex()) 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0532.K-diff-Pairs-in-an-Array/532. K-diff Pairs in an Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findPairs(nums []int, k int) int { 4 | if k < 0 || len(nums) == 0 { 5 | return 0 6 | } 7 | var count int 8 | m := make(map[int]int, len(nums)) 9 | for _, value := range nums { 10 | m[value]++ 11 | } 12 | for key := range m { 13 | if k == 0 && m[key] > 1 { 14 | count++ 15 | continue 16 | } 17 | if k > 0 && m[key+k] > 0 { 18 | count++ 19 | } 20 | } 21 | return count 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0537.Complex-Number-Multiplication/537. Complex Number Multiplication.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "strconv" 5 | "strings" 6 | ) 7 | 8 | func complexNumberMultiply(a string, b string) string { 9 | realA, imagA := parse(a) 10 | realB, imagB := parse(b) 11 | real := realA*realB - imagA*imagB 12 | imag := realA*imagB + realB*imagA 13 | return strconv.Itoa(real) + "+" + strconv.Itoa(imag) + "i" 14 | } 15 | 16 | func parse(s string) (int, int) { 17 | ss := strings.Split(s, "+") 18 | r, _ := strconv.Atoi(ss[0]) 19 | i, _ := strconv.Atoi(ss[1][:len(ss[1])-1]) 20 | return r, i 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0541.Reverse-String-II/541. Reverse String II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func reverseStr(s string, k int) string { 4 | if k > len(s) { 5 | k = len(s) 6 | } 7 | for i := 0; i < len(s); i = i + 2*k { 8 | if len(s)-i >= k { 9 | ss := revers(s[i : i+k]) 10 | s = s[:i] + ss + s[i+k:] 11 | } else { 12 | ss := revers(s[i:]) 13 | s = s[:i] + ss 14 | } 15 | } 16 | return s 17 | } 18 | 19 | func revers(s string) string { 20 | bytes := []byte(s) 21 | i, j := 0, len(bytes)-1 22 | for i < j { 23 | bytes[i], bytes[j] = bytes[j], bytes[i] 24 | i++ 25 | j-- 26 | } 27 | return string(bytes) 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/0557.Reverse-Words-in-a-String-III/557. Reverse Words in a String III.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "strings" 5 | ) 6 | 7 | func reverseWords(s string) string { 8 | ss := strings.Split(s, " ") 9 | for i, s := range ss { 10 | ss[i] = revers(s) 11 | } 12 | return strings.Join(ss, " ") 13 | } 14 | 15 | func revers(s string) string { 16 | bytes := []byte(s) 17 | i, j := 0, len(bytes)-1 18 | for i < j { 19 | bytes[i], bytes[j] = bytes[j], bytes[i] 20 | i++ 21 | j-- 22 | } 23 | return string(bytes) 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0561.Array-Partition-I/561. Array Partition I.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func arrayPairSum(nums []int) int { 4 | array := [20001]int{} 5 | for i := 0; i < len(nums); i++ { 6 | array[nums[i]+10000]++ 7 | } 8 | flag, sum := true, 0 9 | for i := 0; i < len(array); i++ { 10 | for array[i] > 0 { 11 | if flag { 12 | sum = sum + i - 10000 13 | } 14 | flag = !flag 15 | array[i]-- 16 | } 17 | } 18 | return sum 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0575.Distribute-Candies/575. Distribute Candies.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func distributeCandies(candies []int) int { 4 | n, m := len(candies), make(map[int]struct{}, len(candies)) 5 | for _, candy := range candies { 6 | m[candy] = struct{}{} 7 | } 8 | res := len(m) 9 | if n/2 < res { 10 | return n / 2 11 | } 12 | return res 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/0594.Longest-Harmonious-Subsequence/594. Longest Harmonious Subsequence.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findLHS(nums []int) int { 4 | if len(nums) < 2 { 5 | return 0 6 | } 7 | res := make(map[int]int, len(nums)) 8 | for _, num := range nums { 9 | if _, exist := res[num]; exist { 10 | res[num]++ 11 | continue 12 | } 13 | res[num] = 1 14 | } 15 | longest := 0 16 | for k, c := range res { 17 | if n, exist := res[k+1]; exist { 18 | if c+n > longest { 19 | longest = c + n 20 | } 21 | } 22 | } 23 | return longest 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0598.Range-Addition-II/598. Range Addition II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maxCount(m int, n int, ops [][]int) int { 4 | minM, minN := m, n 5 | for _, op := range ops { 6 | minM = min(minM, op[0]) 7 | minN = min(minN, op[1]) 8 | } 9 | return minM * minN 10 | } 11 | 12 | func min(a, b int) int { 13 | if a < b { 14 | return a 15 | } 16 | return b 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0599.Minimum-Index-Sum-of-Two-Lists/599. Minimum Index Sum of Two Lists.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findRestaurant(list1 []string, list2 []string) []string { 4 | m, ans := make(map[string]int, len(list1)), []string{} 5 | for i, r := range list1 { 6 | m[r] = i 7 | } 8 | for j, r := range list2 { 9 | if _, ok := m[r]; ok { 10 | m[r] += j 11 | if len(ans) == 0 || m[r] == m[ans[0]] { 12 | ans = append(ans, r) 13 | } else if m[r] < m[ans[0]] { 14 | ans = []string{r} 15 | } 16 | } 17 | } 18 | return ans 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0633.Sum-of-Square-Numbers/633. Sum of Square Numbers.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math" 4 | 5 | func judgeSquareSum(c int) bool { 6 | low, high := 0, int(math.Sqrt(float64(c))) 7 | for low <= high { 8 | if low*low+high*high < c { 9 | low++ 10 | } else if low*low+high*high > c { 11 | high-- 12 | } else { 13 | return true 14 | } 15 | } 16 | return false 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0645.Set-Mismatch/645. Set Mismatch.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findErrorNums(nums []int) []int { 4 | m, res := make([]int, len(nums)), make([]int, 2) 5 | for _, n := range nums { 6 | if m[n-1] == 0 { 7 | m[n-1] = 1 8 | } else { 9 | res[0] = n 10 | } 11 | } 12 | for i := range m { 13 | if m[i] == 0 { 14 | res[1] = i + 1 15 | break 16 | } 17 | } 18 | return res 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0658.Find-K-Closest-Elements/658. Find K Closest Elements.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "sort" 4 | 5 | // 解法一 库函数二分搜索 6 | func findClosestElements(arr []int, k int, x int) []int { 7 | return arr[sort.Search(len(arr)-k, func(i int) bool { return x-arr[i] <= arr[i+k]-x }):][:k] 8 | } 9 | 10 | // 解法二 手撸二分搜索 11 | func findClosestElements1(arr []int, k int, x int) []int { 12 | low, high := 0, len(arr)-k 13 | for low < high { 14 | mid := low + (high-low)>>1 15 | if x-arr[mid] > arr[mid+k]-x { 16 | low = mid + 1 17 | } else { 18 | high = mid 19 | } 20 | } 21 | return arr[low : low+k] 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0668.Kth-Smallest-Number-in-Multiplication-Table/668. Kth Smallest Number in Multiplication Table.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math" 4 | 5 | func findKthNumber(m int, n int, k int) int { 6 | low, high := 1, m*n 7 | for low < high { 8 | mid := low + (high-low)>>1 9 | if counterKthNum(m, n, mid) >= k { 10 | high = mid 11 | } else { 12 | low = mid + 1 13 | } 14 | } 15 | return low 16 | } 17 | 18 | func counterKthNum(m, n, mid int) int { 19 | count := 0 20 | for i := 1; i <= m; i++ { 21 | count += int(math.Min(math.Floor(float64(mid)/float64(i)), float64(n))) 22 | } 23 | return count 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0676.Implement-Magic-Dictionary/676. Implement Magic Dictionary_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem676(t *testing.T) { 9 | dict := []string{"hello", "leetcode"} 10 | obj := Constructor676() 11 | obj.BuildDict(dict) 12 | fmt.Printf("obj = %v\n", obj) 13 | fmt.Println(obj.Search("hello")) 14 | fmt.Println(obj.Search("apple")) 15 | fmt.Println(obj.Search("leetcode")) 16 | fmt.Println(obj.Search("leetcoded")) 17 | fmt.Println(obj.Search("hhllo")) 18 | fmt.Println(obj.Search("hell")) 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0684.Redundant-Connection/684. Redundant Connection.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/template" 5 | ) 6 | 7 | func findRedundantConnection(edges [][]int) []int { 8 | if len(edges) == 0 { 9 | return []int{} 10 | } 11 | uf, res := template.UnionFind{}, []int{} 12 | uf.Init(len(edges) + 1) 13 | for i := 0; i < len(edges); i++ { 14 | if uf.Find(edges[i][0]) != uf.Find(edges[i][1]) { 15 | uf.Union(edges[i][0], edges[i][1]) 16 | } else { 17 | res = append(res, edges[i][0]) 18 | res = append(res, edges[i][1]) 19 | } 20 | } 21 | return res 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0704.Binary-Search/704. Binary Search.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func search704(nums []int, target int) int { 4 | low, high := 0, len(nums)-1 5 | for low <= high { 6 | mid := low + (high-low)>>1 7 | if nums[mid] == target { 8 | return mid 9 | } else if nums[mid] > target { 10 | high = mid - 1 11 | } else { 12 | low = mid + 1 13 | } 14 | } 15 | return -1 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0706.Design-HashMap/706. Design HashMap_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem706(t *testing.T) { 9 | obj := Constructor706() 10 | obj.Put(7, 10) 11 | fmt.Printf("Get 7 = %v\n", obj.Get(7)) 12 | obj.Put(7, 20) 13 | fmt.Printf("Contains 7 = %v\n", obj.Get(7)) 14 | param1 := obj.Get(100) 15 | fmt.Printf("param1 = %v\n", param1) 16 | obj.Remove(7) 17 | param1 = obj.Get(7) 18 | fmt.Printf("param1 = %v\n", param1) 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0713.Subarray-Product-Less-Than-K/713. Subarray Product Less Than K.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func numSubarrayProductLessThanK(nums []int, k int) int { 4 | if len(nums) == 0 { 5 | return 0 6 | } 7 | res, left, right, prod := 0, 0, 0, 1 8 | for left < len(nums) { 9 | if right < len(nums) && prod*nums[right] < k { 10 | prod = prod * nums[right] 11 | right++ 12 | } else if left == right { 13 | left++ 14 | right++ 15 | } else { 16 | res += right - left 17 | prod = prod / nums[left] 18 | left++ 19 | } 20 | } 21 | return res 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0717.1-bit-and-2-bit-Characters/717. 1-bit and 2-bit Characters.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isOneBitCharacter(bits []int) bool { 4 | var i int 5 | for i = 0; i < len(bits)-1; i++ { 6 | if bits[i] == 1 { 7 | i++ 8 | } 9 | } 10 | return i == len(bits)-1 11 | } 12 | -------------------------------------------------------------------------------- /leetcode/0720.Longest-Word-in-Dictionary/720. Longest Word in Dictionary.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func longestWord(words []string) string { 8 | sort.Strings(words) 9 | mp := make(map[string]bool) 10 | var res string 11 | for _, word := range words { 12 | size := len(word) 13 | if size == 1 || mp[word[:size-1]] { 14 | if size > len(res) { 15 | res = word 16 | } 17 | mp[word] = true 18 | } 19 | } 20 | return res 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0724.Find-Pivot-Index/724. Find Pivot Index.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | // 2 * leftSum + num[i] = sum 4 | // 时间: O(n) 5 | // 空间: O(1) 6 | func pivotIndex(nums []int) int { 7 | if len(nums) <= 0 { 8 | return -1 9 | } 10 | var sum, leftSum int 11 | for _, num := range nums { 12 | sum += num 13 | } 14 | for index, num := range nums { 15 | if leftSum*2+num == sum { 16 | return index 17 | } 18 | leftSum += num 19 | } 20 | return -1 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0732.My-Calendar-III/732. My Calendar III_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem732(t *testing.T) { 9 | obj := Constructor732() 10 | fmt.Printf("book = %v\n\n", obj.Book(10, 20)) // returns 1 11 | fmt.Printf("book = %v\n\n", obj.Book(50, 60)) // returns 1 12 | fmt.Printf("book = %v\n\n", obj.Book(10, 40)) // returns 2 13 | fmt.Printf("book = %v\n\n", obj.Book(5, 15)) // returns 3 14 | fmt.Printf("book = %v\n\n", obj.Book(5, 10)) // returns 3 15 | fmt.Printf("book = %v\n\n", obj.Book(25, 55)) // returns 3 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0735.Asteroid-Collision/735. Asteroid Collision.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func asteroidCollision(asteroids []int) []int { 4 | res := []int{} 5 | for _, v := range asteroids { 6 | for len(res) != 0 && res[len(res)-1] > 0 && res[len(res)-1] < -v { 7 | res = res[:len(res)-1] 8 | } 9 | if len(res) == 0 || v > 0 || res[len(res)-1] < 0 { 10 | res = append(res, v) 11 | } else if v < 0 && res[len(res)-1] == -v { 12 | res = res[:len(res)-1] 13 | } 14 | } 15 | return res 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0744.Find-Smallest-Letter-Greater-Than-Target/744. Find Smallest Letter Greater Than Target.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func nextGreatestLetter(letters []byte, target byte) byte { 4 | low, high := 0, len(letters)-1 5 | for low <= high { 6 | mid := low + (high-low)>>1 7 | if letters[mid] > target { 8 | high = mid - 1 9 | } else { 10 | low = mid + 1 11 | } 12 | } 13 | find := letters[low%len(letters)] 14 | if find <= target { 15 | return letters[0] 16 | } 17 | return find 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0745.Prefix-and-Suffix-Search/745. Prefix and Suffix Search_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem745(t *testing.T) { 9 | obj := Constructor745([]string{"apple"}) 10 | fmt.Printf("obj = %v\n", obj) 11 | param1 := obj.F("a", "e") 12 | fmt.Printf("param_1 = %v obj = %v\n", param1, obj) 13 | param2 := obj.F("b", "") 14 | fmt.Printf("param_2 = %v obj = %v\n", param2, obj) 15 | } 16 | -------------------------------------------------------------------------------- /leetcode/0762.Prime-Number-of-Set-Bits-in-Binary-Representation/762. Prime Number of Set Bits in Binary Representation.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "math/bits" 4 | 5 | func countPrimeSetBits(L int, R int) int { 6 | counter := 0 7 | for i := L; i <= R; i++ { 8 | if isPrime(bits.OnesCount(uint(i))) { 9 | counter++ 10 | } 11 | } 12 | return counter 13 | } 14 | 15 | func isPrime(x int) bool { 16 | return x == 2 || x == 3 || x == 5 || x == 7 || x == 11 || x == 13 || x == 17 || x == 19 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0765.Couples-Holding-Hands/765. Couples Holding Hands.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/template" 5 | ) 6 | 7 | func minSwapsCouples(row []int) int { 8 | if len(row)&1 == 1 { 9 | return 0 10 | } 11 | uf := template.UnionFind{} 12 | uf.Init(len(row)) 13 | for i := 0; i < len(row)-1; i = i + 2 { 14 | uf.Union(i, i+1) 15 | } 16 | for i := 0; i < len(row)-1; i = i + 2 { 17 | if uf.Find(row[i]) != uf.Find(row[i+1]) { 18 | uf.Union(row[i], row[i+1]) 19 | } 20 | } 21 | return len(row)/2 - uf.TotalCount() 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0766.Toeplitz-Matrix/766. Toeplitz Matrix.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isToeplitzMatrix(matrix [][]int) bool { 4 | rows, columns := len(matrix), len(matrix[0]) 5 | for i := 1; i < rows; i++ { 6 | for j := 1; j < columns; j++ { 7 | if matrix[i-1][j-1] != matrix[i][j] { 8 | return false 9 | } 10 | } 11 | } 12 | return true 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/0771.Jewels-and-Stones/771. Jewels and Stones.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | // 解法一 6 | func numJewelsInStones(J string, S string) int { 7 | count := 0 8 | for i := range S { 9 | if strings.Contains(J, string(S[i])) { 10 | count++ 11 | } 12 | } 13 | return count 14 | } 15 | 16 | // 解法二 17 | func numJewelsInStones1(J string, S string) int { 18 | cache, result := make(map[rune]bool), 0 19 | for _, r := range J { 20 | cache[r] = true 21 | } 22 | for _, r := range S { 23 | if _, ok := cache[r]; ok { 24 | result++ 25 | } 26 | } 27 | return result 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/0781.Rabbits-in-Forest/781. Rabbits in Forest.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func numRabbits(ans []int) int { 4 | total, m := 0, make(map[int]int) 5 | for _, v := range ans { 6 | if m[v] == 0 { 7 | m[v] += v 8 | total += v + 1 9 | } else { 10 | m[v]-- 11 | } 12 | } 13 | return total 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/0832.Flipping-an-Image/832. Flipping an Image.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func flipAndInvertImage(A [][]int) [][]int { 4 | for i := 0; i < len(A); i++ { 5 | for a, b := 0, len(A[i])-1; a < b; a, b = a+1, b-1 { 6 | A[i][a], A[i][b] = A[i][b], A[i][a] 7 | } 8 | for a := 0; a < len(A[i]); a++ { 9 | A[i][a] = (A[i][a] + 1) % 2 10 | } 11 | } 12 | return A 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/0836.Rectangle-Overlap/836. Rectangle Overlap.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isRectangleOverlap(rec1 []int, rec2 []int) bool { 4 | return rec1[0] < rec2[2] && rec2[0] < rec1[2] && rec1[1] < rec2[3] && rec2[1] < rec1[3] 5 | } 6 | -------------------------------------------------------------------------------- /leetcode/0839.Similar-String-Groups/839. Similar String Groups.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/template" 5 | ) 6 | 7 | func numSimilarGroups(A []string) int { 8 | uf := template.UnionFind{} 9 | uf.Init(len(A)) 10 | for i := 0; i < len(A); i++ { 11 | for j := i + 1; j < len(A); j++ { 12 | if isSimilar(A[i], A[j]) { 13 | uf.Union(i, j) 14 | } 15 | } 16 | } 17 | return uf.TotalCount() 18 | } 19 | 20 | func isSimilar(a, b string) bool { 21 | var n int 22 | for i := 0; i < len(a); i++ { 23 | if a[i] != b[i] { 24 | n++ 25 | if n > 2 { 26 | return false 27 | } 28 | } 29 | } 30 | return true 31 | } 32 | -------------------------------------------------------------------------------- /leetcode/0841.Keys-and-Rooms/841. Keys and Rooms.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func canVisitAllRooms(rooms [][]int) bool { 4 | visited := make(map[int]bool) 5 | visited[0] = true 6 | dfsVisitAllRooms(rooms, visited, 0) 7 | return len(rooms) == len(visited) 8 | } 9 | 10 | func dfsVisitAllRooms(es [][]int, visited map[int]bool, from int) { 11 | for _, to := range es[from] { 12 | if visited[to] { 13 | continue 14 | } 15 | visited[to] = true 16 | dfsVisitAllRooms(es, visited, to) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0844.Backspace-String-Compare/844. Backspace String Compare.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func backspaceCompare(S string, T string) bool { 4 | s := make([]rune, 0) 5 | for _, c := range S { 6 | if c == '#' { 7 | if len(s) > 0 { 8 | s = s[:len(s)-1] 9 | } 10 | } else { 11 | s = append(s, c) 12 | } 13 | } 14 | s2 := make([]rune, 0) 15 | for _, c := range T { 16 | if c == '#' { 17 | if len(s2) > 0 { 18 | s2 = s2[:len(s2)-1] 19 | } 20 | } else { 21 | s2 = append(s2, c) 22 | } 23 | } 24 | return string(s) == string(s2) 25 | } 26 | -------------------------------------------------------------------------------- /leetcode/0867.Transpose-Matrix/867. Transpose Matrix.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func transpose(A [][]int) [][]int { 4 | row, col, result := len(A), len(A[0]), make([][]int, len(A[0])) 5 | for i := range result { 6 | result[i] = make([]int, row) 7 | } 8 | for i := 0; i < row; i++ { 9 | for j := 0; j < col; j++ { 10 | result[j][i] = A[i][j] 11 | } 12 | } 13 | return result 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/0878.Nth-Magical-Number/878. Nth Magical Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func nthMagicalNumber(N int, A int, B int) int { 4 | low, high := int64(0), int64(1*1e14) 5 | for low < high { 6 | mid := low + (high-low)>>1 7 | if calNthMagicalCount(mid, int64(A), int64(B)) < int64(N) { 8 | low = mid + 1 9 | } else { 10 | high = mid 11 | } 12 | } 13 | return int(low) % 1000000007 14 | } 15 | 16 | func calNthMagicalCount(num, a, b int64) int64 { 17 | ab := a * b / gcd(a, b) 18 | return num/a + num/b - num/ab 19 | } 20 | 21 | func gcd(a, b int64) int64 { 22 | for b != 0 { 23 | a, b = b, a%b 24 | } 25 | return a 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/0880.Decoded-String-at-Index/880. Decoded String at Index.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isLetter(char byte) bool { 4 | if char >= 'a' && char <= 'z' { 5 | return true 6 | } 7 | return false 8 | } 9 | 10 | func decodeAtIndex(S string, K int) string { 11 | length := 0 12 | for i := 0; i < len(S); i++ { 13 | if isLetter(S[i]) { 14 | length++ 15 | if length == K { 16 | return string(S[i]) 17 | } 18 | } else { 19 | if length*int(S[i]-'0') >= K { 20 | if K%length != 0 { 21 | return decodeAtIndex(S[:i], K%length) 22 | } 23 | return decodeAtIndex(S[:i], length) 24 | } 25 | length *= int(S[i] - '0') 26 | } 27 | } 28 | return "" 29 | } 30 | -------------------------------------------------------------------------------- /leetcode/0881.Boats-to-Save-People/881. Boats to Save People.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func numRescueBoats(people []int, limit int) int { 8 | sort.Ints(people) 9 | left, right, res := 0, len(people)-1, 0 10 | for left <= right { 11 | if left == right { 12 | res++ 13 | return res 14 | } 15 | if people[left]+people[right] <= limit { 16 | left++ 17 | right-- 18 | } else { 19 | right-- 20 | } 21 | res++ 22 | } 23 | return res 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/0884.Uncommon-Words-from-Two-Sentences/884. Uncommon Words from Two Sentences.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func uncommonFromSentences(A string, B string) []string { 6 | m, res := map[string]int{}, []string{} 7 | for _, s := range []string{A, B} { 8 | for _, word := range strings.Split(s, " ") { 9 | m[word]++ 10 | } 11 | } 12 | for key := range m { 13 | if m[key] == 1 { 14 | res = append(res, key) 15 | } 16 | } 17 | return res 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0885.Spiral-Matrix-III/885. Spiral Matrix III.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func spiralMatrixIII(R int, C int, r0 int, c0 int) [][]int { 4 | res, round, spDir := [][]int{}, 0, [][]int{ 5 | {0, 1}, // 朝右 6 | {1, 0}, // 朝下 7 | {0, -1}, // 朝左 8 | {-1, 0}, // 朝上 9 | } 10 | res = append(res, []int{r0, c0}) 11 | for i := 0; len(res) < R*C; i++ { 12 | for j := 0; j < i/2+1; j++ { 13 | r0 += spDir[round%4][0] 14 | c0 += spDir[round%4][1] 15 | if 0 <= r0 && r0 < R && 0 <= c0 && c0 < C { 16 | res = append(res, []int{r0, c0}) 17 | } 18 | } 19 | round++ 20 | } 21 | return res 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/0891.Sum-of-Subsequence-Widths/891. Sum of Subsequence Widths.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func sumSubseqWidths(A []int) int { 8 | sort.Ints(A) 9 | res, mod, n, p := 0, 1000000007, len(A), 1 10 | for i := 0; i < n; i++ { 11 | res = (res + (A[i]-A[n-1-i])*p) % mod 12 | p = (p << 1) % mod 13 | } 14 | return res 15 | } 16 | -------------------------------------------------------------------------------- /leetcode/0896.Monotonic-Array/896. Monotonic Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isMonotonic(A []int) bool { 4 | if len(A) <= 1 { 5 | return true 6 | } 7 | if A[0] < A[1] { 8 | return inc(A[1:]) 9 | } 10 | if A[0] > A[1] { 11 | return dec(A[1:]) 12 | } 13 | return inc(A[1:]) || dec(A[1:]) 14 | } 15 | 16 | func inc(A []int) bool { 17 | for i := 0; i < len(A)-1; i++ { 18 | if A[i] > A[i+1] { 19 | return false 20 | } 21 | } 22 | return true 23 | } 24 | 25 | func dec(A []int) bool { 26 | for i := 0; i < len(A)-1; i++ { 27 | if A[i] < A[i+1] { 28 | return false 29 | } 30 | } 31 | return true 32 | } 33 | -------------------------------------------------------------------------------- /leetcode/0914.X-of-a-Kind-in-a-Deck-of-Cards/914. X of a Kind in a Deck of Cards.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func hasGroupsSizeX(deck []int) bool { 4 | if len(deck) < 2 { 5 | return false 6 | } 7 | m, g := map[int]int{}, -1 8 | for _, d := range deck { 9 | m[d]++ 10 | } 11 | for _, v := range m { 12 | if g == -1 { 13 | g = v 14 | } else { 15 | g = gcd(g, v) 16 | } 17 | } 18 | return g >= 2 19 | } 20 | 21 | func gcd(a, b int) int { 22 | if a == 0 { 23 | return b 24 | } 25 | return gcd(b%a, a) 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/0920.Number-of-Music-Playlists/920. Number of Music Playlists.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func numMusicPlaylists(N int, L int, K int) int { 4 | dp, mod := make([][]int, L+1), 1000000007 5 | for i := 0; i < L+1; i++ { 6 | dp[i] = make([]int, N+1) 7 | } 8 | dp[0][0] = 1 9 | for i := 1; i <= L; i++ { 10 | for j := 1; j <= N; j++ { 11 | dp[i][j] = (dp[i-1][j-1] * (N - (j - 1))) % mod 12 | if j > K { 13 | dp[i][j] = (dp[i][j] + (dp[i-1][j]*(j-K))%mod) % mod 14 | } 15 | } 16 | } 17 | return dp[L][N] 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0921.Minimum-Add-to-Make-Parentheses-Valid/921. Minimum Add to Make Parentheses Valid.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func minAddToMakeValid(S string) int { 4 | if len(S) == 0 { 5 | return 0 6 | } 7 | stack := make([]rune, 0) 8 | for _, v := range S { 9 | if v == '(' { 10 | stack = append(stack, v) 11 | } else if (v == ')') && len(stack) > 0 && stack[len(stack)-1] == '(' { 12 | stack = stack[:len(stack)-1] 13 | } else { 14 | stack = append(stack, v) 15 | } 16 | } 17 | return len(stack) 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/0922.Sort-Array-By-Parity-II/922. Sort Array By Parity II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func sortArrayByParityII(A []int) []int { 4 | if len(A) == 0 || len(A)%2 != 0 { 5 | return []int{} 6 | } 7 | res := make([]int, len(A)) 8 | oddIndex := 1 9 | evenIndex := 0 10 | for i := 0; i < len(A); i++ { 11 | if A[i]%2 == 0 { 12 | res[evenIndex] = A[i] 13 | evenIndex += 2 14 | } else { 15 | res[oddIndex] = A[i] 16 | oddIndex += 2 17 | } 18 | } 19 | return res 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/0925.Long-Pressed-Name/925. Long Pressed Name.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isLongPressedName(name string, typed string) bool { 4 | if len(name) == 0 && len(typed) == 0 { 5 | return true 6 | } 7 | if (len(name) == 0 && len(typed) != 0) || (len(name) != 0 && len(typed) == 0) { 8 | return false 9 | } 10 | 11 | j := 0 12 | for i := 0; i < len(name); i++ { 13 | if j < len(typed) && name[i] == typed[j] { 14 | j++ 15 | continue 16 | } else { 17 | if i > 0 && j < len(typed) && name[i-1] == typed[j] { 18 | j++ 19 | i-- 20 | } else { 21 | return false 22 | } 23 | } 24 | } 25 | return true 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/0930.Binary-Subarrays-With-Sum/930. Binary Subarrays With Sum.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "fmt" 4 | 5 | func numSubarraysWithSum(A []int, S int) int { 6 | freq, sum, res := make([]int, len(A)+1), 0, 0 7 | freq[0] = 1 8 | for _, v := range A { 9 | t := sum + v - S 10 | if t >= 0 { 11 | // 总和有多余的,需要减去 t,除去的方法有 freq[t] 种 12 | res += freq[t] 13 | } 14 | sum += v 15 | freq[sum]++ 16 | fmt.Printf("freq = %v sum = %v res = %v t = %v\n", freq, sum, res, t) 17 | } 18 | return res 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0933.Number-of-Recent-Calls/933. Number of Recent Calls_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem933(t *testing.T) { 9 | obj := Constructor933() 10 | fmt.Printf("obj = %v\n", obj) 11 | param1 := obj.Ping(1) 12 | fmt.Printf("param = %v\n", param1) 13 | param1 = obj.Ping(100) 14 | fmt.Printf("param = %v\n", param1) 15 | param1 = obj.Ping(3001) 16 | fmt.Printf("param = %v\n", param1) 17 | param1 = obj.Ping(3002) 18 | fmt.Printf("param = %v\n", param1) 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/0942.DI-String-Match/942. DI String Match.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func diStringMatch(S string) []int { 4 | result, maxNum, minNum, index := make([]int, len(S)+1), len(S), 0, 0 5 | for _, ch := range S { 6 | if ch == 'I' { 7 | result[index] = minNum 8 | minNum++ 9 | } else { 10 | result[index] = maxNum 11 | maxNum-- 12 | } 13 | index++ 14 | } 15 | result[index] = minNum 16 | return result 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0946.Validate-Stack-Sequences/946. Validate Stack Sequences.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "fmt" 4 | 5 | func validateStackSequences(pushed []int, popped []int) bool { 6 | stack, j, N := []int{}, 0, len(pushed) 7 | for _, x := range pushed { 8 | stack = append(stack, x) 9 | fmt.Printf("stack = %v j = %v\n", stack, j) 10 | for len(stack) != 0 && j < N && stack[len(stack)-1] == popped[j] { 11 | stack = stack[0 : len(stack)-1] 12 | j++ 13 | } 14 | fmt.Printf("*****stack = %v j = %v\n", stack, j) 15 | } 16 | return j == N 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/0961.N-Repeated-Element-in-Size-2N-Array/961. N-Repeated Element in Size 2N Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func repeatedNTimes(A []int) int { 4 | kv := make(map[int]struct{}) 5 | for _, val := range A { 6 | if _, ok := kv[val]; ok { 7 | return val 8 | } 9 | kv[val] = struct{}{} 10 | } 11 | return 0 12 | } 13 | -------------------------------------------------------------------------------- /leetcode/0973.K-Closest-Points-to-Origin/973. K Closest Points to Origin.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "sort" 4 | 5 | // KClosest define 6 | func KClosest(points [][]int, K int) [][]int { 7 | sort.Slice(points, func(i, j int) bool { 8 | return points[i][0]*points[i][0]+points[i][1]*points[i][1] < 9 | points[j][0]*points[j][0]+points[j][1]*points[j][1] 10 | }) 11 | ans := make([][]int, K) 12 | for i := 0; i < K; i++ { 13 | ans[i] = points[i] 14 | } 15 | return ans 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0977.Squares-of-a-Sorted-Array/977. Squares of a Sorted Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "sort" 4 | 5 | // 解法一 6 | func sortedSquares(A []int) []int { 7 | ans := make([]int, len(A)) 8 | for i, k, j := 0, len(A)-1, len(ans)-1; i <= j; k-- { 9 | if A[i]*A[i] > A[j]*A[j] { 10 | ans[k] = A[i] * A[i] 11 | i++ 12 | } else { 13 | ans[k] = A[j] * A[j] 14 | j-- 15 | } 16 | } 17 | return ans 18 | } 19 | 20 | // 解法二 21 | func sortedSquares1(A []int) []int { 22 | for i, value := range A { 23 | A[i] = value * value 24 | } 25 | sort.Ints(A) 26 | return A 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/0981.Time-Based-Key-Value-Store/981. Time Based Key-Value Store_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem981(t *testing.T) { 9 | obj := Constructor981() 10 | obj.Set("foo", "bar", 1) 11 | fmt.Printf("Get = %v\n", obj.Get("foo", 1)) 12 | fmt.Printf("Get = %v\n", obj.Get("foo", 3)) 13 | obj.Set("foo", "bar2", 4) 14 | fmt.Printf("Get = %v\n", obj.Get("foo", 4)) 15 | fmt.Printf("Get = %v\n", obj.Get("foo", 5)) 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/0985.Sum-of-Even-Numbers-After-Queries/985. Sum of Even Numbers After Queries.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func sumEvenAfterQueries(A []int, queries [][]int) []int { 4 | cur, res := 0, []int{} 5 | for _, v := range A { 6 | if v%2 == 0 { 7 | cur += v 8 | } 9 | } 10 | for _, q := range queries { 11 | if A[q[1]]%2 == 0 { 12 | cur -= A[q[1]] 13 | } 14 | A[q[1]] += q[0] 15 | if A[q[1]]%2 == 0 { 16 | cur += A[q[1]] 17 | } 18 | res = append(res, cur) 19 | } 20 | return res 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/0995.Minimum-Number-of-K-Consecutive-Bit-Flips/995. Minimum Number of K Consecutive Bit Flips.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func minKBitFlips(A []int, K int) int { 4 | flippedTime, count := 0, 0 5 | for i := 0; i < len(A); i++ { 6 | if i >= K && A[i-K] == 2 { 7 | flippedTime-- 8 | } 9 | // 下面这个判断包含了两种情况: 10 | // 如果 flippedTime 是奇数,且 A[i] == 1 就需要翻转 11 | // 如果 flippedTime 是偶数,且 A[i] == 0 就需要翻转 12 | if flippedTime%2 == A[i] { 13 | if i+K > len(A) { 14 | return -1 15 | } 16 | A[i] = 2 17 | flippedTime++ 18 | count++ 19 | } 20 | } 21 | return count 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/1004.Max-Consecutive-Ones-III/1004. Max Consecutive Ones III.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func longestOnes(A []int, K int) int { 4 | res, left, right := 0, 0, 0 5 | for left < len(A) { 6 | if right < len(A) && ((A[right] == 0 && K > 0) || A[right] == 1) { 7 | if A[right] == 0 { 8 | K-- 9 | } 10 | right++ 11 | } else { 12 | if K == 0 || (right == len(A) && K > 0) { 13 | res = max(res, right-left) 14 | } 15 | if A[left] == 0 { 16 | K++ 17 | } 18 | left++ 19 | } 20 | } 21 | return res 22 | } 23 | 24 | func max(a int, b int) int { 25 | if a > b { 26 | return a 27 | } 28 | return b 29 | } 30 | -------------------------------------------------------------------------------- /leetcode/1005.Maximize-Sum-Of-Array-After-K-Negations/1005. Maximize Sum Of Array After K Negations.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | func largestSumAfterKNegations(A []int, K int) int { 8 | sort.Ints(A) 9 | minIdx := 0 10 | for i := 0; i < K; i++ { 11 | A[minIdx] = -A[minIdx] 12 | if A[minIdx+1] < A[minIdx] { 13 | minIdx++ 14 | } 15 | } 16 | sum := 0 17 | for _, a := range A { 18 | sum += a 19 | } 20 | return sum 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/1017.Convert-to-Base--2/1017. Convert to Base -2.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strconv" 4 | 5 | func baseNeg2(N int) string { 6 | if N == 0 { 7 | return "0" 8 | } 9 | res := "" 10 | for N != 0 { 11 | remainder := N % (-2) 12 | N = N / (-2) 13 | if remainder < 0 { 14 | remainder += 2 15 | N++ 16 | } 17 | res = strconv.Itoa(remainder) + res 18 | } 19 | return res 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/1025.Divisor-Game/1025. Divisor Game.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func divisorGame(N int) bool { 4 | return N%2 == 0 5 | } 6 | -------------------------------------------------------------------------------- /leetcode/1037.Valid-Boomerang/1037. Valid Boomerang.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func isBoomerang(points [][]int) bool { 4 | return (points[0][0]-points[1][0])*(points[0][1]-points[2][1]) != (points[0][0]-points[2][0])*(points[0][1]-points[1][1]) 5 | } 6 | -------------------------------------------------------------------------------- /leetcode/1047.Remove-All-Adjacent-Duplicates-In-String/1047. Remove All Adjacent Duplicates In String.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func removeDuplicates1047(S string) string { 4 | stack := []rune{} 5 | for _, s := range S { 6 | if len(stack) == 0 || len(stack) > 0 && stack[len(stack)-1] != s { 7 | stack = append(stack, s) 8 | } else { 9 | stack = stack[:len(stack)-1] 10 | } 11 | } 12 | return string(stack) 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/1049.Last-Stone-Weight-II/1049. Last Stone Weight II.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func lastStoneWeightII(stones []int) int { 4 | sum := 0 5 | for _, v := range stones { 6 | sum += v 7 | } 8 | n, C, dp := len(stones), sum/2, make([]int, sum/2+1) 9 | for i := 0; i <= C; i++ { 10 | if stones[0] <= i { 11 | dp[i] = stones[0] 12 | } else { 13 | dp[i] = 0 14 | } 15 | } 16 | for i := 1; i < n; i++ { 17 | for j := C; j >= stones[i]; j-- { 18 | dp[j] = max(dp[j], dp[j-stones[i]]+stones[i]) 19 | } 20 | } 21 | return sum - 2*dp[C] 22 | } 23 | 24 | func max(a int, b int) int { 25 | if a > b { 26 | return a 27 | } 28 | return b 29 | } 30 | -------------------------------------------------------------------------------- /leetcode/1051.Height-Checker/1051. Height Checker.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "sort" 4 | 5 | func heightChecker(heights []int) int { 6 | result, checker := 0, []int{} 7 | checker = append(checker, heights...) 8 | sort.Ints(checker) 9 | for i := 0; i < len(heights); i++ { 10 | if heights[i] != checker[i] { 11 | result++ 12 | } 13 | } 14 | return result 15 | } 16 | -------------------------------------------------------------------------------- /leetcode/1078.Occurrences-After-Bigram/1078. Occurrences After Bigram.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func findOcurrences(text string, first string, second string) []string { 6 | var res []string 7 | words := strings.Split(text, " ") 8 | if len(words) < 3 { 9 | return []string{} 10 | } 11 | for i := 2; i < len(words); i++ { 12 | if words[i-2] == first && words[i-1] == second { 13 | res = append(res, words[i]) 14 | } 15 | } 16 | return res 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/1089.Duplicate-Zeros/1089. Duplicate Zeros.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func duplicateZeros(arr []int) { 4 | for i := 0; i < len(arr); i++ { 5 | if arr[i] == 0 && i+1 < len(arr) { 6 | arr = append(arr[:i+1], arr[i:len(arr)-1]...) 7 | i++ 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/1108.Defanging-an-IP-Address/1108. Defanging an IP Address.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func defangIPaddr(address string) string { 6 | return strings.Replace(address, ".", "[.]", -1) 7 | } 8 | -------------------------------------------------------------------------------- /leetcode/1128.Number-of-Equivalent-Domino-Pairs/1128. Number of Equivalent Domino Pairs.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func numEquivDominoPairs(dominoes [][]int) int { 4 | if dominoes == nil || len(dominoes) == 0 { 5 | return 0 6 | } 7 | result, buckets := 0, [100]int{} 8 | for _, dominoe := range dominoes { 9 | key, rotatedKey := dominoe[0]*10+dominoe[1], dominoe[1]*10+dominoe[0] 10 | if dominoe[0] != dominoe[1] { 11 | if buckets[rotatedKey] > 0 { 12 | result += buckets[rotatedKey] 13 | } 14 | } 15 | if buckets[key] > 0 { 16 | result += buckets[key] 17 | buckets[key]++ 18 | } else { 19 | buckets[key]++ 20 | } 21 | } 22 | return result 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/1137.N-th-Tribonacci-Number/1137. N-th Tribonacci Number.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func tribonacci(n int) int { 4 | if n < 2 { 5 | return n 6 | } 7 | trib, prev, prev2 := 1, 1, 0 8 | for n > 2 { 9 | trib, prev, prev2 = trib+prev+prev2, trib, prev 10 | n-- 11 | } 12 | return trib 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/1154.Day-of-the-Year/1154. Day of the Year.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "time" 4 | 5 | func dayOfYear(date string) int { 6 | first := date[:4] + "-01-01" 7 | firstDay, _ := time.Parse("2006-01-02", first) 8 | dateDay, _ := time.Parse("2006-01-02", date) 9 | duration := dateDay.Sub(firstDay) 10 | return int(duration.Hours())/24 + 1 11 | } 12 | -------------------------------------------------------------------------------- /leetcode/1157.Online-Majority-Element-In-Subarray/1157. Online Majority Element In Subarray_test.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Problem1157(t *testing.T) { 9 | arr := []int{1, 1, 2, 2, 1, 1} 10 | obj := Constructor1157(arr) 11 | fmt.Printf("obj = %v\n", obj) 12 | fmt.Printf("query(0,5,4) = %v\n", obj.Query(0, 5, 4)) //1 13 | fmt.Printf("query(0,3,3) = %v\n", obj.Query(0, 3, 3)) //-1 14 | fmt.Printf("query(2,3,2) = %v\n", obj.Query(2, 3, 2)) //2 15 | } 16 | -------------------------------------------------------------------------------- /leetcode/1160.Find-Words-That-Can-Be-Formed-by-Characters/1160. Find Words That Can Be Formed by Characters.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func countCharacters(words []string, chars string) int { 4 | count, res := make([]int, 26), 0 5 | for i := 0; i < len(chars); i++ { 6 | count[chars[i]-'a']++ 7 | } 8 | for _, w := range words { 9 | if canBeFormed(w, count) { 10 | res += len(w) 11 | } 12 | } 13 | return res 14 | } 15 | func canBeFormed(w string, c []int) bool { 16 | count := make([]int, 26) 17 | for i := 0; i < len(w); i++ { 18 | count[w[i]-'a']++ 19 | if count[w[i]-'a'] > c[w[i]-'a'] { 20 | return false 21 | } 22 | } 23 | return true 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/1175.Prime-Arrangements/1175. Prime Arrangements.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "sort" 4 | 5 | var primes = []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97} 6 | 7 | func numPrimeArrangements(n int) int { 8 | primeCount := sort.Search(25, func(i int) bool { return primes[i] > n }) 9 | return factorial(primeCount) * factorial(n-primeCount) % 1000000007 10 | } 11 | 12 | func factorial(n int) int { 13 | if n == 1 || n == 0 { 14 | return 1 15 | } 16 | return n * factorial(n-1) % 1000000007 17 | } 18 | -------------------------------------------------------------------------------- /leetcode/1184.Distance-Between-Bus-Stops/1184. Distance Between Bus Stops.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func distanceBetweenBusStops(distance []int, start int, destination int) int { 4 | clockwiseDis, counterclockwiseDis, n := 0, 0, len(distance) 5 | for i := start; i != destination; i = (i + 1) % n { 6 | clockwiseDis += distance[i] 7 | } 8 | for i := destination; i != start; i = (i + 1) % n { 9 | counterclockwiseDis += distance[i] 10 | } 11 | if clockwiseDis < counterclockwiseDis { 12 | return clockwiseDis 13 | } 14 | return counterclockwiseDis 15 | } 16 | -------------------------------------------------------------------------------- /leetcode/1185.Day-of-the-Week/1185. Day of the Week.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "time" 4 | 5 | func dayOfTheWeek(day int, month int, year int) string { 6 | return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local).Weekday().String() 7 | } 8 | -------------------------------------------------------------------------------- /leetcode/1189.Maximum-Number-of-Balloons/1189. Maximum Number of Balloons.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maxNumberOfBalloons(text string) int { 4 | fre := make([]int, 26) 5 | for _, t := range text { 6 | fre[t-'a']++ 7 | } 8 | // 字符 b 的频次是数组下标 1 对应的元素值 9 | // 字符 a 的频次是数组下标 0 对应的元素值 10 | // 字符 l 的频次是数组下标 11 对应的元素值,这里有 2 个 l,所以元素值需要除以 2 11 | // 字符 o 的频次是数组下标 14 对应的元素值,这里有 2 个 o,所以元素值需要除以 2 12 | // 字符 n 的频次是数组下标 13 对应的元素值 13 | return min(fre[1], min(fre[0], min(fre[11]/2, min(fre[14]/2, fre[13])))) 14 | } 15 | 16 | func min(a int, b int) int { 17 | if a > b { 18 | return b 19 | } 20 | return a 21 | } 22 | -------------------------------------------------------------------------------- /leetcode/1200.Minimum-Absolute-Difference/1200. Minimum Absolute Difference.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "math" 5 | "sort" 6 | ) 7 | 8 | func minimumAbsDifference(arr []int) [][]int { 9 | minDiff, res := math.MaxInt32, [][]int{} 10 | sort.Ints(arr) 11 | for i := 1; i < len(arr); i++ { 12 | if arr[i]-arr[i-1] < minDiff { 13 | minDiff = arr[i] - arr[i-1] 14 | } 15 | if minDiff == 1 { 16 | break 17 | } 18 | } 19 | for i := 1; i < len(arr); i++ { 20 | if arr[i]-arr[i-1] == minDiff { 21 | res = append(res, []int{arr[i-1], arr[i]}) 22 | } 23 | } 24 | return res 25 | } 26 | -------------------------------------------------------------------------------- /leetcode/1207.Unique-Number-of-Occurrences/1207. Unique Number of Occurrences.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func uniqueOccurrences(arr []int) bool { 4 | freq, m := map[int]int{}, map[int]bool{} 5 | for _, v := range arr { 6 | freq[v]++ 7 | } 8 | for _, v := range freq { 9 | if _, ok := m[v]; !ok { 10 | m[v] = true 11 | } else { 12 | return false 13 | } 14 | } 15 | return true 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/1217.Play-with-Chips/1217. Play with Chips.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func minCostToMoveChips(chips []int) int { 4 | odd, even := 0, 0 5 | for _, c := range chips { 6 | if c%2 == 0 { 7 | even++ 8 | } else { 9 | odd++ 10 | } 11 | } 12 | return min(odd, even) 13 | } 14 | 15 | func min(a int, b int) int { 16 | if a > b { 17 | return b 18 | } 19 | return a 20 | } 21 | -------------------------------------------------------------------------------- /leetcode/1221.Split-a-String-in-Balanced-Strings/1221. Split a String in Balanced Strings.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func balancedStringSplit(s string) int { 4 | count, res := 0, 0 5 | for _, r := range s { 6 | if r == 'R' { 7 | count++ 8 | } else { 9 | count-- 10 | } 11 | if count == 0 { 12 | res++ 13 | } 14 | } 15 | return res 16 | } 17 | -------------------------------------------------------------------------------- /leetcode/1232.Check-If-It-Is-a-Straight-Line/1232. Check If It Is a Straight Line.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func checkStraightLine(coordinates [][]int) bool { 4 | dx0 := coordinates[1][0] - coordinates[0][0] 5 | dy0 := coordinates[1][1] - coordinates[0][1] 6 | for i := 1; i < len(coordinates)-1; i++ { 7 | dx := coordinates[i+1][0] - coordinates[i][0] 8 | dy := coordinates[i+1][1] - coordinates[i][1] 9 | if dy*dx0 != dy0*dx { // check cross product 10 | return false 11 | } 12 | } 13 | return true 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/1260.Shift-2D-Grid/1260. Shift 2D Grid.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func shiftGrid(grid [][]int, k int) [][]int { 4 | x, y := len(grid[0]), len(grid) 5 | newGrid := make([][]int, y) 6 | for i := 0; i < y; i++ { 7 | newGrid[i] = make([]int, x) 8 | } 9 | for i := 0; i < y; i++ { 10 | for j := 0; j < x; j++ { 11 | ny := (k / x) + i 12 | if (j + (k % x)) >= x { 13 | ny++ 14 | } 15 | newGrid[ny%y][(j+(k%x))%x] = grid[i][j] 16 | } 17 | } 18 | return newGrid 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/1266.Minimum-Time-Visiting-All-Points/1266. Minimum Time Visiting All Points.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func minTimeToVisitAllPoints(points [][]int) int { 4 | res := 0 5 | for i := 1; i < len(points); i++ { 6 | res += max(abs(points[i][0]-points[i-1][0]), abs(points[i][1]-points[i-1][1])) 7 | } 8 | return res 9 | } 10 | 11 | func max(a int, b int) int { 12 | if a > b { 13 | return a 14 | } 15 | return b 16 | } 17 | 18 | func abs(a int) int { 19 | if a > 0 { 20 | return a 21 | } 22 | return -a 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/1281.Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281. Subtract the Product and Sum of Digits of an Integer.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func subtractProductAndSum(n int) int { 4 | sum, product := 0, 1 5 | for ; n > 0; n /= 10 { 6 | sum += n % 10 7 | product *= n % 10 8 | } 9 | return product - sum 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/1287.Element-Appearing-More-Than-In-Sorted-Array/1287. Element Appearing More Than 25% In Sorted Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findSpecialInteger(arr []int) int { 4 | n := len(arr) 5 | for i := 0; i < n-n/4; i++ { 6 | if arr[i] == arr[i+n/4] { 7 | return arr[i] 8 | } 9 | } 10 | return -1 11 | } 12 | -------------------------------------------------------------------------------- /leetcode/1290.Convert-Binary-Number-in-a-Linked-List-to-Integer/1290. Convert Binary Number in a Linked List to Integer.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import ( 4 | "github.com/halfrost/LeetCode-Go/structures" 5 | ) 6 | 7 | // ListNode define 8 | type ListNode = structures.ListNode 9 | 10 | /** 11 | * Definition for singly-linked list. 12 | * type ListNode struct { 13 | * Val int 14 | * Next *ListNode 15 | * } 16 | */ 17 | func getDecimalValue(head *ListNode) int { 18 | sum := 0 19 | for head != nil { 20 | sum = sum*2 + head.Val 21 | head = head.Next 22 | } 23 | return sum 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/1295.Find-Numbers-with-Even-Number-of-Digits/1295. Find Numbers with Even Number of Digits.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strconv" 4 | 5 | func findNumbers(nums []int) int { 6 | res := 0 7 | for _, n := range nums { 8 | res += 1 - len(strconv.Itoa(n))%2 9 | } 10 | return res 11 | } 12 | -------------------------------------------------------------------------------- /leetcode/1299.Replace-Elements-with-Greatest-Element-on-Right-Side/1299. Replace Elements with Greatest Element on Right Side.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func replaceElements(arr []int) []int { 4 | j, temp := -1, 0 5 | for i := len(arr) - 1; i >= 0; i-- { 6 | temp = arr[i] 7 | arr[i] = j 8 | j = max(j, temp) 9 | } 10 | return arr 11 | } 12 | 13 | func max(a int, b int) int { 14 | if a > b { 15 | return a 16 | } 17 | return b 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/1304.Find-N-Unique-Integers-Sum-up-to-Zero/1304. Find N Unique Integers Sum up to Zero.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func sumZero(n int) []int { 4 | res, left, right, start := make([]int, n), 0, n-1, 1 5 | for left < right { 6 | res[left] = start 7 | res[right] = -start 8 | start++ 9 | left = left + 1 10 | right = right - 1 11 | } 12 | return res 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/1306.Jump-Game-III/1306. Jump Game III.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func canReach(arr []int, start int) bool { 4 | if start >= 0 && start < len(arr) && arr[start] < len(arr) { 5 | jump := arr[start] 6 | arr[start] += len(arr) 7 | return jump == 0 || canReach(arr, start+jump) || canReach(arr, start-jump) 8 | } 9 | return false 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/1313.Decompress-Run-Length-Encoded-List/1313. Decompress Run-Length Encoded List.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func decompressRLElist(nums []int) []int { 4 | res := []int{} 5 | for i := 0; i < len(nums); i += 2 { 6 | for j := 0; j < nums[i]; j++ { 7 | res = append(res, nums[i+1]) 8 | } 9 | } 10 | return res 11 | } 12 | -------------------------------------------------------------------------------- /leetcode/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/1317. Convert Integer to the Sum of Two No-Zero Integers.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func getNoZeroIntegers(n int) []int { 4 | noZeroPair := []int{} 5 | for i := 1; i <= n/2; i++ { 6 | if isNoZero(i) && isNoZero(n-i) { 7 | noZeroPair = append(noZeroPair, []int{i, n - i}...) 8 | break 9 | } 10 | } 11 | return noZeroPair 12 | } 13 | 14 | func isNoZero(n int) bool { 15 | for n != 0 { 16 | if n%10 == 0 { 17 | return false 18 | } 19 | n /= 10 20 | } 21 | return true 22 | } 23 | -------------------------------------------------------------------------------- /leetcode/1380.Lucky-Numbers-in-a-Matrix/1380. Lucky Numbers in a Matrix.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func luckyNumbers(matrix [][]int) []int { 4 | t, r, res := make([]int, len(matrix[0])), make([]int, len(matrix[0])), []int{} 5 | for _, val := range matrix { 6 | m, k := val[0], 0 7 | for j := 0; j < len(matrix[0]); j++ { 8 | if val[j] < m { 9 | m = val[j] 10 | k = j 11 | } 12 | if t[j] < val[j] { 13 | t[j] = val[j] 14 | } 15 | } 16 | 17 | if t[k] == m { 18 | r[k] = m 19 | } 20 | } 21 | for k, v := range r { 22 | if v > 0 && v == t[k] { 23 | res = append(res, v) 24 | } 25 | } 26 | return res 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/1385.Find-the-Distance-Value-Between-Two-Arrays/1385. Find the Distance Value Between Two Arrays.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func findTheDistanceValue(arr1 []int, arr2 []int, d int) int { 4 | res := 0 5 | for i := range arr1 { 6 | for j := range arr2 { 7 | if abs(arr1[i]-arr2[j]) <= d { 8 | break 9 | } 10 | if j == len(arr2)-1 { 11 | res++ 12 | } 13 | } 14 | } 15 | return res 16 | } 17 | 18 | func abs(a int) int { 19 | if a < 0 { 20 | return -1 * a 21 | } 22 | return a 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/1389.Create-Target-Array-in-the-Given-Order/1389. Create Target Array in the Given Order.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func createTargetArray(nums []int, index []int) []int { 4 | result := make([]int, len(nums)) 5 | for i, pos := range index { 6 | copy(result[pos+1:i+1], result[pos:i]) 7 | result[pos] = nums[i] 8 | } 9 | return result 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import "strings" 4 | 5 | func isPrefixOfWord(sentence string, searchWord string) int { 6 | for i, v := range strings.Split(sentence, " ") { 7 | if strings.HasPrefix(v, searchWord) { 8 | return i + 1 9 | } 10 | } 11 | return -1 12 | } 13 | -------------------------------------------------------------------------------- /leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array/1464. Maximum Product of Two Elements in an Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maxProduct(nums []int) int { 4 | max1, max2 := 0, 0 5 | for _, num := range nums { 6 | if num >= max1 { 7 | max2 = max1 8 | max1 = num 9 | } else if num <= max1 && num >= max2 { 10 | max2 = num 11 | } 12 | } 13 | return (max1 - 1) * (max2 - 1) 14 | } 15 | -------------------------------------------------------------------------------- /leetcode/1470.Shuffle-the-Array/1470. Shuffle the Array.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func shuffle(nums []int, n int) []int { 4 | result := make([]int, 0) 5 | for i := 0; i < n; i++ { 6 | result = append(result, nums[i]) 7 | result = append(result, nums[n+i]) 8 | } 9 | return result 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func maximalRectangle(matrix [][]byte) int { 4 | return 0 5 | } 6 | -------------------------------------------------------------------------------- /leetcode/9990316.Remove-Duplicate-Letters/316. Remove Duplicate Letters.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func removeDuplicateLetters(s string) string { 4 | return "" 5 | } 6 | -------------------------------------------------------------------------------- /leetcode/9990377.Combination-Sum-IV/377. Combination Sum IV.go: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | func combinationSum4(nums []int, target int) int { 4 | if len(nums) == 0 { 5 | return 0 6 | } 7 | c, res := []int{}, 0 8 | findcombinationSum4(nums, target, 0, c, &res) 9 | return res 10 | } 11 | 12 | func findcombinationSum4(nums []int, target, index int, c []int, res *int) { 13 | if target <= 0 { 14 | if target == 0 { 15 | *res++ 16 | } 17 | return 18 | } 19 | for i := 0; i < len(nums); i++ { 20 | c = append(c, nums[i]) 21 | findcombinationSum4(nums, target-nums[i], i, c, res) 22 | c = c[:len(c)-1] 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/33c21e4bea079aa161bd114d152c5ce05536fdd6/logo.png -------------------------------------------------------------------------------- /structures/Heap.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | // intHeap 实现了 heap 的接口 4 | type intHeap []int 5 | 6 | func (h intHeap) Len() int { 7 | return len(h) 8 | } 9 | 10 | func (h intHeap) Less(i, j int) bool { 11 | return h[i] < h[j] 12 | } 13 | 14 | func (h intHeap) Swap(i, j int) { 15 | h[i], h[j] = h[j], h[i] 16 | } 17 | 18 | func (h *intHeap) Push(x interface{}) { 19 | // Push 使用 *h,是因为 20 | // Push 增加了 h 的长度 21 | *h = append(*h, x.(int)) 22 | } 23 | 24 | func (h *intHeap) Pop() interface{} { 25 | // Pop 使用 *h ,是因为 26 | // Pop 减短了 h 的长度 27 | res := (*h)[len(*h)-1] 28 | *h = (*h)[:len(*h)-1] 29 | return res 30 | } 31 | -------------------------------------------------------------------------------- /structures/Heap_test.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | import ( 4 | "container/heap" 5 | "fmt" 6 | "testing" 7 | 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_intHeap(t *testing.T) { 12 | ast := assert.New(t) 13 | 14 | ih := new(intHeap) 15 | heap.Init(ih) 16 | 17 | heap.Push(ih, 1) 18 | heap.Pop(ih) 19 | 20 | begin, end := 0, 10 21 | for i := begin; i < end; i++ { 22 | heap.Push(ih, i) 23 | ast.Equal(0, (*ih)[0], "插入 %d 后的最小值却是 %d,ih=%v", i, (*ih)[0], (*ih)) 24 | } 25 | 26 | for i := begin; i < end; i++ { 27 | fmt.Println(i, *ih) 28 | ast.Equal(i, heap.Pop(ih), "Pop 后 ih=%v", (*ih)) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /structures/NestedInterger_test.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_NestedInteger(t *testing.T) { 10 | ast := assert.New(t) 11 | 12 | n := NestedInteger{} 13 | 14 | ast.True(n.IsInteger()) 15 | 16 | n.SetInteger(1) 17 | ast.Equal(1, n.GetInteger()) 18 | 19 | elem := NestedInteger{Num: 1} 20 | 21 | expected := NestedInteger{ 22 | Num: 1, 23 | Ns: []*NestedInteger{&elem}, 24 | } 25 | n.Add(elem) 26 | 27 | ast.Equal(expected, n) 28 | 29 | ast.Equal(expected.Ns, n.GetList()) 30 | } 31 | -------------------------------------------------------------------------------- /structures/Point.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | // Point 定义了一个二维坐标点 4 | type Point struct { 5 | X, Y int 6 | } 7 | 8 | // Intss2Points 把 [][]int 转换成 []Point 9 | func Intss2Points(points [][]int) []Point { 10 | res := make([]Point, len(points)) 11 | for i, p := range points { 12 | res[i] = Point{ 13 | X: p[0], 14 | Y: p[1], 15 | } 16 | } 17 | return res 18 | } 19 | 20 | // Points2Intss 把 []Point 转换成 [][]int 21 | func Points2Intss(points []Point) [][]int { 22 | res := make([][]int, len(points)) 23 | for i, p := range points { 24 | res[i] = []int{p.X, p.Y} 25 | } 26 | return res 27 | } 28 | -------------------------------------------------------------------------------- /structures/Queue.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | // Queue 是用于存放 int 的队列 4 | type Queue struct { 5 | nums []int 6 | } 7 | 8 | // NewQueue 返回 *kit.Queue 9 | func NewQueue() *Queue { 10 | return &Queue{nums: []int{}} 11 | } 12 | 13 | // Push 把 n 放入队列 14 | func (q *Queue) Push(n int) { 15 | q.nums = append(q.nums, n) 16 | } 17 | 18 | // Pop 从 q 中取出最先进入队列的值 19 | func (q *Queue) Pop() int { 20 | res := q.nums[0] 21 | q.nums = q.nums[1:] 22 | return res 23 | } 24 | 25 | // Len 返回 q 的长度 26 | func (q *Queue) Len() int { 27 | return len(q.nums) 28 | } 29 | 30 | // IsEmpty 反馈 q 是否为空 31 | func (q *Queue) IsEmpty() bool { 32 | return q.Len() == 0 33 | } 34 | -------------------------------------------------------------------------------- /structures/Queue_test.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_Queue(t *testing.T) { 10 | ast := assert.New(t) 11 | 12 | q := NewQueue() 13 | ast.True(q.IsEmpty(), "检查新建的 q 是否为空") 14 | 15 | start, end := 0, 100 16 | 17 | for i := start; i < end; i++ { 18 | q.Push(i) 19 | ast.Equal(i-start+1, q.Len(), "Push 后检查 q 的长度。") 20 | } 21 | 22 | for i := start; i < end; i++ { 23 | ast.Equal(i, q.Pop(), "从 q 中 pop 出数来。") 24 | } 25 | 26 | ast.True(q.IsEmpty(), "检查 Pop 完毕后的 q 是否为空") 27 | } 28 | -------------------------------------------------------------------------------- /structures/Stack.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | // Stack 是用于存放 int 的 栈 4 | type Stack struct { 5 | nums []int 6 | } 7 | 8 | // NewStack 返回 *kit.Stack 9 | func NewStack() *Stack { 10 | return &Stack{nums: []int{}} 11 | } 12 | 13 | // Push 把 n 放入 栈 14 | func (s *Stack) Push(n int) { 15 | s.nums = append(s.nums, n) 16 | } 17 | 18 | // Pop 从 s 中取出最后放入 栈 的值 19 | func (s *Stack) Pop() int { 20 | res := s.nums[len(s.nums)-1] 21 | s.nums = s.nums[:len(s.nums)-1] 22 | return res 23 | } 24 | 25 | // Len 返回 s 的长度 26 | func (s *Stack) Len() int { 27 | return len(s.nums) 28 | } 29 | 30 | // IsEmpty 反馈 s 是否为空 31 | func (s *Stack) IsEmpty() bool { 32 | return s.Len() == 0 33 | } 34 | -------------------------------------------------------------------------------- /structures/Stack_test.go: -------------------------------------------------------------------------------- 1 | package structures 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_Stack(t *testing.T) { 10 | ast := assert.New(t) 11 | 12 | s := NewStack() 13 | ast.True(s.IsEmpty(), "检查新建的 s 是否为空") 14 | 15 | start, end := 0, 100 16 | 17 | for i := start; i < end; i++ { 18 | s.Push(i) 19 | ast.Equal(i-start+1, s.Len(), "Push 后检查 q 的长度。") 20 | } 21 | 22 | for i := end - 1; i >= start; i-- { 23 | ast.Equal(i, s.Pop(), "从 s 中 pop 出数来。") 24 | } 25 | 26 | ast.True(s.IsEmpty(), "检查 Pop 完毕后的 s 是否为空") 27 | } 28 | -------------------------------------------------------------------------------- /topic/Backtracking.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/33c21e4bea079aa161bd114d152c5ce05536fdd6/topic/Backtracking.png -------------------------------------------------------------------------------- /topic/Binary_Indexed_Tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/33c21e4bea079aa161bd114d152c5ce05536fdd6/topic/Binary_Indexed_Tree.png -------------------------------------------------------------------------------- /topic/Bit_Manipulation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/33c21e4bea079aa161bd114d152c5ce05536fdd6/topic/Bit_Manipulation.png -------------------------------------------------------------------------------- /topic/Linked_List.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/33c21e4bea079aa161bd114d152c5ce05536fdd6/topic/Linked_List.png -------------------------------------------------------------------------------- /topic/Segment_Tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/33c21e4bea079aa161bd114d152c5ce05536fdd6/topic/Segment_Tree.png -------------------------------------------------------------------------------- /topic/Sliding_Window.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/33c21e4bea079aa161bd114d152c5ce05536fdd6/topic/Sliding_Window.png -------------------------------------------------------------------------------- /topic/Sort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/33c21e4bea079aa161bd114d152c5ce05536fdd6/topic/Sort.png -------------------------------------------------------------------------------- /topic/Stack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/33c21e4bea079aa161bd114d152c5ce05536fdd6/topic/Stack.png -------------------------------------------------------------------------------- /topic/Two_pointers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/33c21e4bea079aa161bd114d152c5ce05536fdd6/topic/Two_pointers.png -------------------------------------------------------------------------------- /topic/Union_Find.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ysluckly/leetcode/33c21e4bea079aa161bd114d152c5ce05536fdd6/topic/Union_Find.png -------------------------------------------------------------------------------- /website/archetypes/default.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "{{ replace .Name "-" " " | title }}" 3 | date: {{ .Date }} 4 | draft: true 5 | --- 6 | 7 | -------------------------------------------------------------------------------- /website/content/ChapterFour/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 第四章 3 | type: docs 4 | --- 5 | 6 | # 第四章 Leetcode 题解 7 | 8 |
9 |
10 |
9 |
10 |
9 | {{- .Summary -}} 10 | {{ if .Truncated }} 11 | ... 12 | {{ end }} 13 |
14 |8 | {{- .Content -}} 9 |
10 |2 | {{ .Inner | markdownify }} 3 |4 | -------------------------------------------------------------------------------- /website/themes/book/layouts/shortcodes/katex.html: -------------------------------------------------------------------------------- 1 | {{- if not (.Page.Scratch.Get "katex") -}} 2 | 3 | 4 | 5 | 6 | {{- .Page.Scratch.Set "katex" true -}} 7 | {{- end -}} 8 | 9 | 10 | {{ cond (in .Params "display") "\\[" "\\(" -}} 11 | {{- trim .Inner "\n" -}} 12 | {{- cond (in .Params "display") "\\]" "\\)" }} 13 | 14 | -------------------------------------------------------------------------------- /website/themes/book/layouts/shortcodes/mermaid.html: -------------------------------------------------------------------------------- 1 | {{ if not (.Page.Scratch.Get "mermaid") }} 2 | 3 | 4 | 9 | {{ .Page.Scratch.Set "mermaid" true }} 10 | {{ end }} 11 | 12 |
13 | {{- .Inner -}} 14 |
15 | -------------------------------------------------------------------------------- /website/themes/book/layouts/shortcodes/section.html: -------------------------------------------------------------------------------- 1 |9 | {{- .Summary -}} 10 | {{ if .Truncated }} 11 | ... 12 | {{ end }} 13 |
14 |