├── 001. Two Sum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 002. Add Two Numbers ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 003. Longest Substring Without Repeating Characters ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 004. Median of Two Sorted Arrays ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 005. Longest Palindromic Substring ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 006. ZigZag Conversion ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 007. Reverse Integer ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 008. String to Integer (atoi) ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 009. Palindrome Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 010. Regular Expression Matching ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 011. Container With Most Water ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 012. Integer to Roman ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 013. Roman to Integer ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 014. Longest Common Prefix ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 015. 3Sum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 016. 3Sum Closest ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 017. Letter Combinations of a Phone Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 018. 4Sum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 019. Remove Nth Node From End of List ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 020. Valid Parentheses ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 021. Merge Two Sorted Lists ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 022. Generate Parentheses ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 023. Merge k Sorted Lists ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 024. Swap Nodes in Pairs ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 025. Reverse Nodes in k-Group ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 026. Remove Duplicates from Sorted Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 027. Remove Element ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 028. Implement strStr() ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 029. Divide Two Integers ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 030. Substring with Concatenation of All Words ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 031. Next Permutation ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 032. Longest Valid Parentheses ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 033. Search in Rotated Sorted Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 034. Search for a Range ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 035. Search Insert Position ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 036. Valid Sudoku ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 037. Sudoku Solver ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 039. Combination Sum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 040. Combination Sum II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 041. First Missing Positive ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 042. Trapping Rain Water ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 043. Multiply Strings ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 044. Wildcard Matching ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 045. Jump Game II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 046. Permutations ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 047. Permutations II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 048. Rotate Image ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 049. Group Anagrams ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 050. Pow(x, n) ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 051. N-Queens ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 052. N-Queens II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 053. Maximum Subarray ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 054. Spiral Matrix ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 055. Jump Game ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 056. Merge Intervals ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 057. Insert Interval ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 058. Length of Last Word ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 059. Spiral Matrix II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 060. Permutation Sequence ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 061. Rotate List ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 062. Unique Paths ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 063. Unique Paths II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 064. Minimum Path Sum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 065. Valid Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 066. Plus One ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 067. Add Binary ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 068. Text Justification ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 069. Sqrt(x) ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 070. Climbing Stairs ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 071. Simplify Path ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 072. Edit Distance ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 073. Set Matrix Zeroes ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 074. Search a 2D Matrix ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 075. Sort Colors ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 076. Minimum Window Substring ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 077. Combinations ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 078. Subsets ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 079. Word Search ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 080. Remove Duplicates from Sorted Array II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 081. Search in Rotated Sorted Array II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 082. Remove Duplicates from Sorted List II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 083. Remove Duplicates from Sorted List ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 084. Largest Rectangle in Histogram ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 085. Maximal Rectangle ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 086. Partition List ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 087. Scramble String ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 088. Merge Sorted Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 089. Gray Code ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 090. Subsets II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 091. Decode Ways ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 092. Reverse Linked List II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 093. Restore IP Addresses ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 094. Binary Tree Inorder Traversal ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 095. Unique Binary Search Trees II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 096. Unique Binary Search Trees ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 097. Interleaving String ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 098. Validate Binary Search Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 099. Recover Binary Search Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 100. Same Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 101. Symmetric Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 102. Binary Tree Level Order Traversal ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 103. Binary Tree Zigzag Level Order Traversal ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 104. Maximum Depth of Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 105. Construct Binary Tree from Preorder and Inorder Traversal ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 106. Construct Binary Tree from Inorder and Postorder Traversal ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 107. Binary Tree Level Order Traversal II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 108. Convert Sorted Array to Binary Search Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 109. Convert Sorted List to Binary Search Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 110. Balanced Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 111. Minimum Depth of Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 112. Path Sum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 113. Path Sum II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 114. Flatten Binary Tree to Linked List ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 115. Distinct Subsequences ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 116. Populating Next Right Pointers in Each Node ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 117. Populating Next Right Pointers in Each Node II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 118. Pascal's Triangle ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 119. Pascal's Triangle II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 120. Triangle ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 121. Best Time to Buy and Sell Stock ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 122. Best Time to Buy and Sell Stock II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 123. Best Time to Buy and Sell Stock III ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 124. Binary Tree Maximum Path Sum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 125. Valid Palindrome ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 126. Word Ladder II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 127. Word Ladder ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 128. Longest Consecutive Sequence ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 129. Sum Root to Leaf Numbers ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 130. Surrounded Regions ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 131. Palindrome Partitioning ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 132. Palindrome Partitioning II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 133. Clone Graph ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 134. Gas Station ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 135. Candy ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 136. Single Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 137. Single Number II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 138. Copy List with Random Pointer ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 139. Word Break ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 140. Word Break II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 141. Linked List Cycle ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 142. Linked List Cycle II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 143. Reorder List ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 144. Binary Tree Preorder Traversal ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 145. Binary Tree Postorder Traversal ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 146. LRU Cache ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 147. Insertion Sort List ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 148. Sort List ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 149. Max Points on a Line ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 150. Evaluate Reverse Polish Notation ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 151. Reverse Words in a String ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 152. Maximum Product Subarray ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 153. Find Minimum in Rotated Sorted Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 154. Find Minimum in Rotated Sorted Array II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 155. Min Stack ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 160. Intersection of Two Linked Lists ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 162. Find Peak Element ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 164. Maximum Gap ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 165. Compare Version Numbers ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 166. Fraction to Recurring Decimal ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 167. Two Sum II - Input array is sorted ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 168. Excel Sheet Column Title ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 169. Majority Element ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 171. Excel Sheet Column Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 172. Factorial Trailing Zeroes ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 173. Binary Search Tree Iterator ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 174. Dungeon Game ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 179. Largest Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 187. Repeated DNA Sequences ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 188. Best Time to Buy and Sell Stock IV ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 189. Rotate Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 190. Reverse Bits ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 191. Number of 1 Bits ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 198. House Robber ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 199. Binary Tree Right Side View ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 200. Number of Islands ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 201. Bitwise AND of Numbers Range ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 202. Happy Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 203. Remove Linked List Elements ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 204. Count Primes ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 205. Isomorphic Strings ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 206. Reverse Linked List ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 207. Course Schedule ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 208. Implement Trie (Prefix Tree) ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 209. Minimum Size Subarray Sum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 210. Course Schedule II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 211. Add and Search Word - Data structure design ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 212. Word Search II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 213. House Robber II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 214. Shortest Palindrome ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 215. Kth Largest Element in an Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 216. Combination Sum III ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 217. Contains Duplicate ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 218. The Skyline Problem ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 219. Contains Duplicate II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 220. Contains Duplicate III ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 221. Maximal Square ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 222. Count Complete Tree Nodes ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 223. Rectangle Area ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 224. Basic Calculator ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 225. Implement Stack using Queues ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 226. Invert Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 227. Basic Calculator II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 228. Summary Ranges ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 229. Majority Element II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 230. Kth Smallest Element in a BST ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 231. Power of Two ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 232. Implement Queue using Stacks ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 233. Number of Digit One ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 234. Palindrome Linked List ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 235. Lowest Common Ancestor of a Binary Search Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 236. Lowest Common Ancestor of a Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 237. Delete Node in a Linked List ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 238. Product of Array Except Self ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 239. Sliding Window Maximum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 240. Search a 2D Matrix II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 241. Different Ways to Add Parentheses ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 242. Valid Anagram ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 257. Binary Tree Paths ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 258. Add Digits ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 260. Single Number III ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 263. Ugly Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 264. Ugly Number II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 268. Missing Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 273. Integer to English Words ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 274. H-Index ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 275. H-Index II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 278. First Bad Version ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 279. Perfect Squares ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 282. Expression Add Operators ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 283. Move Zeroes ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 284. Peeking Iterator ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 287. Find the Duplicate Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 289. Game of Life ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 290. Word Pattern ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 292. Nim Game ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 295. Find Median from Data Stream ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 297. Serialize and Deserialize Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 299. Bulls and Cows ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 300. Longest Increasing Subsequence ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 303. Range Sum Query - Immutable ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 304. Range Sum Query 2D - Immutable ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 306. Additive Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 307. Range Sum Query - Mutable ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 309. Best Time to Buy and Sell Stock with Cooldown ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 310. Minimum Height Trees ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 312. Burst Balloons ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 313. Super Ugly Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 315. Count of Smaller Numbers After Self ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 316. Remove Duplicate Letters ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 318. Maximum Product of Word Lengths ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 319. Bulb Switcher ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 321. Create Maximum Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 322. Coin Change ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 324. Wiggle Sort II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 326. Power of Three ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 328. Odd Even Linked List ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 329. Longest Increasing Path in a Matrix ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 330. Patching Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 331. Verify Preorder Serialization of a Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 332. Reconstruct Itinerary ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 334. Increasing Triplet Subsequence ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 335. Self Crossing ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 336. Palindrome Pairs ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 337. House Robber III ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 338. Counting Bits ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 341. Flatten Nested List Iterator ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 342. Power of Four ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 343. Integer Break ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 344. Reverse String ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 345. Reverse Vowels of a String ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 347. Top K Frequent Elements ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 349. Intersection of Two Arrays ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 350. Intersection of Two Arrays II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 352. Data Stream as Disjoint Intervals ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 354. Russian Doll Envelopes ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 355. Design Twitter ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 357. Count Numbers with Unique Digits ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 363. Max Sum of Rectangle No Larger Than K ├── C++ │ └── Solution.cpp └── Java │ └── Solution.java ├── 365. Water and Jug Problem ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 367. Valid Perfect Square ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 368. Largest Divisible Subset ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 371. Sum of Two Integers ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 372. Super Pow ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 373. Find K Pairs with Smallest Sums ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 374. Guess Number Higher or Lower ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 375. Guess Number Higher or Lower II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 376. Wiggle Subsequence ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 377. Combination Sum IV ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 378. Kth Smallest Element in a Sorted Matrix ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 380. Insert Delete GetRandom O(1) ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 381. Insert Delete GetRandom O(1) - Duplicates allowed ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 382. Linked List Random Node ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 383. Ransom Note ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 384. Shuffle an Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 385. Mini Parser ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 386. Lexicographical Numbers ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 387. First Unique Character in a String ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 388. Longest Absolute File Path ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 389. Find the Difference ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 390. Elimination Game ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 391. Perfect Rectangle ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 392. Is Subsequence ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 393. UTF-8 Validation ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 394. Decode String ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 395. Longest Substring with At Least K Repeating Characters ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 396. Rotate Function ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 397. Integer Replacement ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 398. Random Pick Index ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 399. Evaluate Division ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 400. Nth Digit ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 401. Binary Watch ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 402. Remove K Digits ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 403. Frog Jump ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 404. Sum of Left Leaves ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 405. Convert a Number to Hexadecimal ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 406. Queue Reconstruction by Height ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 407. Trapping Rain Water II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 409. Longest Palindrome ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 410. Split Array Largest Sum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 412. Fizz Buzz ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 413. Arithmetic Slices ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 414. Third Maximum Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 415. Add Strings ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 416. Partition Equal Subset Sum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 417. Pacific Atlantic Water Flow ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 419. Battleships in a Board ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 421. Maximum XOR of Two Numbers in an Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 423. Reconstruct Original Digits from English ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 424. Longest Repeating Character Replacement ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 432. All O`one Data Structure ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 434. Number of Segments in a String ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 435. Non-overlapping Intervals ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 436. Find Right Interval ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 437. Path Sum III ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 438. Find All Anagrams in a String ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 440. K-th Smallest in Lexicographical Order ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 441. Arranging Coins ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 442. Find All Duplicates in an Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 445. Add Two Numbers II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 446. Arithmetic Slices II - Subsequence ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 447. Number of Boomerangs ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 448. Find All Numbers Disappeared in an Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 449. Serialize and Deserialize BST ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 450. Delete Node in a BST ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 451. Sort Characters By Frequency ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 452. Minimum Number of Arrows to Burst Balloons ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 453. Minimum Moves to Equal Array Elements ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 454. 4Sum II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 455. Assign Cookies ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 456. 132 Pattern ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 459. Repeated Substring Pattern ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 460. LFU Cache ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 461. Hamming Distance ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 462. Minimum Moves to Equal Array Elements II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 463. Island Perimeter ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 464. Can I Win ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 467. Unique Substrings in Wraparound String ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 468. Validate IP Address ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 472. Concatenated Words ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 473. Matchsticks to Square ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 474. Ones and Zeroes ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 475. Heaters ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 476. Number Complement ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 477. Total Hamming Distance ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 479. Largest Palindrome Product ├── C++ │ └── Solution.cpp └── Java │ └── Solution.java ├── 480. Sliding Window Median ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 482. License Key Formatting ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 485. Max Consecutive Ones ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 486. Predict the Winner ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 491. Increasing Subsequences ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 492. Construct the Rectangle ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 493. Reverse Pairs ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 494. Target Sum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 495. Teemo Attacking ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 496. Next Greater Element I ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 498. Diagonal Traverse ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 500. Keyboard Row ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 501. Find Mode in Binary Search Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 502. IPO ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 503. Next Greater Element II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 504. Base 7 ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 506. Relative Ranks ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 507. Perfect Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 508. Most Frequent Subtree Sum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 513. Find Bottom Left Tree Value ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 514. Freedom Trail ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 515. Find Largest Value in Each Tree Row ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 516. Longest Palindromic Subsequence ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 517. Super Washing Machines ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 520. Detect Capital ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 521. Longest Uncommon Subsequence I ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 522. Longest Uncommon Subsequence II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 523. Continuous Subarray Sum ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 524. Longest Word in Dictionary through Deleting ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 525. Contiguous Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 526. Beautiful Arrangement ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 529. Minesweeper ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 530. Minimum Absolute Difference in BST ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 532. K-diff Pairs in an Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 535. Encode and Decode TinyURL ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 537. Complex Number Multiplication ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 538. Convert BST to Greater Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 539. Minimum Time Difference ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 540. Single Element in a Sorted Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 541. Reverse String II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 542. 01 Matrix ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 543. Diameter of Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 547. Friend Circles ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 551. Student Attendance Record I ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 552. Student Attendance Record II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 553. Optimal Division ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 554. Brick Wall ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 556. Next Greater Element III ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 557. Reverse Words in a String III ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 560. Subarray Sum Equals K ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 561. Array Partition I ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 563. Binary Tree Tilt ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 566. Reshape the Matrix ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 567. Permutation in String ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 572. Subtree of Another Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 575. Distribute Candies ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 576. Out of Boundary Paths ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 581. Shortest Unsorted Continuous Subarray ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 583. Delete Operation for Two Strings ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 592. Fraction Addition and Subtraction ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 593. Valid Square ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 594. Longest Harmonious Subsequence ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 598. Range Addition II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 599. Minimum Index Sum of Two Lists ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 605. Can Place Flowers ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 606. Construct String from Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 609. Find Duplicate File in System ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 611. Valid Triangle Number ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 617. Merge Two Binary Trees ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 621. Task Scheduler ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 623. Add One Row to Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 628. Maximum Product of Three Numbers ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 632. Smallest Range ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 633. Sum of Square Numbers ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 636. Exclusive Time of Functions ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 637. Average of Levels in Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 638. Shopping Offers ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 640. Solve the Equation ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 643. Maximum Average Subarray I ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 645. Set Mismatch ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 646. Maximum Length of Pair Chain ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 647. Palindromic Substrings ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 648. Replace Words ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 649. Dota2 Senate ├── C++ │ └── Solution.cpp └── Java │ └── Solution.java ├── 650. 2 Keys Keyboard ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 652. Find Duplicate Subtrees ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 653. Two Sum IV - Input is a BST ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 654. Maximum Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 655. Print Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 657. Judge Route Circle ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 658. Find K Closest Elements ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 659. Split Array into Consecutive Subsequences ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 661. Image Smoother ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 662. Maximum Width of Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 665. Non-decreasing Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 667. Beautiful Arrangement II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 669. Trim a Binary Search Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 670. Maximum Swap ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 671. Second Minimum Node In a Binary Tree ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 672. Bulb Switcher II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 673. Number of Longest Increasing Subsequence ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 674. Longest Continuous Increasing Subsequence ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 676. Implement Magic Dictionary ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 677. Map Sum Pairs ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 678. Valid Parenthesis String ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 679. 24 Game ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 680. Valid Palindrome II ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 681. Next Closest Time ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 682. Baseball Game ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 683. K Empty Slots ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 684. Redundant Connection ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 686. Repeated String Match ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 687. Longest Univalue Path ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 688. Knight Probability in Chessboard ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 689. Maximum Sum of 3 Non-Overlapping Subarrays ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 690. Employee Importance ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 692. Top K Frequent Words ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 693. Binary Number with Alternating Bits ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 695. Max Area of Island ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 696. Count Binary Substrings ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 697. Degree of an Array ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py ├── 698. Partition to K Equal Sum Subsets ├── C++ │ └── Solution.cpp └── Java │ └── Solution.java ├── 712. Minimum ASCII Delete Sum for Two Strings ├── C++ │ └── Solution.cpp ├── Java │ └── Solution.java └── Python │ └── Solution.py └── README.md /001. Two Sum/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector &nums, int target) { 4 | map needs; 5 | for (int i = 0; i < nums.size(); i++) { 6 | int num = nums[i]; 7 | if (needs.find(num) != needs.end()) { 8 | return vector {needs[num], i}; 9 | } 10 | needs.insert({target - num, i}); 11 | } 12 | } 13 | }; -------------------------------------------------------------------------------- /001. Two Sum/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | Map needs = new HashMap<>(); 4 | for (int i = 0; i < nums.length; i++) { 5 | int num = nums[i]; 6 | if (needs.containsKey(num)) { 7 | return new int[] {needs.get(num), i}; 8 | } 9 | needs.put(target - num, i); 10 | } 11 | return null; 12 | } 13 | } -------------------------------------------------------------------------------- /001. Two Sum/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def twoSum(self, nums, target): 3 | """ 4 | :type nums: List[int] 5 | :type target: int 6 | :rtype: List[int] 7 | """ 8 | needs = {} 9 | for (index, num) in enumerate(nums): 10 | if num in needs: 11 | return [needs[num], index] 12 | needs[target - num] = index 13 | -------------------------------------------------------------------------------- /003. Longest Substring Without Repeating Characters/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | unordered_map memory; 5 | int result = 0; 6 | for (int i = 0, j = 0; i < s.length(); i++) { 7 | if (memory.find(s[i]) != memory.end()) { 8 | j = max(j, memory[s[i]] + 1); 9 | } 10 | memory[s[i]] = i; 11 | result = max(result, i - j + 1); 12 | } 13 | return result; 14 | } 15 | }; -------------------------------------------------------------------------------- /003. Longest Substring Without Repeating Characters/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int lengthOfLongestSubstring(String s) { 3 | Map memory = new HashMap<>(); 4 | int result = 0; 5 | for (int i = 0, j = 0; i < s.length(); i++) { 6 | char letter = s.charAt(i); 7 | if (memory.containsKey(letter)) { 8 | j = Math.max(j, memory.get(letter) + 1); 9 | } 10 | memory.put(letter, i); 11 | result = Math.max(result, i - j + 1); 12 | } 13 | return result; 14 | } 15 | } -------------------------------------------------------------------------------- /003. Longest Substring Without Repeating Characters/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def lengthOfLongestSubstring(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | memory = {} 8 | result = 0 9 | j = 0 10 | for i, letter in enumerate(s): 11 | if letter in memory: 12 | j = max(j, memory[letter] + 1) 13 | memory[letter] = i 14 | result = max(result, i - j + 1) 15 | return result 16 | -------------------------------------------------------------------------------- /005. Longest Palindromic Substring/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def longestPalindrome(self, s): 3 | """ 4 | :type s: str 5 | :rtype: str 6 | """ 7 | result = "" 8 | dp = [[False] * len(s) for i in range(len(s))] 9 | for i in range(len(s) - 1, -1, -1): 10 | for j in range(i, len(s)): 11 | dp[i][j] = s[i] == s[j] and (j - i < 3 or dp[i + 1][j - 1]) 12 | if dp[i][j] and j - i + 1 > len(result): 13 | result = s[i:j + 1] 14 | return result 15 | -------------------------------------------------------------------------------- /007. Reverse Integer/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reverse(int x) { 4 | long result = 0; 5 | while (x) { 6 | result = result * 10 + x % 10; 7 | x /= 10; 8 | } 9 | if (result > INT_MAX || result < INT_MIN) { 10 | return 0; 11 | } 12 | return (int) result; 13 | } 14 | }; -------------------------------------------------------------------------------- /007. Reverse Integer/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int reverse(int x) { 3 | long result = 0; 4 | while (x != 0) { 5 | result = result * 10 + x % 10; 6 | x /= 10; 7 | } 8 | if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) { 9 | return 0; 10 | } 11 | return (int) result; 12 | } 13 | } -------------------------------------------------------------------------------- /007. Reverse Integer/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def reverse(self, x): 3 | """ 4 | :type x: int 5 | :rtype: int 6 | """ 7 | sign = -1 if x < 0 else 1 8 | x *= sign 9 | result = 0 10 | while x: 11 | result = result * 10 + x % 10 12 | x //= 10 13 | if result > 0x7fffffff: 14 | return 0 15 | return result * sign 16 | -------------------------------------------------------------------------------- /009. Palindrome Number/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | if (x == 0) { 5 | return true; 6 | } 7 | if (x < 0 || x % 10 == 0) { 8 | return false; 9 | } 10 | int palindrome = 0; 11 | while (palindrome < x) { 12 | palindrome = palindrome * 10 + x % 10; 13 | x /= 10; 14 | } 15 | return (x == palindrome) || (x == palindrome / 10); 16 | } 17 | }; -------------------------------------------------------------------------------- /009. Palindrome Number/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPalindrome(int x) { 3 | if (x == 0) { 4 | return true; 5 | } 6 | if (x < 0 || x % 10 == 0) { 7 | return false; 8 | } 9 | int palindrome = 0; 10 | while (palindrome < x) { 11 | palindrome = palindrome * 10 + x % 10; 12 | x /= 10; 13 | } 14 | return x == palindrome || x == palindrome / 10; 15 | } 16 | } -------------------------------------------------------------------------------- /009. Palindrome Number/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isPalindrome(self, x): 3 | """ 4 | :type x: int 5 | :rtype: bool 6 | """ 7 | if x == 0: 8 | return True 9 | if x < 0 or x % 10 == 0: 10 | return False 11 | palindrome = 0 12 | while palindrome < x: 13 | palindrome = palindrome * 10 + x % 10 14 | x //= 10 15 | return x == palindrome or x == palindrome // 10 16 | -------------------------------------------------------------------------------- /012. Integer to Roman/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string intToRoman(int num) { 4 | vector words{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; 5 | vector values{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; 6 | string result = ""; 7 | for (int i = 0; i < words.size(); i++) { 8 | while (num >= values[i]) { 9 | result += words[i]; 10 | num -= values[i]; 11 | } 12 | } 13 | return result; 14 | } 15 | }; -------------------------------------------------------------------------------- /012. Integer to Roman/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def intToRoman(self, num): 3 | """ 4 | :type num: int 5 | :rtype: str 6 | """ 7 | words = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'] 8 | values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] 9 | result = '' 10 | for i in range(len(words)): 11 | while num >= values[i]: 12 | result += words[i] 13 | num -= values[i] 14 | return result 15 | -------------------------------------------------------------------------------- /014. Longest Common Prefix/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestCommonPrefix(vector &strs) { 4 | if (strs.empty()) { 5 | return ""; 6 | } 7 | string firstStr = strs[0]; 8 | int commonIndex = 0; 9 | while (true) { 10 | for (string &str : strs) { 11 | if (str.size() == commonIndex || str[commonIndex] != firstStr[commonIndex]) { 12 | return firstStr.substr(0, commonIndex); 13 | } 14 | } 15 | commonIndex++; 16 | } 17 | } 18 | }; -------------------------------------------------------------------------------- /014. Longest Common Prefix/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def longestCommonPrefix(self, strs): 3 | """ 4 | :type strs: List[str] 5 | :rtype: str 6 | """ 7 | if not strs: 8 | return '' 9 | first_str = strs[0] 10 | common_index = 0 11 | while True: 12 | for str in strs: 13 | if len(str) == common_index or str[common_index] != first_str[common_index]: 14 | return first_str[0:common_index] 15 | common_index += 1 16 | -------------------------------------------------------------------------------- /026. Remove Duplicates from Sorted Array/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def removeDuplicates(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | if not nums: 8 | return 0 9 | i = 0 10 | for j in range(1, len(nums)): 11 | if nums[j] != nums[i]: 12 | i += 1 13 | nums[i] = nums[j] 14 | return i + 1 15 | -------------------------------------------------------------------------------- /027. Remove Element/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeElement(vector &nums, int val) { 4 | int i = 0; 5 | int j = nums.size() - 1; 6 | while (i <= j) { 7 | if (nums[i] != val) { 8 | i++; 9 | } else if (nums[j] == val) { 10 | j--; 11 | } else { 12 | int temp = nums[i]; 13 | nums[i] = nums[j]; 14 | nums[j] = temp; 15 | } 16 | } 17 | return i; 18 | } 19 | }; -------------------------------------------------------------------------------- /027. Remove Element/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int removeElement(int[] nums, int val) { 3 | int i = 0; 4 | int j = nums.length - 1; 5 | while (i <= j) { 6 | if (nums[i] != val) { 7 | i++; 8 | } else if (nums[j] == val) { 9 | j--; 10 | } else { 11 | int temp = nums[i]; 12 | nums[i] = nums[j]; 13 | nums[j] = temp; 14 | } 15 | } 16 | return i; 17 | } 18 | } -------------------------------------------------------------------------------- /027. Remove Element/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def removeElement(self, nums, val): 3 | """ 4 | :type nums: List[int] 5 | :type val: int 6 | :rtype: int 7 | """ 8 | i = 0 9 | j = len(nums) - 1 10 | while i <= j: 11 | if nums[i] != val: 12 | i += 1 13 | elif nums[j] == val: 14 | j -= 1 15 | else: 16 | nums[i], nums[j] = nums[j], nums[i] 17 | return i 18 | -------------------------------------------------------------------------------- /028. Implement strStr()/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int strStr(string haystack, string needle) { 4 | for (int i = 0; i < (int) haystack.size() - (int) needle.size() + 1; i++) { 5 | int j = 0; 6 | while (j < needle.size()) { 7 | if (haystack[i + j] != needle[j]) { 8 | break; 9 | } 10 | j++; 11 | } 12 | if (j == needle.size()) { 13 | return i; 14 | } 15 | } 16 | return -1; 17 | } 18 | }; -------------------------------------------------------------------------------- /028. Implement strStr()/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int strStr(String haystack, String needle) { 3 | for (int i = 0; i < haystack.length() - needle.length() + 1; i++) { 4 | int j = 0; 5 | while (j < needle.length()) { 6 | if (haystack.charAt(i + j) != needle.charAt(j)) { 7 | break; 8 | } 9 | j++; 10 | } 11 | if (j == needle.length()) { 12 | return i; 13 | } 14 | } 15 | return -1; 16 | } 17 | } -------------------------------------------------------------------------------- /028. Implement strStr()/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def strStr(self, haystack, needle): 3 | """ 4 | :type haystack: str 5 | :type needle: str 6 | :rtype: int 7 | """ 8 | for i in range(0, len(haystack) - len(needle) + 1): 9 | j = 0 10 | while j < len(needle): 11 | if haystack[i + j] != needle[j]: 12 | break 13 | j += 1 14 | if j == len(needle): 15 | return i 16 | return -1 17 | -------------------------------------------------------------------------------- /031. Next Permutation/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public void nextPermutation(int[] nums) { 3 | int i = nums.length - 2; 4 | while (i >= 0 && nums[i] >= nums[i + 1]) { 5 | i--; 6 | } 7 | if (i < 0) { 8 | Arrays.sort(nums); 9 | return; 10 | } 11 | int j = nums.length - 1; 12 | while (nums[j] <= nums[i]) { 13 | j--; 14 | } 15 | int temp = nums[i]; 16 | nums[i] = nums[j]; 17 | nums[j] = temp; 18 | Arrays.sort(nums, i + 1, nums.length); 19 | } 20 | } -------------------------------------------------------------------------------- /031. Next Permutation/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def nextPermutation(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: void Do not return anything, modify nums in-place instead. 6 | """ 7 | i = len(nums) - 2 8 | while i >= 0 and nums[i] >= nums[i + 1]: 9 | i -= 1 10 | if i < 0: 11 | nums.sort() 12 | return 13 | j = len(nums) - 1 14 | while nums[j] <= nums[i]: 15 | j -= 1 16 | nums[i], nums[j] = nums[j], nums[i] 17 | nums[i + 1:] = sorted(nums[i + 1:]) 18 | -------------------------------------------------------------------------------- /035. Search Insert Position/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def searchInsert(self, nums, target): 3 | """ 4 | :type nums: List[int] 5 | :type target: int 6 | :rtype: int 7 | """ 8 | low = 0 9 | high = len(nums) - 1 10 | while low <= high: 11 | mid = (low + high) // 2 12 | if nums[mid] == target: 13 | return mid 14 | if nums[mid] > target: 15 | high = mid - 1 16 | else: 17 | low = mid + 1 18 | return low 19 | -------------------------------------------------------------------------------- /041. First Missing Positive/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def firstMissingPositive(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | for i in range(len(nums)): 8 | while nums[i] > 0 and nums[i] <= len(nums) and nums[nums[i] - 1] != nums[i]: 9 | a = i 10 | b = nums[i] - 1 11 | nums[a], nums[b] = nums[b], nums[a] 12 | for i in range(len(nums)): 13 | if nums[i] != i + 1: 14 | return i + 1 15 | return len(nums) + 1 16 | -------------------------------------------------------------------------------- /045. Jump Game II/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int jump(vector &nums) { 4 | int result = 0; 5 | int index = 0; 6 | int current = 0; 7 | int next = 0; 8 | while (current < nums.size() - 1) { 9 | result++; 10 | while (index <= current) { 11 | next = max(next, index + nums[index]); 12 | index++; 13 | } 14 | current = next; 15 | } 16 | return result; 17 | } 18 | }; -------------------------------------------------------------------------------- /045. Jump Game II/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int jump(int[] nums) { 3 | int result = 0; 4 | int index = 0; 5 | int current = 0; 6 | int next = 0; 7 | while (current < nums.length - 1) { 8 | result++; 9 | while (index <= current) { 10 | next = Math.max(next, index + nums[index]); 11 | index++; 12 | } 13 | current = next; 14 | } 15 | return result; 16 | } 17 | } -------------------------------------------------------------------------------- /045. Jump Game II/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def jump(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | result = 0 8 | index = 0 9 | current = 0 10 | next = 0 11 | while current < len(nums) - 1: 12 | result += 1 13 | while index <= current: 14 | next = max(next, index + nums[index]) 15 | index += 1 16 | current = next 17 | return result 18 | -------------------------------------------------------------------------------- /046. Permutations/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def permute(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: List[List[int]] 6 | """ 7 | result = [] 8 | result.append([]) 9 | for i, num in enumerate(nums): 10 | new_result = [] 11 | for j in range(i + 1): 12 | for item in result: 13 | new_item = item[:] 14 | new_item.insert(j, num) 15 | new_result.append(new_item) 16 | result = new_result 17 | return result 18 | -------------------------------------------------------------------------------- /048. Rotate Image/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def rotate(self, matrix): 3 | """ 4 | :type matrix: List[List[int]] 5 | :rtype: void Do not return anything, modify matrix in-place instead. 6 | """ 7 | m = len(matrix) 8 | n = len(matrix[0]) 9 | for i in range(m // 2): 10 | matrix[i], matrix[m - i - 1] = matrix[m - i - 1], matrix[i] 11 | for i in range(m): 12 | for j in range(i + 1, n): 13 | matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] 14 | -------------------------------------------------------------------------------- /049. Group Anagrams/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> groupAnagrams(vector &strs) { 4 | unordered_map> memory; 5 | for (const auto &word : strs) { 6 | string sortedWord = word; 7 | sort(sortedWord.begin(), sortedWord.end()); 8 | memory[sortedWord].push_back(word); 9 | } 10 | vector> result; 11 | for (const auto &item : memory) { 12 | result.push_back(item.second); 13 | } 14 | return result; 15 | } 16 | }; -------------------------------------------------------------------------------- /049. Group Anagrams/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def groupAnagrams(self, strs): 3 | """ 4 | :type strs: List[str] 5 | :rtype: List[List[str]] 6 | """ 7 | memory = {} 8 | for word in strs: 9 | sorted_word = ''.join(sorted(word)) 10 | if sorted_word not in memory: 11 | memory[sorted_word] = [] 12 | memory[sorted_word].append(word) 13 | return list(memory.values()) 14 | -------------------------------------------------------------------------------- /050. Pow(x, n)/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) { 4 | if (n == 0) { 5 | return 1; 6 | } 7 | if (n == INT_MIN) { 8 | n++; 9 | n = -n; 10 | x = 1 / x; 11 | return x * x * myPow(x * x, n / 2); 12 | } 13 | if (n < 0) { 14 | n = -n; 15 | x = 1 / x; 16 | } 17 | if (n % 2 == 0) { 18 | return myPow(x * x, n / 2); 19 | } 20 | return x * myPow(x * x, n / 2); 21 | } 22 | }; -------------------------------------------------------------------------------- /050. Pow(x, n)/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public double myPow(double x, int n) { 3 | if (n == 0) { 4 | return 1; 5 | } 6 | if (n == Integer.MIN_VALUE) { 7 | n++; 8 | n = -n; 9 | x = 1 / x; 10 | return x * x * myPow(x * x, n / 2); 11 | } 12 | if (n < 0) { 13 | n = -n; 14 | x = 1 / x; 15 | } 16 | if (n % 2 == 0) { 17 | return myPow(x * x, n / 2); 18 | } 19 | return x * myPow(x * x, n / 2); 20 | } 21 | } -------------------------------------------------------------------------------- /050. Pow(x, n)/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def myPow(self, x, n): 3 | """ 4 | :type x: float 5 | :type n: int 6 | :rtype: float 7 | """ 8 | if not n: 9 | return 1 10 | if n < 0: 11 | n = -n 12 | x = 1 / x 13 | if n % 2: 14 | return x * self.myPow(x * x, n // 2) 15 | return self.myPow(x * x, n // 2) 16 | -------------------------------------------------------------------------------- /053. Maximum Subarray/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector &nums) { 4 | int maxEndingHere = nums[0]; 5 | int maxSoFar = nums[0]; 6 | for (int i = 1; i < nums.size(); i++) { 7 | maxEndingHere = max(nums[i], maxEndingHere + nums[i]); 8 | maxSoFar = max(maxSoFar, maxEndingHere); 9 | } 10 | return maxSoFar; 11 | } 12 | }; -------------------------------------------------------------------------------- /053. Maximum Subarray/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxSubArray(int[] nums) { 3 | int maxEndingHere = nums[0]; 4 | int maxSoFar = nums[0]; 5 | for (int i = 1; i < nums.length; i++) { 6 | maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]); 7 | maxSoFar = Math.max(maxSoFar, maxEndingHere); 8 | } 9 | return maxSoFar; 10 | } 11 | } -------------------------------------------------------------------------------- /053. Maximum Subarray/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxSubArray(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | maxEndingHere = nums[0] 8 | maxSoFar = nums[0] 9 | for num in nums[1:]: 10 | maxEndingHere = max(num, maxEndingHere + num) 11 | maxSoFar = max(maxSoFar, maxEndingHere) 12 | return maxSoFar 13 | -------------------------------------------------------------------------------- /055. Jump Game/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canJump(vector &nums) { 4 | int current = 0; 5 | int i = 0; 6 | while (i < nums.size() && i <= current) { 7 | current = max(current, i + nums[i]); 8 | i++; 9 | } 10 | return i == nums.size(); 11 | } 12 | }; -------------------------------------------------------------------------------- /055. Jump Game/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean canJump(int[] nums) { 3 | int current = 0; 4 | int i = 0; 5 | while (i < nums.length && i <= current) { 6 | current = Math.max(current, i + nums[i]); 7 | i++; 8 | } 9 | return i == nums.length; 10 | } 11 | } -------------------------------------------------------------------------------- /055. Jump Game/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def canJump(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: bool 6 | """ 7 | current = 0 8 | i = 0 9 | while i < len(nums) and i <= current: 10 | current = max(current, i + nums[i]) 11 | i += 1 12 | return i == len(nums) 13 | -------------------------------------------------------------------------------- /058. Length of Last Word/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLastWord(string s) { 4 | int length = 0; 5 | int index = s.size() - 1; 6 | while (index >= 0 && s[index] == ' ') { 7 | index--; 8 | } 9 | while (index >= 0 && s[index] != ' ') { 10 | index--; 11 | length++; 12 | } 13 | return length; 14 | } 15 | }; -------------------------------------------------------------------------------- /058. Length of Last Word/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int lengthOfLastWord(String s) { 3 | int length = 0; 4 | int index = s.length() - 1; 5 | while (index >= 0 && s.charAt(index) == ' ') { 6 | index--; 7 | } 8 | while (index >= 0 && s.charAt(index) != ' ') { 9 | index--; 10 | length++; 11 | } 12 | return length; 13 | } 14 | } -------------------------------------------------------------------------------- /058. Length of Last Word/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def lengthOfLastWord(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | length = 0 8 | index = len(s) - 1 9 | while index >= 0 and s[index] == ' ': 10 | index -= 1 11 | while index >= 0 and s[index] != ' ': 12 | index -= 1 13 | length += 1 14 | return length 15 | -------------------------------------------------------------------------------- /062. Unique Paths/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniquePaths(int m, int n) { 4 | vector> dp(m, vector(n)); 5 | for (int i = 0; i < m; i++) { 6 | dp[i][0] = 1; 7 | } 8 | for (int j = 0; j < n; j++) { 9 | dp[0][j] = 1; 10 | } 11 | for (int i = 1; i < m; i++) { 12 | for (int j = 1; j < n; j++) { 13 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 14 | } 15 | } 16 | return dp[m - 1][n - 1]; 17 | } 18 | }; -------------------------------------------------------------------------------- /062. Unique Paths/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int uniquePaths(int m, int n) { 3 | int[][] dp = new int[m][n]; 4 | for (int i = 0; i < m; i++) { 5 | dp[i][0] = 1; 6 | } 7 | for (int j = 0; j < n; j++) { 8 | dp[0][j] = 1; 9 | } 10 | for (int i = 1; i < m; i++) { 11 | for (int j = 1; j < n; j++) { 12 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 13 | } 14 | } 15 | return dp[m - 1][n - 1]; 16 | } 17 | } -------------------------------------------------------------------------------- /062. Unique Paths/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def uniquePaths(self, m, n): 3 | """ 4 | :type m: int 5 | :type n: int 6 | :rtype: int 7 | """ 8 | dp = [[None] * n for i in range(m)] 9 | for i in range(m): 10 | dp[i][0] = 1 11 | for j in range(n): 12 | dp[0][j] = 1 13 | for i in range(1, m): 14 | for j in range(1, n): 15 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1] 16 | return dp[m - 1][n - 1] 17 | -------------------------------------------------------------------------------- /066. Plus One/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector plusOne(vector &digits) { 4 | int carry = 1; 5 | int index = digits.size() - 1; 6 | while (carry == 1 && index >= 0) { 7 | if (digits[index] == 9) { 8 | digits[index] = 0; 9 | } else { 10 | digits[index]++; 11 | carry = 0; 12 | } 13 | index--; 14 | } 15 | if (digits[0] == 0) { 16 | digits.push_back(0); 17 | digits[0] = 1; 18 | } 19 | return digits; 20 | } 21 | }; -------------------------------------------------------------------------------- /066. Plus One/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] plusOne(int[] digits) { 3 | int carry = 1; 4 | int index = digits.length - 1; 5 | while (carry == 1 && index >= 0) { 6 | if (digits[index] == 9) { 7 | digits[index] = 0; 8 | } else { 9 | digits[index]++; 10 | carry = 0; 11 | } 12 | index--; 13 | } 14 | if (digits[0] == 0) { 15 | digits = new int[digits.length + 1]; 16 | digits[0] = 1; 17 | } 18 | return digits; 19 | } 20 | } -------------------------------------------------------------------------------- /069. Sqrt(x)/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mySqrt(int x) { 4 | if (x == 0) { 5 | return 0; 6 | } 7 | int low = 1; 8 | int high = x; 9 | while (low <= high) { 10 | int mid = (low + high) / 2; 11 | if (mid <= x / mid) { 12 | low = mid + 1; 13 | } else { 14 | high = mid - 1; 15 | } 16 | } 17 | return low - 1; 18 | } 19 | }; -------------------------------------------------------------------------------- /069. Sqrt(x)/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int mySqrt(int x) { 3 | if (x == 0) { 4 | return 0; 5 | } 6 | int low = 1; 7 | int high = x; 8 | while (low <= high) { 9 | int mid = (low + high) / 2; 10 | if (mid <= x / mid) { 11 | low = mid + 1; 12 | } else { 13 | high = mid - 1; 14 | } 15 | } 16 | return low - 1; 17 | } 18 | } -------------------------------------------------------------------------------- /069. Sqrt(x)/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def mySqrt(self, x): 3 | """ 4 | :type x: int 5 | :rtype: int 6 | """ 7 | if x == 0: 8 | return 0 9 | low = 1 10 | high = x 11 | while low <= high: 12 | mid = (low + high) // 2 13 | if mid <= x / mid: 14 | low = mid + 1 15 | else: 16 | high = mid - 1 17 | return low - 1 18 | -------------------------------------------------------------------------------- /070. Climbing Stairs/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | if (n <= 2) { 5 | return n; 6 | } 7 | int a = 1; 8 | int b = 2; 9 | for (int i = 3; i <= n; i++) { 10 | int c = b; 11 | b += a; 12 | a = c; 13 | } 14 | return b; 15 | } 16 | }; -------------------------------------------------------------------------------- /070. Climbing Stairs/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int climbStairs(int n) { 3 | if (n <= 2) { 4 | return n; 5 | } 6 | int a = 1; 7 | int b = 2; 8 | for (int i = 3; i <= n; i++) { 9 | int c = b; 10 | b += a; 11 | a = c; 12 | } 13 | return b; 14 | } 15 | } -------------------------------------------------------------------------------- /070. Climbing Stairs/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def climbStairs(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | if n <= 2: 8 | return n 9 | a = 1 10 | b = 2 11 | for i in range(3, n + 1): 12 | c = b 13 | b += a 14 | a = c 15 | return b 16 | -------------------------------------------------------------------------------- /071. Simplify Path/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def simplifyPath(self, path): 3 | """ 4 | :type path: str 5 | :rtype: str 6 | """ 7 | stack = [] 8 | for item in path.split('/'): 9 | if item == '' or item == '.': 10 | continue 11 | if item == '..': 12 | if stack: 13 | stack.pop() 14 | else: 15 | stack.append(item) 16 | result = '/'.join(stack) 17 | result = '/' + result 18 | return result 19 | -------------------------------------------------------------------------------- /078. Subsets/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> subsets(vector &nums) { 4 | vector> result; 5 | for (int i = 0; i < 1 << nums.size(); i++) { 6 | vector current; 7 | for (int j = 0; j < nums.size(); j++) { 8 | if (i & 1 << j) { 9 | current.push_back(nums[j]); 10 | } 11 | } 12 | result.push_back(current); 13 | } 14 | return result; 15 | } 16 | }; -------------------------------------------------------------------------------- /078. Subsets/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List> subsets(int[] nums) { 3 | List> result = new ArrayList<>(); 4 | for (int i = 0; i < 1 << nums.length; i++) { 5 | List current = new ArrayList<>(); 6 | for (int j = 0; j < nums.length; j++) { 7 | if ((i & 1 << j) != 0) { 8 | current.add(nums[j]); 9 | } 10 | } 11 | result.add(current); 12 | } 13 | return result; 14 | } 15 | } -------------------------------------------------------------------------------- /078. Subsets/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def subsets(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: List[List[int]] 6 | """ 7 | result = [] 8 | for i in range(1 << len(nums)): 9 | current = [] 10 | for j in range(len(nums)): 11 | if i & 1 << j: 12 | current.append(nums[j]) 13 | result.append(current) 14 | return result 15 | -------------------------------------------------------------------------------- /088. Merge Sorted Array/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector &nums1, int m, vector &nums2, int n) { 4 | int index = m + n - 1; 5 | m--; 6 | n--; 7 | while (n >= 0) { 8 | if (m >= 0 && nums1[m] > nums2[n]) { 9 | nums1[index] = nums1[m]; 10 | m--; 11 | } else { 12 | nums1[index] = nums2[n]; 13 | n--; 14 | } 15 | index--; 16 | } 17 | } 18 | }; -------------------------------------------------------------------------------- /088. Merge Sorted Array/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public void merge(int[] nums1, int m, int[] nums2, int n) { 3 | int index = m + n - 1; 4 | m--; 5 | n--; 6 | while (n >= 0) { 7 | if (m >= 0 && nums1[m] > nums2[n]) { 8 | nums1[index] = nums1[m]; 9 | m--; 10 | } else { 11 | nums1[index] = nums2[n]; 12 | n--; 13 | } 14 | index--; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /089. Gray Code/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector grayCode(int n) { 4 | vector result; 5 | for (int i = 0; i < 1 << n; i++) { 6 | result.push_back(i ^ (i >> 1)); 7 | } 8 | return result; 9 | } 10 | }; -------------------------------------------------------------------------------- /089. Gray Code/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List grayCode(int n) { 3 | List result = new ArrayList<>(); 4 | for (int i = 0; i < 1 << n; i++) { 5 | result.add(i ^ (i >> 1)); 6 | } 7 | return result; 8 | } 9 | } -------------------------------------------------------------------------------- /089. Gray Code/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def grayCode(self, n): 3 | """ 4 | :type n: int 5 | :rtype: List[int] 6 | """ 7 | return [i ^ (i >> 1) for i in range(1 << n)] 8 | -------------------------------------------------------------------------------- /091. Decode Ways/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def numDecodings(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | if not s: 8 | return 0 9 | dp = [0] * (len(s) + 1) 10 | dp[0] = 1 11 | for i in range(1, len(s) + 1): 12 | if s[i - 1] != '0': 13 | dp[i] = dp[i - 1] 14 | if i > 1 and s[i - 2:i] > '09' and s[i - 2:i] < '27': 15 | dp[i] += dp[i - 2] 16 | return dp[len(s)] 17 | -------------------------------------------------------------------------------- /096. Unique Binary Search Trees/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numTrees(int n) { 4 | vector results(n + 1, 0); 5 | results[0] = 1; 6 | for (int i = 0; i <= n; i++) { 7 | for (int j = 0; j < i; j++) { 8 | results[i] += results[j] * results[i - j - 1]; 9 | } 10 | } 11 | return results[n]; 12 | } 13 | }; -------------------------------------------------------------------------------- /096. Unique Binary Search Trees/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int numTrees(int n) { 3 | int[] results = new int[n + 1]; 4 | results[0] = 1; 5 | for (int i = 1; i <= n; i++) { 6 | for (int j = 0; j < i; j++) { 7 | results[i] += results[j] * results[i - j - 1]; 8 | } 9 | } 10 | return results[n]; 11 | } 12 | } -------------------------------------------------------------------------------- /096. Unique Binary Search Trees/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def numTrees(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | results = [0] * (n + 1) 8 | results[0] = 1 9 | for i in range(1, n + 1): 10 | for j in range(i): 11 | results[i] += results[j] * results[i - j - 1] 12 | return results[n] 13 | -------------------------------------------------------------------------------- /100. Same Tree/Java/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public boolean isSameTree(TreeNode p, TreeNode q) { 12 | if ((p == null && q == null) || 13 | (p != null && q != null && p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right))) { 14 | return true; 15 | } 16 | return false; 17 | } 18 | } -------------------------------------------------------------------------------- /104. Maximum Depth of Binary Tree/Java/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public int maxDepth(TreeNode root) { 12 | if (root == null) { 13 | return 0; 14 | } 15 | int leftDepth = maxDepth(root.left); 16 | int rightDepth = maxDepth(root.right); 17 | return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1; 18 | } 19 | } -------------------------------------------------------------------------------- /119. Pascal's Triangle II/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getRow(int rowIndex) { 4 | vector result(rowIndex + 1, 0); 5 | result[0] = 1; 6 | for (int row = 1; row <= rowIndex; row++) { 7 | for (int i = row; i > 0; i--) { 8 | result[i] += result[i - 1]; 9 | } 10 | } 11 | return result; 12 | } 13 | }; -------------------------------------------------------------------------------- /119. Pascal's Triangle II/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List getRow(int rowIndex) { 3 | int[] result = new int[rowIndex + 1]; 4 | result[0] = 1; 5 | for (int row = 1; row <= rowIndex; row++) { 6 | for (int i = row; i > 0; i--) { 7 | result[i] += result[i - 1]; 8 | } 9 | } 10 | List resultList = new ArrayList<>(result.length); 11 | for (int item : result) { 12 | resultList.add(item); 13 | } 14 | return resultList; 15 | } 16 | } -------------------------------------------------------------------------------- /119. Pascal's Triangle II/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def getRow(self, rowIndex): 3 | """ 4 | :type rowIndex: int 5 | :rtype: List[int] 6 | """ 7 | result = [0] * (rowIndex + 1) 8 | result[0] = 1 9 | for row in range(1, rowIndex + 1): 10 | for i in range(row, 0, -1): 11 | result[i] += result[i - 1] 12 | return result 13 | -------------------------------------------------------------------------------- /120. Triangle/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumTotal(vector> &triangle) { 4 | vector dp = triangle.back(); 5 | for (int i = triangle.size() - 2; i >= 0; i--) { 6 | for (int j = 0; j <= i; j++) { 7 | dp[j] = min(dp[j], dp[j + 1]) + triangle[i][j]; 8 | } 9 | } 10 | return dp[0]; 11 | } 12 | }; -------------------------------------------------------------------------------- /120. Triangle/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int minimumTotal(List> triangle) { 3 | int[] dp = new int[triangle.size()]; 4 | for (int i = 0; i < dp.length; i++) { 5 | dp[i] = triangle.get(triangle.size() - 1).get(i); 6 | } 7 | for (int i = triangle.size() - 2; i >= 0; i--) { 8 | for (int j = 0; j <= i; j++) { 9 | dp[j] = Math.min(dp[j], dp[j + 1]) + triangle.get(i).get(j); 10 | } 11 | } 12 | return dp[0]; 13 | } 14 | } -------------------------------------------------------------------------------- /120. Triangle/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def minimumTotal(self, triangle): 3 | """ 4 | :type triangle: List[List[int]] 5 | :rtype: int 6 | """ 7 | dp = triangle[-1][:] 8 | for i in range(len(triangle) - 2, -1, -1): 9 | for j in range(i + 1): 10 | dp[j] = min(dp[j], dp[j + 1]) + triangle[i][j] 11 | return dp[0] 12 | -------------------------------------------------------------------------------- /121. Best Time to Buy and Sell Stock/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector &prices) { 4 | int maxEndingHere = 0; 5 | int maxSoFar = 0; 6 | for (int i = 1; i < prices.size(); i++) { 7 | maxEndingHere = max(0, maxEndingHere + prices[i] - prices[i - 1]); 8 | maxSoFar = max(maxSoFar, maxEndingHere); 9 | } 10 | return maxSoFar; 11 | } 12 | }; -------------------------------------------------------------------------------- /121. Best Time to Buy and Sell Stock/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxProfit(int[] prices) { 3 | int maxEndingHere = 0; 4 | int maxSoFar = 0; 5 | for (int i = 1; i < prices.length; i++) { 6 | maxEndingHere = Math.max(0, maxEndingHere + prices[i] - prices[i - 1]); 7 | maxSoFar = Math.max(maxSoFar, maxEndingHere); 8 | } 9 | return maxSoFar; 10 | } 11 | } -------------------------------------------------------------------------------- /121. Best Time to Buy and Sell Stock/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxProfit(self, prices): 3 | """ 4 | :type prices: List[int] 5 | :rtype: int 6 | """ 7 | max_ending_here = 0 8 | max_so_far = 0 9 | for i in range(1, len(prices)): 10 | max_ending_here = max(0, max_ending_here + (prices[i] - prices[i - 1])) 11 | max_so_far = max(max_so_far, max_ending_here) 12 | return max_so_far 13 | -------------------------------------------------------------------------------- /122. Best Time to Buy and Sell Stock II/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector &prices) { 4 | int profit = 0; 5 | for (int i = 1; i < prices.size(); i++) { 6 | int gain = prices[i] - prices[i - 1]; 7 | if (gain > 0) { 8 | profit += gain; 9 | } 10 | } 11 | return profit; 12 | } 13 | }; -------------------------------------------------------------------------------- /122. Best Time to Buy and Sell Stock II/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxProfit(int[] prices) { 3 | int profit = 0; 4 | for (int i = 1; i < prices.length; i++) { 5 | int gain = prices[i] - prices[i - 1]; 6 | if (gain > 0) { 7 | profit += gain; 8 | } 9 | } 10 | return profit; 11 | } 12 | } -------------------------------------------------------------------------------- /122. Best Time to Buy and Sell Stock II/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProfit(self, prices): 3 | """ 4 | :type prices: List[int] 5 | :rtype: int 6 | """ 7 | profit = 0 8 | for i in range(1, len(prices)): 9 | gain = prices[i] - prices[i - 1] 10 | if gain > 0: 11 | profit += gain 12 | return profit 13 | -------------------------------------------------------------------------------- /125. Valid Palindrome/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(string s) { 4 | int i = 0; 5 | int j = s.size() - 1; 6 | while (i < j) { 7 | if (!isalnum(s[i])) { 8 | i++; 9 | } else if (!isalnum(s[j])) { 10 | j--; 11 | } else { 12 | if (tolower(s[i]) != tolower(s[j])) { 13 | return false; 14 | } 15 | i++; 16 | j--; 17 | } 18 | } 19 | return true; 20 | } 21 | }; -------------------------------------------------------------------------------- /125. Valid Palindrome/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isPalindrome(self, s): 3 | """ 4 | :type s: str 5 | :rtype: bool 6 | """ 7 | i = 0 8 | j = len(s) - 1 9 | while i < j: 10 | if not s[i].isalnum(): 11 | i += 1 12 | elif not s[j].isalnum(): 13 | j -= 1 14 | else: 15 | if s[i].lower() != s[j].lower(): 16 | return False 17 | i += 1 18 | j -= 1 19 | return True 20 | -------------------------------------------------------------------------------- /134. Gas Station/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int canCompleteCircuit(vector &gas, vector &cost) { 4 | int start = gas.size() - 1; 5 | int end = 0; 6 | int gain = gas[start] - cost[start]; 7 | while (start > end) { 8 | if (gain >= 0) { 9 | gain += gas[end] - cost[end]; 10 | end++; 11 | } else { 12 | start--; 13 | gain += gas[start] - cost[start]; 14 | } 15 | } 16 | return gain >= 0 ? start : -1; 17 | } 18 | }; -------------------------------------------------------------------------------- /134. Gas Station/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int canCompleteCircuit(int[] gas, int[] cost) { 3 | int start = gas.length - 1; 4 | int end = 0; 5 | int gain = gas[start] - cost[start]; 6 | while (start > end) { 7 | if (gain >= 0) { 8 | gain += gas[end] - cost[end]; 9 | end++; 10 | } else { 11 | start--; 12 | gain += gas[start] - cost[start]; 13 | } 14 | } 15 | return gain >= 0 ? start : -1; 16 | } 17 | } -------------------------------------------------------------------------------- /135. Candy/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def candy(self, ratings): 3 | """ 4 | :type ratings: List[int] 5 | :rtype: int 6 | """ 7 | length = len(ratings) 8 | counts = [1] * length 9 | for i in range(1, length): 10 | if ratings[i] > ratings[i - 1]: 11 | counts[i] = counts[i - 1] + 1 12 | for i in range(length - 2, -1, -1): 13 | if ratings[i] > ratings[i + 1]: 14 | counts[i] = max(counts[i], counts[i + 1] + 1) 15 | return sum(counts) 16 | -------------------------------------------------------------------------------- /136. Single Number/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector &nums) { 4 | int result = 0; 5 | for (auto num : nums) { 6 | result ^= num; 7 | } 8 | return result; 9 | } 10 | }; -------------------------------------------------------------------------------- /136. Single Number/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int singleNumber(int[] nums) { 3 | int result = 0; 4 | for (int num : nums) { 5 | result ^= num; 6 | } 7 | return result; 8 | } 9 | } -------------------------------------------------------------------------------- /136. Single Number/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def singleNumber(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | result = 0 8 | for num in nums: 9 | result ^= num 10 | return result 11 | -------------------------------------------------------------------------------- /137. Single Number II/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector &nums) { 4 | int x1 = 0; 5 | int x2 = 0; 6 | int mask; 7 | for (int num : nums) { 8 | x2 ^= x1 & num; 9 | x1 ^= num; 10 | mask = ~(x1 & x2); 11 | x2 &= mask; 12 | x1 &= mask; 13 | } 14 | return x1; 15 | } 16 | }; -------------------------------------------------------------------------------- /137. Single Number II/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int singleNumber(int[] nums) { 3 | int x1 = 0; 4 | int x2 = 0; 5 | int mask; 6 | for (int num : nums) { 7 | x2 ^= x1 & num; 8 | x1 ^= num; 9 | mask = ~(x1 & x2); 10 | x2 &= mask; 11 | x1 &= mask; 12 | } 13 | return x1; 14 | } 15 | } -------------------------------------------------------------------------------- /137. Single Number II/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def singleNumber(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | x1 = 0 8 | x2 = 0 9 | for num in nums: 10 | x2 ^= x1 & num 11 | x1 ^= num 12 | mask = ~(x1 & x2) 13 | x2 &= mask 14 | x1 &= mask 15 | return x1 16 | -------------------------------------------------------------------------------- /139. Word Break/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean wordBreak(String s, List wordDict) { 3 | boolean[] dp = new boolean[s.length() + 1]; 4 | dp[0] = true; 5 | for (int i = 1; i <= s.length(); i++) { 6 | for (int j = i - 1; j >= 0; j--) { 7 | if (dp[j]) { 8 | if (wordDict.contains(s.substring(j, i))) { 9 | dp[i] = true; 10 | break; 11 | } 12 | } 13 | } 14 | } 15 | return dp[s.length()]; 16 | } 17 | } -------------------------------------------------------------------------------- /139. Word Break/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def wordBreak(self, s, wordDict): 3 | """ 4 | :type s: str 5 | :type wordDict: List[str] 6 | :rtype: bool 7 | """ 8 | dp = [False] * (len(s) + 1) 9 | dp[0] = True 10 | for i in range(1, len(s) + 1): 11 | for j in range(i - 1, -1, -1): 12 | if dp[j]: 13 | if s[j:i] in wordDict: 14 | dp[i] = True 15 | break 16 | return dp[-1] 17 | -------------------------------------------------------------------------------- /153. Find Minimum in Rotated Sorted Array/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector &nums) { 4 | int low = 0; 5 | int high = nums.size() - 1; 6 | while (low < high) { 7 | if (nums[low] < nums[high]) { 8 | return nums[low]; 9 | } 10 | int mid = (low + high) / 2; 11 | if (nums[mid] >= nums[low]) { 12 | low = mid + 1; 13 | } else { 14 | high = mid; 15 | } 16 | } 17 | return nums[low]; 18 | } 19 | }; -------------------------------------------------------------------------------- /153. Find Minimum in Rotated Sorted Array/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findMin(int[] nums) { 3 | int low = 0; 4 | int high = nums.length - 1; 5 | while (low < high) { 6 | if (nums[low] < nums[high]) { 7 | return nums[low]; 8 | } 9 | int mid = (low + high) / 2; 10 | if (nums[mid] >= nums[low]) { 11 | low = mid + 1; 12 | } else { 13 | high = mid; 14 | } 15 | } 16 | return nums[low]; 17 | } 18 | } -------------------------------------------------------------------------------- /153. Find Minimum in Rotated Sorted Array/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findMin(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | length = len(nums) 8 | low = 0 9 | high = length - 1 10 | while low < high: 11 | mid = (low + high) // 2 12 | if nums[mid] > nums[high]: 13 | low = mid + 1 14 | else: 15 | high = mid 16 | return nums[low] 17 | -------------------------------------------------------------------------------- /154. Find Minimum in Rotated Sorted Array II/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector &nums) { 4 | int low = 0; 5 | int high = nums.size() - 1; 6 | while (low < high) { 7 | int mid = (low + high) / 2; 8 | if (nums[mid] > nums[high]) { 9 | low = mid + 1; 10 | } else if (nums[mid] < nums[high]) { 11 | high = mid; 12 | } else { 13 | high--; 14 | } 15 | } 16 | return nums[low]; 17 | } 18 | }; -------------------------------------------------------------------------------- /154. Find Minimum in Rotated Sorted Array II/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findMin(int[] nums) { 3 | int low = 0; 4 | int high = nums.length - 1; 5 | while (low < high) { 6 | int mid = (low + high) / 2; 7 | if (nums[mid] > nums[high]) { 8 | low = mid + 1; 9 | } else if (nums[mid] < nums[high]) { 10 | high = mid; 11 | } else { 12 | high--; 13 | } 14 | } 15 | return nums[low]; 16 | } 17 | } -------------------------------------------------------------------------------- /154. Find Minimum in Rotated Sorted Array II/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findMin(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | low = 0 8 | high = len(nums) - 1 9 | while low < high: 10 | mid = (low + high) // 2 11 | if nums[mid] > nums[high]: 12 | low = mid + 1 13 | elif nums[mid] < nums[high]: 14 | high = mid 15 | else: 16 | high -= 1 17 | return nums[low] 18 | -------------------------------------------------------------------------------- /162. Find Peak Element/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findPeakElement(int[] nums) { 3 | if (nums.length <= 1) { 4 | return 0; 5 | } 6 | if (nums[0] > nums[1]) { 7 | return 0; 8 | } 9 | for (int i = 1; i < nums.length - 1; i++) { 10 | if (nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) { 11 | return i; 12 | } 13 | } 14 | return nums.length - 1; 15 | } 16 | } -------------------------------------------------------------------------------- /162. Find Peak Element/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findPeakElement(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | if len(nums) <= 1: 8 | return 0 9 | if nums[0] > nums[1]: 10 | return 0 11 | for i in range(len(nums) - 1): 12 | if nums[i] > nums[i - 1] and nums[i] > nums[i + 1]: 13 | return i 14 | return len(nums) - 1 15 | -------------------------------------------------------------------------------- /167. Two Sum II - Input array is sorted/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector &numbers, int target) { 4 | int i = 0; 5 | int j = numbers.size() - 1; 6 | while (i < j) { 7 | int twoSum = numbers[i] + numbers[j]; 8 | if (twoSum == target) { 9 | break; 10 | } 11 | if (twoSum > target) { 12 | j--; 13 | } else { 14 | i++; 15 | } 16 | } 17 | 18 | return vector {i + 1, j + 1}; 19 | } 20 | }; -------------------------------------------------------------------------------- /167. Two Sum II - Input array is sorted/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] twoSum(int[] numbers, int target) { 3 | int i = 0; 4 | int j = numbers.length - 1; 5 | while (i < j) { 6 | int twoSum = numbers[i] + numbers[j]; 7 | if (twoSum == target) { 8 | break; 9 | } 10 | if (twoSum > target) { 11 | j--; 12 | } else { 13 | i++; 14 | } 15 | } 16 | return new int[] {i + 1, j + 1}; 17 | } 18 | } -------------------------------------------------------------------------------- /167. Two Sum II - Input array is sorted/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def twoSum(self, numbers, target): 3 | """ 4 | :type numbers: List[int] 5 | :type target: int 6 | :rtype: List[int] 7 | """ 8 | i = 0 9 | j = len(numbers) - 1 10 | while i < j: 11 | two_sum = numbers[i] + numbers[j] 12 | if two_sum == target: 13 | break 14 | if two_sum > target: 15 | j -= 1 16 | else: 17 | i += 1 18 | return [i + 1, j + 1] 19 | -------------------------------------------------------------------------------- /168. Excel Sheet Column Title/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convertToTitle(int n) { 4 | int i = 0; 5 | char c; 6 | string s; 7 | while (n) { 8 | n--; 9 | int t; 10 | t = n % 26; 11 | n = n / 26; 12 | c = 'A' + t; 13 | s = c + s; 14 | } 15 | return s; 16 | } 17 | }; -------------------------------------------------------------------------------- /168. Excel Sheet Column Title/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String convertToTitle(int n) { 3 | StringBuilder builder = new StringBuilder(); 4 | while (n != 0) { 5 | n--; 6 | builder.insert(0, (char)('A' + n % 26)); 7 | n /= 26; 8 | } 9 | return builder.toString(); 10 | } 11 | } -------------------------------------------------------------------------------- /168. Excel Sheet Column Title/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def convertToTitle(self, n): 3 | """ 4 | :type n: int 5 | :rtype: str 6 | """ 7 | result = '' 8 | while n: 9 | n -= 1 10 | result = chr(ord('A') + n % 26) + result 11 | n //= 26 12 | return result 13 | -------------------------------------------------------------------------------- /169. Majority Element/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector &nums) { 4 | int majority = 0; 5 | int count = 0; 6 | for (int &num : nums) { 7 | if (count == 0) { 8 | majority = num; 9 | } 10 | if (majority == num) { 11 | count++; 12 | } else { 13 | count--; 14 | } 15 | } 16 | return majority; 17 | } 18 | }; -------------------------------------------------------------------------------- /169. Majority Element/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int majorityElement(int[] nums) { 3 | int majority = 0; 4 | int count = 0; 5 | for (int num : nums) { 6 | if (count == 0) { 7 | majority = num; 8 | } 9 | if (majority == num) { 10 | count++; 11 | } else { 12 | count--; 13 | } 14 | } 15 | return majority; 16 | } 17 | } -------------------------------------------------------------------------------- /169. Majority Element/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def majorityElement(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | majority = None 8 | count = 0 9 | for num in nums: 10 | if count == 0: 11 | majority = num 12 | if majority == num: 13 | count += 1 14 | else: 15 | count -= 1 16 | return majority 17 | -------------------------------------------------------------------------------- /171. Excel Sheet Column Number/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int titleToNumber(string s) { 4 | int result = 0; 5 | for (auto letter : s) { 6 | result = result * 26 + letter - 'A' + 1; 7 | } 8 | return result; 9 | } 10 | }; -------------------------------------------------------------------------------- /171. Excel Sheet Column Number/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int titleToNumber(String s) { 3 | int result = 0; 4 | for (char letter : s.toCharArray()) { 5 | result = result * 26 + letter - 'A' + 1; 6 | } 7 | return result; 8 | } 9 | } -------------------------------------------------------------------------------- /171. Excel Sheet Column Number/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def titleToNumber(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | result = 0 8 | for letter in s: 9 | result = result * 26 + ord(letter) - ord('A') + 1 10 | return result 11 | -------------------------------------------------------------------------------- /172. Factorial Trailing Zeroes/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trailingZeroes(int n) { 4 | int result = 0; 5 | long i = 5; 6 | while (i <= n) { 7 | result += n / i; 8 | i *= 5; 9 | } 10 | return result; 11 | } 12 | }; -------------------------------------------------------------------------------- /172. Factorial Trailing Zeroes/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int trailingZeroes(int n) { 3 | int result = 0; 4 | long i = 5; 5 | while (i <= n) { 6 | result += n / i; 7 | i *= 5; 8 | } 9 | return result; 10 | } 11 | } -------------------------------------------------------------------------------- /172. Factorial Trailing Zeroes/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def trailingZeroes(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | result = 0 8 | i = 5 9 | while i <= n: 10 | result += n // i 11 | i *= 5 12 | return result 13 | -------------------------------------------------------------------------------- /174. Dungeon Game/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int calculateMinimumHP(vector> &dungeon) { 4 | int m = dungeon.size(); 5 | int n = dungeon[0].size(); 6 | vector> dp(m + 1, vector(n + 1, INT_MAX)); 7 | dp[m][n - 1] = 1; 8 | dp[m - 1][n] = 1; 9 | for (int i = m - 1; i >= 0; i--) { 10 | for (int j = n - 1; j >= 0; j--) { 11 | dp[i][j] = max(1, min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]); 12 | } 13 | } 14 | return dp[0][0]; 15 | } 16 | }; -------------------------------------------------------------------------------- /174. Dungeon Game/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def calculateMinimumHP(self, dungeon): 3 | """ 4 | :type dungeon: List[List[int]] 5 | :rtype: int 6 | """ 7 | m = len(dungeon) 8 | n = len(dungeon[0]) 9 | dp = [[sys.maxsize] * (n + 1) for i in range(m + 1)] 10 | dp[m][n - 1] = 1 11 | dp[m - 1][n] = 1 12 | for i in range(m - 1, -1, -1): 13 | for j in range(n - 1, -1, -1): 14 | dp[i][j] = max(1, min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]) 15 | return dp[0][0] 16 | -------------------------------------------------------------------------------- /179. Largest Number/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @param {integer[]} nums 3 | # @return {string} 4 | def largestNumber(self, nums): 5 | strs = [str(num) for num in nums] 6 | strs.sort(lambda x, y: 1 if x + y < y + x else -1 if x + y > y + x else 0) 7 | if strs[0] == '0': 8 | return '0' 9 | return ''.join(strs) 10 | -------------------------------------------------------------------------------- /189. Rotate Array/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rev(int a[], int s, int e) { 4 | while (s < e) { 5 | int t = a[s]; 6 | a[s] = a[e]; 7 | a[e] = t; 8 | s++; 9 | e--; 10 | } 11 | } 12 | void rotate(int nums[], int n, int k) { 13 | if (k > 0) { 14 | if (k > n) 15 | k %= n; 16 | rev(nums, 0, n - 1); 17 | rev(nums, 0, k - 1); 18 | rev(nums, k, n - 1); 19 | } 20 | } 21 | }; -------------------------------------------------------------------------------- /189. Rotate Array/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def rotate(self, nums, k): 3 | """ 4 | :type nums: List[int] 5 | :type k: int 6 | :rtype: void Do not return anything, modify nums in-place instead. 7 | """ 8 | nums[:] = nums[len(nums) - k:] + nums[:len(nums) - k] 9 | -------------------------------------------------------------------------------- /190. Reverse Bits/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | uint32_t reverseBits(uint32_t n) { 4 | int result = 0; 5 | for (int i = 0; i < 32; i++) { 6 | result <<= 1; 7 | result |= n & 1; 8 | n >>= 1; 9 | } 10 | return result; 11 | } 12 | }; -------------------------------------------------------------------------------- /190. Reverse Bits/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need treat n as an unsigned value 3 | public int reverseBits(int n) { 4 | int result = 0; 5 | for (int i = 0; i < 32; i++) { 6 | result <<= 1; 7 | result |= n & 1; 8 | n >>= 1; 9 | } 10 | return result; 11 | } 12 | } -------------------------------------------------------------------------------- /190. Reverse Bits/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @param n, an integer 3 | # @return an integer 4 | def reverseBits(self, n): 5 | result = 0 6 | for i in range(32): 7 | result <<= 1 8 | result |= n & 1 9 | n >>= 1 10 | return result 11 | -------------------------------------------------------------------------------- /191. Number of 1 Bits/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(uint32_t n) { 4 | int result = 0; 5 | while (n != 0) { 6 | n &= n - 1; 7 | result++; 8 | } 9 | return result; 10 | } 11 | }; -------------------------------------------------------------------------------- /191. Number of 1 Bits/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need to treat n as an unsigned value 3 | public int hammingWeight(int n) { 4 | int result = 0; 5 | while (n != 0) { 6 | n &= n - 1; 7 | result++; 8 | } 9 | return result; 10 | } 11 | } -------------------------------------------------------------------------------- /191. Number of 1 Bits/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def hammingWeight(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | result = 0 8 | while n: 9 | n &= n - 1 10 | result += 1 11 | return result 12 | -------------------------------------------------------------------------------- /198. House Robber/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rob(vector &nums) { 4 | if (nums.size() == 0) { 5 | return 0; 6 | } 7 | int results[nums.size() + 1] = {0}; 8 | results[0] = 0; 9 | results[1] = nums[0]; 10 | for (int i = 2; i <= nums.size(); i++) { 11 | results[i] = max(nums[i - 1] + results[i - 2], results[i - 1]); 12 | } 13 | return results[nums.size()]; 14 | } 15 | }; -------------------------------------------------------------------------------- /198. House Robber/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int rob(int[] nums) { 3 | if (nums.length == 0) { 4 | return 0; 5 | } 6 | int[] results = new int[nums.length + 1]; 7 | results[0] = 0; 8 | results[1] = nums[0]; 9 | for (int i = 2; i <= nums.length; i++) { 10 | results[i] = Math.max(nums[i - 1] + results[i - 2], results[i - 1]); 11 | } 12 | return results[nums.length]; 13 | } 14 | } -------------------------------------------------------------------------------- /198. House Robber/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def rob(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | if not nums: 8 | return 0 9 | results = [0] * (len(nums) + 1) 10 | results[0] = 0 11 | results[1] = nums[0] 12 | for i in range(2, len(nums) + 1): 13 | results[i] = max(nums[i - 1] + results[i - 2], results[i - 1]) 14 | return results[-1] 15 | -------------------------------------------------------------------------------- /201. Bitwise AND of Numbers Range/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rangeBitwiseAnd(int m, int n) { 4 | int theXor = m ^ n; 5 | int diffIndex = 31; 6 | while (diffIndex >= 0 && (theXor & 1 << diffIndex) == 0) { 7 | diffIndex--; 8 | } 9 | return m & 0xffffffff << (diffIndex + 1); 10 | } 11 | }; -------------------------------------------------------------------------------- /201. Bitwise AND of Numbers Range/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int rangeBitwiseAnd(int m, int n) { 3 | int theXor = m ^ n; 4 | int diffIndex = 31; 5 | while (diffIndex >= 0 && (theXor & 1 << diffIndex) == 0) { 6 | diffIndex--; 7 | } 8 | return m & 0xffffffff << (diffIndex + 1); 9 | } 10 | } -------------------------------------------------------------------------------- /201. Bitwise AND of Numbers Range/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def rangeBitwiseAnd(self, m, n): 3 | """ 4 | :type m: int 5 | :type n: int 6 | :rtype: int 7 | """ 8 | the_xor = m ^ n 9 | diff_index = 31 10 | while diff_index >= 0 and not the_xor & 1 << diff_index: 11 | diff_index -= 1 12 | return m & 0xffffffff << (diff_index + 1) 13 | -------------------------------------------------------------------------------- /202. Happy Number/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isHappy(int n) { 4 | set got; 5 | while (n != 1 && got.find(n) == got.end()) { 6 | got.insert(n); 7 | int sum = 0; 8 | while (n) { 9 | sum += pow(n % 10, 2); 10 | n /= 10; 11 | } 12 | n = sum; 13 | } 14 | return n == 1; 15 | } 16 | }; -------------------------------------------------------------------------------- /202. Happy Number/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isHappy(int n) { 3 | Set got = new HashSet<>(); 4 | while (n != 1 && !got.contains(n)) { 5 | got.add(n); 6 | int sum = 0; 7 | while (n != 0) { 8 | sum += Math.pow(n % 10, 2); 9 | n /= 10; 10 | } 11 | n = sum; 12 | } 13 | return n == 1; 14 | } 15 | } -------------------------------------------------------------------------------- /202. Happy Number/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isHappy(self, n): 3 | """ 4 | :type n: int 5 | :rtype: bool 6 | """ 7 | got = set() 8 | while n != 1 and n not in got: 9 | got.add(n) 10 | sum = 0 11 | while n: 12 | sum += (n % 10)**2 13 | n //= 10 14 | n = sum 15 | 16 | return n == 1 17 | -------------------------------------------------------------------------------- /204. Count Primes/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countPrimes(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | if n < 2: 8 | return 0 9 | primes = [True] * n 10 | primes[0] = False 11 | primes[1] = False 12 | for i in range(2, int(n ** 0.5) + 1): 13 | if primes[i]: 14 | for j in range(i * i, n, i): 15 | primes[j] = False 16 | return sum(primes) 17 | -------------------------------------------------------------------------------- /205. Isomorphic Strings/Java/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isIsomorphic(String s, String t) { 3 | int[] m1 = new int[256]; 4 | int[] m2 = new int[256]; 5 | for (int i = 0; i < s.length(); i++) { 6 | char k1 = s.charAt(i); 7 | char k2 = t.charAt(i); 8 | if (m1[k1] != m2[k2]) { 9 | return false; 10 | } 11 | m1[k1] = i + 1; 12 | m2[k2] = i + 1; 13 | } 14 | return true; 15 | } 16 | } -------------------------------------------------------------------------------- /214. Shortest Palindrome/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string shortestPalindrome(string s) { 4 | string rev(s); 5 | reverse(rev.rbegin(), rev.rend()); 6 | int length = s.length(); 7 | for (int i = length; i >= 0; i--) { 8 | if (s.substr(0, i) == rev.substr(length - i)) { 9 | return rev.substr(0, length - i) + s; 10 | } 11 | } 12 | return ""; 13 | } 14 | }; -------------------------------------------------------------------------------- /214. Shortest Palindrome/Java/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String shortestPalindrome(String s) { 3 | String reverse = new StringBuilder(s).reverse().toString(); 4 | for (int i = 0; i <= s.length(); i++) { 5 | if (s.startsWith(reverse.substring(i))) { 6 | return reverse.substring(0, i) + s; 7 | } 8 | } 9 | return null; 10 | } 11 | } -------------------------------------------------------------------------------- /214. Shortest Palindrome/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def shortestPalindrome(self, s): 3 | """ 4 | :type s: str 5 | :rtype: str 6 | """ 7 | reverse = s[::-1] 8 | for i in range(len(s) + 1): 9 | if s.startswith(reverse[i:]): 10 | return reverse[:i] + s 11 | -------------------------------------------------------------------------------- /217. Contains Duplicate/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsDuplicate(vector &nums) { 4 | set contains; 5 | for (int num : nums) { 6 | if (contains.find(num) != contains.end()) { 7 | return true; 8 | } 9 | contains.insert(num); 10 | } 11 | return false; 12 | } 13 | }; -------------------------------------------------------------------------------- /217. Contains Duplicate/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean containsDuplicate(int[] nums) { 3 | Set set = new HashSet<>(); 4 | for (int num : nums) { 5 | if (set.contains(num)) { 6 | return true; 7 | } 8 | set.add(num); 9 | } 10 | return false; 11 | } 12 | } -------------------------------------------------------------------------------- /217. Contains Duplicate/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def containsDuplicate(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: bool 6 | """ 7 | contains = set() 8 | for num in nums: 9 | if num in contains: 10 | return True 11 | contains.add(num) 12 | return False 13 | -------------------------------------------------------------------------------- /219. Contains Duplicate II/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsNearbyDuplicate(vector &nums, int k) { 4 | unordered_set window; 5 | for (int i = 0; i < nums.size(); i++) { 6 | if (i > k) { 7 | window.erase(nums[i - k - 1]); 8 | } 9 | if (window.find(nums[i]) != window.end()) { 10 | return true; 11 | } 12 | window.insert(nums[i]); 13 | } 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /219. Contains Duplicate II/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean containsNearbyDuplicate(int[] nums, int k) { 3 | Set window = new HashSet<>(); 4 | for (int i = 0; i < nums.length; i++) { 5 | if (i > k) { 6 | window.remove(nums[i - k - 1]); 7 | } 8 | if (window.contains(nums[i])) { 9 | return true; 10 | } 11 | window.add(nums[i]); 12 | } 13 | return false; 14 | } 15 | } -------------------------------------------------------------------------------- /219. Contains Duplicate II/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def containsNearbyDuplicate(self, nums, k): 3 | """ 4 | :type nums: List[int] 5 | :type k: int 6 | :rtype: bool 7 | """ 8 | window = set() 9 | for i in range(0, len(nums)): 10 | if i > k: 11 | window.remove(nums[i - k - 1]) 12 | if nums[i] in window: 13 | return True 14 | window.add(nums[i]) 15 | return False 16 | -------------------------------------------------------------------------------- /223. Rectangle Area/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 4 | int theSum = (C - A) * (D - B) + (G - E) * (H - F); 5 | int left = max(A, E); 6 | int right = min(C, G); 7 | int up = min(D, H); 8 | int down = max(B, F); 9 | if (right > left && up > down) { 10 | int overlap = (right - left) * (up - down); 11 | return theSum - overlap; 12 | } 13 | return theSum; 14 | } 15 | }; -------------------------------------------------------------------------------- /223. Rectangle Area/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 3 | int theSum = (C - A) * (D - B) + (G - E) * (H - F); 4 | int left = Math.max(A, E); 5 | int right = Math.min(C, G); 6 | int up = Math.min(D, H); 7 | int down = Math.max(B, F); 8 | if (right > left && up > down) { 9 | int overlap = (right - left) * (up - down); 10 | return theSum - overlap; 11 | } 12 | return theSum; 13 | } 14 | } -------------------------------------------------------------------------------- /226. Invert Binary Tree/Python/Solution.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | 8 | 9 | class Solution: 10 | def invertTree(self, root): 11 | """ 12 | :type root: TreeNode 13 | :rtype: TreeNode 14 | """ 15 | if root is None: 16 | return 17 | self.invertTree(root.left) 18 | self.invertTree(root.right) 19 | root.left, root.right = root.right, root.left 20 | return root 21 | -------------------------------------------------------------------------------- /231. Power of Two/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfTwo(int n) { 4 | return n > 0 && 0x80000000 % n == 0; 5 | } 6 | }; -------------------------------------------------------------------------------- /231. Power of Two/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPowerOfTwo(int n) { 3 | return n > 0 && 0x80000000 % n == 0; 4 | } 5 | } -------------------------------------------------------------------------------- /231. Power of Two/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isPowerOfTwo(self, n): 3 | """ 4 | :type n: int 5 | :rtype: bool 6 | """ 7 | return n > 0 and 0x80000000 % n == 0 8 | -------------------------------------------------------------------------------- /233. Number of Digit One/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countDigitOne(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | result = 0 8 | times = 1 9 | while times <= n: 10 | a = n // times 11 | b = n % times 12 | if a % 10 == 0: 13 | result += a // 10 * times 14 | elif a % 10 == 1: 15 | result += a // 10 * times + b + 1 16 | else: 17 | result += (a // 10 + 1) * times 18 | times *= 10 19 | return result 20 | -------------------------------------------------------------------------------- /237. Delete Node in a Linked List/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | void deleteNode(ListNode *node) { 12 | node->val = node->next->val; 13 | node->next = node->next->next; 14 | } 15 | }; -------------------------------------------------------------------------------- /237. Delete Node in a Linked List/Java/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { val = x; } 7 | * } 8 | */ 9 | public class Solution { 10 | public void deleteNode(ListNode node) { 11 | node.val = node.next.val; 12 | node.next = node.next.next; 13 | } 14 | } -------------------------------------------------------------------------------- /237. Delete Node in a Linked List/Python/Solution.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode(object): 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | 7 | 8 | class Solution(object): 9 | def deleteNode(self, node): 10 | """ 11 | :type node: ListNode 12 | :rtype: void Do not return anything, modify node in-place instead. 13 | """ 14 | node.val = node.next.val 15 | node.next = node.next.next 16 | -------------------------------------------------------------------------------- /238. Product of Array Except Self/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector productExceptSelf(vector &nums) { 4 | vector result(nums.size()); 5 | result[0] = 1; 6 | for (int i = 1; i < nums.size(); i++) { 7 | result[i] = result[i - 1] * nums[i - 1]; 8 | } 9 | int temp = 1; 10 | for (int i = nums.size() - 1; i >= 0; i--) { 11 | result[i] *= temp; 12 | temp *= nums[i]; 13 | } 14 | return result; 15 | } 16 | }; -------------------------------------------------------------------------------- /238. Product of Array Except Self/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] productExceptSelf(int[] nums) { 3 | int[] result = new int[nums.length]; 4 | result[0] = 1; 5 | for (int i = 1; i < nums.length; i++) { 6 | result[i] = result[i - 1] * nums[i - 1]; 7 | } 8 | int temp = 1; 9 | for (int i = nums.length - 1; i >= 0; i--) { 10 | result[i] *= temp; 11 | temp *= nums[i]; 12 | } 13 | return result; 14 | } 15 | } -------------------------------------------------------------------------------- /238. Product of Array Except Self/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def productExceptSelf(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: List[int] 6 | """ 7 | result = [None] * len(nums) 8 | result[0] = 1 9 | for i in range(1, len(nums)): 10 | result[i] = result[i - 1] * nums[i - 1] 11 | temp = 1 12 | for i in range(len(nums) - 1, -1, -1): 13 | result[i] *= temp 14 | temp *= nums[i] 15 | return result 16 | -------------------------------------------------------------------------------- /242. Valid Anagram/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | if (s.length() != t.length()) { 5 | return false; 6 | } 7 | vector counts(26, 0); 8 | for (auto item : s) { 9 | counts[item - 'a']++; 10 | } 11 | for (char item : t) { 12 | counts[item - 'a']--; 13 | } 14 | for (auto count : counts) { 15 | if (count != 0) { 16 | return false; 17 | } 18 | } 19 | return true; 20 | } 21 | }; -------------------------------------------------------------------------------- /242. Valid Anagram/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isAnagram(self, s, t): 3 | """ 4 | :type s: str 5 | :type t: str 6 | :rtype: bool 7 | """ 8 | if len(s) != len(t): 9 | return False 10 | counts = [0] * 26 11 | for item in s: 12 | counts[ord(item) - ord('a')] += 1 13 | for item in t: 14 | counts[ord(item) - ord('a')] -= 1 15 | return all(count == 0 for count in counts) 16 | -------------------------------------------------------------------------------- /258. Add Digits/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int addDigits(int num) { 4 | return 1 + (num - 1) % 9; 5 | } 6 | }; -------------------------------------------------------------------------------- /258. Add Digits/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int addDigits(int num) { 3 | return 1 + (num - 1) % 9; 4 | } 5 | } -------------------------------------------------------------------------------- /258. Add Digits/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def addDigits(self, num): 3 | """ 4 | :type num: int 5 | :rtype: int 6 | """ 7 | if num == 0: 8 | return 0 9 | return 1 + (num - 1) % 9 10 | -------------------------------------------------------------------------------- /260. Single Number III/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector singleNumber(vector &nums) { 4 | int mixedXor = 0; 5 | for (auto num : nums) { 6 | mixedXor ^= num; 7 | } 8 | int diff = mixedXor & (mixedXor - 1) ^mixedXor; 9 | int first = 0; 10 | int second = 0; 11 | for (auto num : nums) { 12 | if (diff & num) { 13 | first ^= num; 14 | } else { 15 | second ^= num; 16 | } 17 | } 18 | return vector {first, second}; 19 | } 20 | }; -------------------------------------------------------------------------------- /260. Single Number III/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] singleNumber(int[] nums) { 3 | int mixedXor = 0; 4 | for (int num : nums) { 5 | mixedXor ^= num; 6 | } 7 | int diff = mixedXor & (mixedXor - 1) ^ mixedXor; 8 | int first = 0; 9 | int second = 0; 10 | for (int num : nums) { 11 | if ((diff & num) == 0) { 12 | first ^= num; 13 | } else { 14 | second ^= num; 15 | } 16 | } 17 | return new int[] {first, second}; 18 | } 19 | } -------------------------------------------------------------------------------- /260. Single Number III/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def singleNumber(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: List[int] 6 | """ 7 | mixed_xor = 0 8 | for num in nums: 9 | mixed_xor ^= num 10 | diff = mixed_xor & (mixed_xor - 1) ^ mixed_xor 11 | first = 0 12 | second = 0 13 | for num in nums: 14 | if diff & num: 15 | first ^= num 16 | else: 17 | second ^= num 18 | return [first, second] 19 | -------------------------------------------------------------------------------- /263. Ugly Number/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isUgly(int num) { 4 | if (num == 0) { 5 | return false; 6 | } 7 | while (num % 2 == 0) { 8 | num /= 2; 9 | } 10 | while (num % 3 == 0) { 11 | num /= 3; 12 | } 13 | while (num % 5 == 0) { 14 | num /= 5; 15 | } 16 | return num == 1; 17 | } 18 | }; -------------------------------------------------------------------------------- /263. Ugly Number/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isUgly(int num) { 3 | if (num == 0) { 4 | return false; 5 | } 6 | while (num % 2 == 0) { 7 | num /= 2; 8 | } 9 | while (num % 3 == 0) { 10 | num /= 3; 11 | } 12 | while (num % 5 == 0) { 13 | num /= 5; 14 | } 15 | return num == 1; 16 | } 17 | } -------------------------------------------------------------------------------- /263. Ugly Number/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isUgly(self, num): 3 | """ 4 | :type num: int 5 | :rtype: bool 6 | """ 7 | while num % 2 == 0 and num: 8 | num //= 2 9 | while num % 3 == 0 and num: 10 | num //= 3 11 | while num % 5 == 0 and num: 12 | num //= 5 13 | return num == 1 14 | -------------------------------------------------------------------------------- /268. Missing Number/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int missingNumber(vector &nums) { 4 | int result = 0; 5 | for (int i = 0; i < nums.size(); i++) { 6 | result ^= i; 7 | result ^= nums[i]; 8 | } 9 | result ^= nums.size(); 10 | return result; 11 | } 12 | }; -------------------------------------------------------------------------------- /268. Missing Number/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int missingNumber(int[] nums) { 3 | int result = 0; 4 | for (int i = 0; i < nums.length; i++) { 5 | result ^= i; 6 | result ^= nums[i]; 7 | } 8 | result ^= nums.length; 9 | return result; 10 | } 11 | } -------------------------------------------------------------------------------- /268. Missing Number/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def missingNumber(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | result = 0 8 | for i in range(0, len(nums)): 9 | result ^= i 10 | result ^= nums[i] 11 | result ^= len(nums) 12 | return result 13 | -------------------------------------------------------------------------------- /278. First Bad Version/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | // Forward declaration of isBadVersion API. 2 | bool isBadVersion(int version); 3 | 4 | class Solution { 5 | public: 6 | int firstBadVersion(int n) { 7 | long low = 0; 8 | int high = n; 9 | while (low <= high) { 10 | int mid = (low + high) / 2; 11 | if (isBadVersion(mid)) { 12 | high = mid - 1; 13 | } else { 14 | low = mid + 1; 15 | } 16 | } 17 | if (high == n) { 18 | return high; 19 | } 20 | return high + 1; 21 | } 22 | }; -------------------------------------------------------------------------------- /279. Perfect Squares/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSquares(int n) { 4 | vector dp(n + 1, n); 5 | dp[0] = 0; 6 | for (int i = 0; i <= n; i++) { 7 | int j = 1; 8 | while (j * j <= i) { 9 | dp[i] = min(dp[i], dp[i - j * j] + 1); 10 | j++; 11 | } 12 | } 13 | return dp[n]; 14 | } 15 | }; -------------------------------------------------------------------------------- /279. Perfect Squares/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int numSquares(int n) { 3 | int[] dp = new int[n + 1]; 4 | Arrays.fill(dp, n); 5 | dp[0] = 0; 6 | for (int i = 0; i <= n; i++) { 7 | int j = 1; 8 | while (j * j <= i) { 9 | dp[i] = Math.min(dp[i], dp[i - j * j] + 1); 10 | j++; 11 | } 12 | } 13 | return dp[n]; 14 | } 15 | } -------------------------------------------------------------------------------- /279. Perfect Squares/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def numSquares(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | dp = [n] * (n + 1) 8 | dp[0] = 0 9 | for i in range(1, n + 1): 10 | j = 1 11 | while j * j <= i: 12 | dp[i] = min(dp[i], dp[i - j * j] + 1) 13 | j += 1 14 | return dp[n] 15 | -------------------------------------------------------------------------------- /283. Move Zeroes/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void moveZeroes(vector &nums) { 4 | int i = 0; 5 | for (auto num : nums) { 6 | if (num != 0) { 7 | nums[i] = num; 8 | i++; 9 | } 10 | } 11 | while (i < nums.size()) { 12 | nums[i] = 0; 13 | i++; 14 | } 15 | } 16 | }; -------------------------------------------------------------------------------- /283. Move Zeroes/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public void moveZeroes(int[] nums) { 3 | int i = 0; 4 | for (int num : nums) { 5 | if (num != 0) { 6 | nums[i] = num; 7 | i++; 8 | } 9 | } 10 | while (i < nums.length) { 11 | nums[i] = 0; 12 | i++; 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /283. Move Zeroes/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def moveZeroes(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: void Do not return anything, modify nums in-place instead. 6 | """ 7 | i = 0 8 | for num in nums: 9 | if num != 0: 10 | nums[i] = num 11 | i += 1 12 | while i < len(nums): 13 | nums[i] = 0 14 | i += 1 15 | -------------------------------------------------------------------------------- /287. Find the Duplicate Number/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findDuplicate(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | low = 1 8 | high = len(nums) - 1 9 | while low < high: 10 | mid = (low + high) // 2 11 | count = 0 12 | for num in nums: 13 | if num <= mid: 14 | count += 1 15 | if count <= mid: 16 | low = mid + 1 17 | else: 18 | high = mid 19 | return low 20 | -------------------------------------------------------------------------------- /292. Nim Game/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canWinNim(int n) { 4 | return n % 4 != 0; 5 | } 6 | }; -------------------------------------------------------------------------------- /292. Nim Game/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean canWinNim(int n) { 3 | return n % 4 != 0; 4 | } 5 | } -------------------------------------------------------------------------------- /292. Nim Game/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def canWinNim(self, n): 3 | """ 4 | :type n: int 5 | :rtype: bool 6 | """ 7 | return n % 4 != 0 8 | -------------------------------------------------------------------------------- /300. Longest Increasing Subsequence/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLIS(vector &nums) { 4 | if (nums.size() == 0) { 5 | return 0; 6 | } 7 | vector dp(nums.size(), 1); 8 | for (int i = 1; i < nums.size(); i++) { 9 | for (int j = 0; j < i; j++) { 10 | if (nums[i] > nums[j]) { 11 | dp[i] = max(dp[i], dp[j] + 1); 12 | } 13 | } 14 | } 15 | return *max_element(dp.begin(), dp.end()); 16 | } 17 | }; -------------------------------------------------------------------------------- /300. Longest Increasing Subsequence/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def lengthOfLIS(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | if not nums: 8 | return 0 9 | dp = [1] * len(nums) 10 | for i in range(1, len(nums)): 11 | for j in range(i): 12 | if nums[i] > nums[j]: 13 | dp[i] = max(dp[i], dp[j] + 1) 14 | 15 | return max(dp) 16 | -------------------------------------------------------------------------------- /319. Bulb Switcher/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bulbSwitch(int n) { 4 | return (int) sqrt(n); 5 | } 6 | }; -------------------------------------------------------------------------------- /319. Bulb Switcher/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int bulbSwitch(int n) { 3 | return (int) Math.sqrt(n); 4 | } 5 | } -------------------------------------------------------------------------------- /319. Bulb Switcher/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def bulbSwitch(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | return int(n ** 0.5) 8 | -------------------------------------------------------------------------------- /322. Coin Change/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int coinChange(vector &coins, int amount) { 4 | vector dp(amount + 1, amount + 1); 5 | dp[0] = 0; 6 | for (int i = 1; i <= amount; i++) { 7 | for (auto &coin : coins) { 8 | if (coin <= i) { 9 | dp[i] = min(dp[i], dp[i - coin] + 1); 10 | } 11 | } 12 | } 13 | return dp[amount] > amount ? -1 : dp[amount]; 14 | } 15 | }; -------------------------------------------------------------------------------- /322. Coin Change/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int coinChange(int[] coins, int amount) { 3 | int[] dp = new int[amount + 1]; 4 | Arrays.fill(dp, amount + 1); 5 | dp[0] = 0; 6 | for (int i = 1; i <= amount; i++) { 7 | for (int coin : coins) { 8 | if (coin <= i) { 9 | dp[i] = Math.min(dp[i], dp[i - coin] + 1); 10 | } 11 | } 12 | } 13 | return dp[amount] > amount ? -1 : dp[amount]; 14 | } 15 | } -------------------------------------------------------------------------------- /322. Coin Change/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def coinChange(self, coins, amount): 3 | """ 4 | :type coins: List[int] 5 | :type amount: int 6 | :rtype: int 7 | """ 8 | dp = [sys.maxsize] * (amount + 1) 9 | dp[0] = 0 10 | for i in range(1, amount + 1): 11 | for coin in coins: 12 | if coin <= i: 13 | dp[i] = min(dp[i], dp[i - coin] + 1) 14 | return -1 if dp[amount] > amount else dp[amount] 15 | -------------------------------------------------------------------------------- /326. Power of Three/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfThree(int n) { 4 | return n > 0 && ((int) pow((double) 3, 19)) % n == 0; 5 | } 6 | }; -------------------------------------------------------------------------------- /326. Power of Three/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPowerOfThree(int n) { 3 | return n > 0 && Math.pow(3, 19) % n == 0; 4 | } 5 | } -------------------------------------------------------------------------------- /326. Power of Three/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isPowerOfThree(self, n): 3 | """ 4 | :type n: int 5 | :rtype: bool 6 | """ 7 | if n <= 0: 8 | return False 9 | return (3 ** 19) % n == 0 10 | -------------------------------------------------------------------------------- /330. Patching Array/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def minPatches(self, nums, n): 3 | """ 4 | :type nums: List[int] 5 | :type n: int 6 | :rtype: int 7 | """ 8 | hope = 1 9 | result = 0 10 | index = 0 11 | while hope <= n: 12 | if index < len(nums) and nums[index] <= hope: 13 | hope += nums[index] 14 | index += 1 15 | else: 16 | result += 1 17 | hope += hope 18 | return result 19 | -------------------------------------------------------------------------------- /331. Verify Preorder Serialization of a Binary Tree/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValidSerialization(string preorder) { 4 | int degree = -1; 5 | string node; 6 | stringstream ss(preorder); 7 | while (getline(ss, node, ',')) { 8 | degree++; 9 | if (degree > 0) { 10 | return false; 11 | } 12 | if (node != "#") { 13 | degree -= 2; 14 | } 15 | } 16 | return degree == 0; 17 | } 18 | }; -------------------------------------------------------------------------------- /331. Verify Preorder Serialization of a Binary Tree/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isValidSerialization(String preorder) { 3 | int degree = -1; 4 | String[] nodes = preorder.split(","); 5 | for (String node : nodes) { 6 | degree++; 7 | if (degree > 0) { 8 | return false; 9 | } 10 | if (!node.equals("#")) { 11 | degree -= 2; 12 | } 13 | } 14 | return degree == 0; 15 | } 16 | } -------------------------------------------------------------------------------- /331. Verify Preorder Serialization of a Binary Tree/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isValidSerialization(self, preorder): 3 | """ 4 | :type preorder: str 5 | :rtype: bool 6 | """ 7 | degree = -1 8 | for node in preorder.split(','): 9 | degree += 1 10 | if degree > 0: 11 | return False 12 | if node != '#': 13 | degree -= 2 14 | return degree == 0 15 | -------------------------------------------------------------------------------- /334. Increasing Triplet Subsequence/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool increasingTriplet(vector &nums) { 4 | int first = INT_MAX; 5 | int second = INT_MAX; 6 | for (auto &num : nums) { 7 | if (num < first) { 8 | first = num; 9 | } else if (num < second && num > first) { 10 | second = num; 11 | } else if (num > second) { 12 | return true; 13 | } 14 | } 15 | return false; 16 | } 17 | }; -------------------------------------------------------------------------------- /334. Increasing Triplet Subsequence/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean increasingTriplet(int[] nums) { 3 | int first = Integer.MAX_VALUE; 4 | int second = Integer.MAX_VALUE; 5 | for (int num : nums) { 6 | if (num < first) { 7 | first = num; 8 | } else if (num < second && num > first) { 9 | second = num; 10 | } else if (num > second) { 11 | return true; 12 | } 13 | } 14 | return false; 15 | } 16 | } -------------------------------------------------------------------------------- /334. Increasing Triplet Subsequence/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def increasingTriplet(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: bool 6 | """ 7 | first = sys.maxsize 8 | second = sys.maxsize 9 | for num in nums: 10 | if num < first: 11 | first = num 12 | elif num < second and num > first: 13 | second = num 14 | elif num > second: 15 | return True 16 | return False 17 | -------------------------------------------------------------------------------- /338. Counting Bits/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int num) { 4 | vector result(num + 1); 5 | result[0] = 0; 6 | for (int i = 0; i < result.size(); i++) { 7 | result[i] = result[i >> 1] + (i & 1); 8 | } 9 | return result; 10 | } 11 | }; -------------------------------------------------------------------------------- /338. Counting Bits/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] countBits(int num) { 3 | int[] result = new int[num + 1]; 4 | for (int i = 0; i < result.length; i++) { 5 | result[i] = result[i >> 1] + (i & 1); 6 | } 7 | return result; 8 | } 9 | } -------------------------------------------------------------------------------- /338. Counting Bits/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countBits(self, num): 3 | """ 4 | :type num: int 5 | :rtype: List[int] 6 | """ 7 | result = [] 8 | result.append(0) 9 | for i in range(1, num + 1): 10 | result.append(result[i >> 1] + (i & 1)) 11 | return result 12 | -------------------------------------------------------------------------------- /342. Power of Four/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfFour(int num) { 4 | return num > 0 && (num & (num - 1)) == 0 && (num & 0x55555555) == num; 5 | } 6 | }; -------------------------------------------------------------------------------- /342. Power of Four/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPowerOfFour(int num) { 3 | return num > 0 && (num & (num - 1)) == 0 && (num & 0x55555555) == num; 4 | } 5 | } -------------------------------------------------------------------------------- /342. Power of Four/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isPowerOfFour(self, num): 3 | """ 4 | :type num: int 5 | :rtype: bool 6 | """ 7 | return num > 0 and num & (num - 1) == 0 and num & 0x55555555 == num 8 | -------------------------------------------------------------------------------- /343. Integer Break/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int integerBreak(int n) { 4 | switch (n) { 5 | case 2: 6 | return 1; 7 | case 3: 8 | return 2; 9 | case 4: 10 | return 4; 11 | default: 12 | int result = 1; 13 | while (n > 4) { 14 | n -= 3; 15 | result *= 3; 16 | } 17 | if (n > 1) { 18 | result *= n; 19 | } 20 | return result; 21 | } 22 | } 23 | }; -------------------------------------------------------------------------------- /343. Integer Break/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def integerBreak(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | if n == 2: 8 | return 1 9 | if n == 3: 10 | return 2 11 | if n == 4: 12 | return 4 13 | result = 1 14 | while n > 4: 15 | n -= 3 16 | result *= 3 17 | if n == 4: 18 | result *= 4 19 | elif n == 3: 20 | result *= 3 21 | elif n == 2: 22 | result *= 2 23 | return result 24 | -------------------------------------------------------------------------------- /344. Reverse String/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseString(string s) { 4 | for (int i = 0, j = s.length() - 1; i < j; i++, j--) { 5 | char temp = s[i]; 6 | s[i] = s[j]; 7 | s[j] = temp; 8 | } 9 | return s; 10 | } 11 | }; -------------------------------------------------------------------------------- /344. Reverse String/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String reverseString(String s) { 3 | char[] charArray = s.toCharArray(); 4 | for (int i = 0, j = charArray.length - 1; i < j; i++, j--) { 5 | char temp = charArray[i]; 6 | charArray[i] = charArray[j]; 7 | charArray[j] = temp; 8 | } 9 | return new String(charArray); 10 | } 11 | } -------------------------------------------------------------------------------- /344. Reverse String/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def reverseString(self, s): 3 | """ 4 | :type s: str 5 | :rtype: str 6 | """ 7 | return s[::-1] 8 | -------------------------------------------------------------------------------- /349. Intersection of Two Arrays/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersection(vector &nums1, vector &nums2) { 4 | set nums1Set(nums1.begin(), nums1.end()); 5 | set intersect; 6 | for (int num : nums2) { 7 | if (nums1Set.find(num) != nums1Set.end()) { 8 | intersect.insert(num); 9 | } 10 | } 11 | return vector(intersect.begin(), intersect.end()); 12 | } 13 | }; -------------------------------------------------------------------------------- /349. Intersection of Two Arrays/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def intersection(self, nums1, nums2): 3 | """ 4 | :type nums1: List[int] 5 | :type nums2: List[int] 6 | :rtype: List[int] 7 | """ 8 | nums1_set = set(nums1) 9 | intersect = set() 10 | for num in nums2: 11 | if num in nums1_set: 12 | intersect.add(num) 13 | return list(intersect) 14 | -------------------------------------------------------------------------------- /350. Intersection of Two Arrays II/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersect(vector &nums1, vector &nums2) { 4 | map nums1Map; 5 | for (int num : nums1) { 6 | nums1Map[num]++; 7 | } 8 | vector resultVector; 9 | for (int num : nums2) { 10 | if (nums1Map.find(num) != nums1Map.end() && nums1Map[num] != 0) { 11 | resultVector.push_back(num); 12 | nums1Map[num]--; 13 | } 14 | } 15 | return resultVector; 16 | } 17 | }; -------------------------------------------------------------------------------- /350. Intersection of Two Arrays II/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def intersect(self, nums1, nums2): 3 | """ 4 | :type nums1: List[int] 5 | :type nums2: List[int] 6 | :rtype: List[int] 7 | """ 8 | nums1_dict = {} 9 | for num in nums1: 10 | nums1_dict[num] = nums1_dict.get(num, 0) + 1 11 | result = [] 12 | for num in nums2: 13 | if num in nums1_dict and nums1_dict[num] != 0: 14 | result.append(num) 15 | nums1_dict[num] -= 1 16 | return result 17 | -------------------------------------------------------------------------------- /357. Count Numbers with Unique Digits/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countNumbersWithUniqueDigits(int n) { 4 | if (n == 0) { 5 | return 1; 6 | } 7 | vector map(n + 1); 8 | map[1] = 10; 9 | for (int i = 2; i <= n; i++) { 10 | int count = 9; 11 | for (int j = 9 - i + 2; j < 10; j++) { 12 | count *= j; 13 | } 14 | map[i] = map[i - 1] + count; 15 | } 16 | return map[n]; 17 | } 18 | }; -------------------------------------------------------------------------------- /357. Count Numbers with Unique Digits/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int countNumbersWithUniqueDigits(int n) { 3 | if (n == 0) { 4 | return 1; 5 | } 6 | int[] map = new int[n + 1]; 7 | map[1] = 10; 8 | for (int i = 2; i <= n; i++) { 9 | int count = 9; 10 | for (int j = 9 - i + 2; j < 10; j++) { 11 | count *= j; 12 | } 13 | map[i] = map[i - 1] + count; 14 | } 15 | return map[n]; 16 | } 17 | } -------------------------------------------------------------------------------- /357. Count Numbers with Unique Digits/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countNumbersWithUniqueDigits(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | if n == 0: 8 | return 1 9 | map = [None] * (n + 1) 10 | map[1] = 10 11 | for i in range(2, n + 1): 12 | count = 9 13 | for j in range(9 - i + 2, 10): 14 | count *= j 15 | map[i] = map[i - 1] + count 16 | return map[n] 17 | -------------------------------------------------------------------------------- /365. Water and Jug Problem/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def canMeasureWater(self, x, y, z): 3 | """ 4 | :type x: int 5 | :type y: int 6 | :type z: int 7 | :rtype: bool 8 | """ 9 | def gcd(x, y): 10 | while y: 11 | x, y = y, x % y 12 | return x 13 | 14 | if x + y < z: 15 | return False 16 | if x == z or y == z or x + y == z: 17 | return True 18 | return z % gcd(x, y) == 0 19 | -------------------------------------------------------------------------------- /367. Valid Perfect Square/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPerfectSquare(int num) { 4 | long low = 0; 5 | long high = num; 6 | while (low <= high) { 7 | long mid = (low + high) / 2; 8 | long sqar = mid * mid; 9 | if (sqar == num) { 10 | return true; 11 | } 12 | if (sqar < num) { 13 | low = mid + 1; 14 | } else { 15 | high = mid - 1; 16 | } 17 | } 18 | return false; 19 | } 20 | }; -------------------------------------------------------------------------------- /367. Valid Perfect Square/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPerfectSquare(int num) { 3 | long low = 0; 4 | long high = num; 5 | while (low <= high) { 6 | long mid = (low + high) / 2; 7 | long sqar = mid * mid; 8 | if (sqar == num) { 9 | return true; 10 | } 11 | if (sqar < num) { 12 | low = mid + 1; 13 | } else { 14 | high = mid - 1; 15 | } 16 | } 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /367. Valid Perfect Square/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isPerfectSquare(self, num): 3 | """ 4 | :type num: int 5 | :rtype: bool 6 | """ 7 | low = 0 8 | high = num 9 | while low <= high: 10 | mid = (low + high) // 2 11 | sqar = mid * mid 12 | if sqar == num: 13 | return True 14 | if sqar < num: 15 | low = mid + 1 16 | elif sqar > num: 17 | high = mid - 1 18 | return False 19 | -------------------------------------------------------------------------------- /371. Sum of Two Integers/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getSum(int a, int b) { 4 | int sum = a ^ b; 5 | int part = a & b; 6 | while (part) { 7 | int theA = sum; 8 | int theB = part << 1; 9 | sum = theA ^ theB; 10 | part = theA & theB; 11 | } 12 | return sum; 13 | } 14 | }; -------------------------------------------------------------------------------- /371. Sum of Two Integers/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int getSum(int a, int b) { 3 | int sum = a ^ b; 4 | int part = a & b; 5 | while (part != 0) { 6 | int theA = sum; 7 | int theB = part << 1; 8 | sum = theA ^ theB; 9 | part = theA & theB; 10 | } 11 | return sum; 12 | } 13 | } -------------------------------------------------------------------------------- /371. Sum of Two Integers/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getSum(self, a, b): 3 | """ 4 | :type a: int 5 | :type b: int 6 | :rtype: int 7 | """ 8 | mask = 0xffffffff 9 | sum = (a ^ b) & mask 10 | part = a & b 11 | while part: 12 | a = sum 13 | b = (part << 1) & mask 14 | sum = (a ^ b) & mask 15 | part = a & b 16 | if sum & 0x80000000: 17 | sum -= 0x100000000 18 | return sum 19 | -------------------------------------------------------------------------------- /372. Super Pow/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def superPow(self, a, b): 3 | """ 4 | :type a: int 5 | :type b: List[int] 6 | :rtype: int 7 | """ 8 | def pow(a, n, base): 9 | result = 1 10 | for _ in range(n): 11 | result = (result * a) % base 12 | return result 13 | 14 | if not b: 15 | return 1 16 | last = b.pop() 17 | base = 1337 18 | return pow(self.superPow(a, b), 10, base) * pow(a, last, base) % base 19 | -------------------------------------------------------------------------------- /377. Combination Sum IV/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int combinationSum4(vector &nums, int target) { 4 | vector results(target + 1); 5 | results[0] = 1; 6 | for (int i = 0; i <= target; i++) { 7 | for (auto &num : nums) { 8 | if (i - num >= 0) { 9 | results[i] += results[i - num]; 10 | } 11 | } 12 | } 13 | return results[target]; 14 | } 15 | }; -------------------------------------------------------------------------------- /377. Combination Sum IV/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int combinationSum4(int[] nums, int target) { 3 | int[] results = new int[target + 1]; 4 | results[0] = 1; 5 | for (int i = 1; i <= target; i++) { 6 | for (int num : nums) { 7 | if (i - num >= 0) { 8 | results[i] += results[i - num]; 9 | } 10 | } 11 | } 12 | return results[target]; 13 | } 14 | } -------------------------------------------------------------------------------- /377. Combination Sum IV/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def combinationSum4(self, nums, target): 3 | """ 4 | :type nums: List[int] 5 | :type target: int 6 | :rtype: int 7 | """ 8 | results = [0] * (target + 1) 9 | results[0] = 1 10 | for i in range(1, target + 1): 11 | for num in nums: 12 | if i - num >= 0: 13 | results[i] += results[i - num] 14 | return results[target] 15 | -------------------------------------------------------------------------------- /383. Ransom Note/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canConstruct(string ransomNote, string magazine) { 4 | vector has(26, 0); 5 | for (char item : magazine) { 6 | has[item - 'a']++; 7 | } 8 | for (char item : ransomNote) { 9 | if (has[item - 'a'] == 0) { 10 | return false; 11 | } 12 | has[item - 'a']--; 13 | } 14 | return true; 15 | } 16 | }; -------------------------------------------------------------------------------- /383. Ransom Note/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean canConstruct(String ransomNote, String magazine) { 3 | int[] has = new int[26]; 4 | for (char item : magazine.toCharArray()) { 5 | has[item - 'a']++; 6 | } 7 | for (char item : ransomNote.toCharArray()) { 8 | if (has[item - 'a'] == 0) { 9 | return false; 10 | } 11 | has[item - 'a']--; 12 | } 13 | return true; 14 | } 15 | } -------------------------------------------------------------------------------- /383. Ransom Note/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canConstruct(self, ransomNote, magazine): 3 | """ 4 | :type ransomNote: str 5 | :type magazine: str 6 | :rtype: bool 7 | """ 8 | has = [0] * 26 9 | for item in magazine: 10 | has[ord(item) - ord('a')] += 1 11 | for item in ransomNote: 12 | if has[ord(item) - ord('a')] == 0: 13 | return False 14 | has[ord(item) - ord('a')] -= 1 15 | return True 16 | -------------------------------------------------------------------------------- /387. First Unique Character in a String/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstUniqChar(string s) { 4 | vector counts(26, 0); 5 | for (auto item : s) { 6 | counts[item - 'a']++; 7 | } 8 | for (int i = 0; i < s.size(); i++) { 9 | if (counts[s[i] - 'a'] == 1) { 10 | return i; 11 | } 12 | } 13 | return -1; 14 | } 15 | }; -------------------------------------------------------------------------------- /387. First Unique Character in a String/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int firstUniqChar(String s) { 3 | int[] counts = new int[26]; 4 | for (char item : s.toCharArray()) { 5 | counts[item - 'a']++; 6 | } 7 | int index = 0; 8 | for (char item : s.toCharArray()) { 9 | if (counts[item - 'a'] == 1) { 10 | return index; 11 | } 12 | index++; 13 | } 14 | return -1; 15 | } 16 | } -------------------------------------------------------------------------------- /387. First Unique Character in a String/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def firstUniqChar(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | counts = [0] * 26 8 | for item in s: 9 | counts[ord(item) - ord('a')] += 1 10 | for (index, item) in enumerate(s): 11 | if counts[ord(item) - ord('a')] == 1: 12 | return index 13 | return -1 14 | -------------------------------------------------------------------------------- /389. Find the Difference/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char findTheDifference(string s, string t) { 4 | char result = 0; 5 | for (char item : s) { 6 | result ^= item; 7 | } 8 | for (char item : t) { 9 | result ^= item; 10 | } 11 | return result; 12 | } 13 | }; -------------------------------------------------------------------------------- /389. Find the Difference/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public char findTheDifference(String s, String t) { 3 | char result = 0; 4 | for (char item : s.toCharArray()) { 5 | result ^= item; 6 | } 7 | for (char item : t.toCharArray()) { 8 | result ^= item; 9 | } 10 | return result; 11 | } 12 | } -------------------------------------------------------------------------------- /389. Find the Difference/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findTheDifference(self, s, t): 3 | """ 4 | :type s: str 5 | :type t: str 6 | :rtype: str 7 | """ 8 | result_ord = 0 9 | for item in s: 10 | result_ord ^= ord(item) 11 | for item in t: 12 | result_ord ^= ord(item) 13 | return chr(result_ord) 14 | -------------------------------------------------------------------------------- /390. Elimination Game/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def lastRemaining(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | def left_to_right(n): 8 | if n == 1: 9 | return n 10 | return 2 * right_to_left(n // 2) 11 | 12 | def right_to_left(n): 13 | if n == 1: 14 | return n 15 | if n % 2: 16 | return 2 * left_to_right(n // 2) 17 | return 2 * left_to_right(n // 2) - 1 18 | 19 | return left_to_right(n) 20 | -------------------------------------------------------------------------------- /392. Is Subsequence/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubsequence(string s, string t) { 4 | if (s.empty()) { 5 | return true; 6 | } 7 | int indexS = 0; 8 | for (auto item : t) { 9 | if (item == s[indexS]) { 10 | if (indexS == s.length() - 1) { 11 | return true; 12 | } 13 | indexS++; 14 | } 15 | } 16 | return false; 17 | } 18 | }; -------------------------------------------------------------------------------- /392. Is Subsequence/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isSubsequence(String s, String t) { 3 | if (s.isEmpty()) { 4 | return true; 5 | } 6 | int indexS = 0; 7 | for (char item : t.toCharArray()) { 8 | if (item == s.charAt(indexS)) { 9 | if (indexS == s.length() - 1) { 10 | return true; 11 | } 12 | indexS++; 13 | } 14 | } 15 | return false; 16 | } 17 | } -------------------------------------------------------------------------------- /392. Is Subsequence/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isSubsequence(self, s, t): 3 | """ 4 | :type s: str 5 | :type t: str 6 | :rtype: bool 7 | """ 8 | if not s: 9 | return True 10 | index_s = 0 11 | for item in t: 12 | if item == s[index_s]: 13 | if index_s == len(s) - 1: 14 | return True 15 | index_s += 1 16 | return False 17 | -------------------------------------------------------------------------------- /396. Rotate Function/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxRotateFunction(vector &A) { 4 | int sum = 0; 5 | int F = 0; 6 | int length = A.size(); 7 | for (int i = 0; i < length; i++) { 8 | sum += A[i]; 9 | F += i * A[i]; 10 | } 11 | int theMax = F; 12 | for (int i = 1; i < length; i++) { 13 | F += sum - length * A[length - i]; 14 | theMax = max(theMax, F); 15 | } 16 | return theMax; 17 | } 18 | }; -------------------------------------------------------------------------------- /396. Rotate Function/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxRotateFunction(int[] A) { 3 | int sum = 0; 4 | int F = 0; 5 | for (int i = 0; i < A.length; i++) { 6 | sum += A[i]; 7 | F += i * A[i]; 8 | } 9 | int max = F; 10 | for (int i = 1; i < A.length; i++) { 11 | F += sum - A.length * A[A.length - i]; 12 | max = Math.max(max, F); 13 | } 14 | return max; 15 | } 16 | } -------------------------------------------------------------------------------- /396. Rotate Function/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxRotateFunction(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: int 6 | """ 7 | the_sum = sum(A) 8 | F = sum([index * item for index, item in enumerate(A)]) 9 | current_max = F 10 | length = len(A) 11 | for i in range(1, length): 12 | F += the_sum - length * A[length - i] 13 | current_max = max(current_max, F) 14 | return current_max 15 | -------------------------------------------------------------------------------- /397. Integer Replacement/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int integerReplacement(int n) { 4 | if (n == INT_MAX) { 5 | return 32; 6 | } 7 | int count = 0; 8 | while (n > 1) { 9 | if (n % 2 == 0) { 10 | n /= 2; 11 | } else { 12 | if ((n + 1) % 4 == 0 && n != 3) { 13 | n++; 14 | } else { 15 | n--; 16 | } 17 | } 18 | count++; 19 | } 20 | return count; 21 | } 22 | }; -------------------------------------------------------------------------------- /397. Integer Replacement/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int integerReplacement(int n) { 3 | if (n == Integer.MAX_VALUE) { 4 | return 32; 5 | } 6 | int count = 0; 7 | while (n > 1) { 8 | if (n % 2 == 0) { 9 | n /= 2; 10 | } else { 11 | if ((n + 1) % 4 == 0 && n != 3) { 12 | n++; 13 | } else { 14 | n--; 15 | } 16 | } 17 | count++; 18 | } 19 | return count; 20 | } 21 | } -------------------------------------------------------------------------------- /397. Integer Replacement/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def integerReplacement(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | count = 0 8 | while n > 1: 9 | if n % 2: 10 | if not (n + 1) % 4 and n != 3: 11 | n += 1 12 | else: 13 | n -= 1 14 | else: 15 | n //= 2 16 | count += 1 17 | return count 18 | -------------------------------------------------------------------------------- /400. Nth Digit/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findNthDigit(int n) { 4 | int digits = 1; 5 | int start = 1; 6 | long length = 9; 7 | while (n > digits * length) { 8 | n -= digits * length; 9 | digits++; 10 | start *= 10; 11 | length *= 10; 12 | } 13 | n--; 14 | int number = start + n / digits; 15 | return to_string(number)[n % digits] - '0'; 16 | } 17 | }; -------------------------------------------------------------------------------- /400. Nth Digit/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findNthDigit(int n) { 3 | int digits = 1; 4 | int start = 1; 5 | long length = 9; 6 | while (n > digits * length) { 7 | n -= digits * length; 8 | digits++; 9 | start *= 10; 10 | length *= 10; 11 | } 12 | n--; 13 | int number = start + n / digits; 14 | return Character.getNumericValue(String.valueOf(number).charAt(n % digits)); 15 | } 16 | } -------------------------------------------------------------------------------- /400. Nth Digit/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findNthDigit(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | digits = 1 8 | start = 1 9 | length = 9 10 | while (n > digits * length): 11 | n -= digits * length 12 | digits += 1 13 | start *= 10 14 | length *= 10 15 | n -= 1 16 | number = start + n // digits 17 | return int(str(number)[n % digits]) 18 | -------------------------------------------------------------------------------- /405. Convert a Number to Hexadecimal/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string toHex(int num) { 4 | if (num == 0) { 5 | return "0"; 6 | } 7 | string symbols = "0123456789abcdef"; 8 | string result = ""; 9 | while (num) { 10 | int value = num & 0xf; 11 | result += symbols[value]; 12 | num = (int)((unsigned int) num >> 4); 13 | } 14 | reverse(result.rbegin(), result.rend()); 15 | return result; 16 | } 17 | }; -------------------------------------------------------------------------------- /405. Convert a Number to Hexadecimal/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String toHex(int num) { 3 | if (num == 0) { 4 | return "0"; 5 | } 6 | String symbols = "0123456789abcdef"; 7 | StringBuilder result = new StringBuilder(); 8 | while (num != 0) { 9 | int value = num & 0xf; 10 | result.append(symbols.charAt(value)); 11 | num >>>= 4; 12 | } 13 | return result.reverse().toString(); 14 | } 15 | } -------------------------------------------------------------------------------- /405. Convert a Number to Hexadecimal/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def toHex(self, num): 3 | """ 4 | :type num: int 5 | :rtype: str 6 | """ 7 | if num == 0: 8 | return '0' 9 | symbols = '0123456789abcdef' 10 | result = '' 11 | while num: 12 | value = num & 0xf 13 | result += symbols[value] 14 | num = num >> 4 if num > 0 else (num % 0x100000000) >> 4 15 | return result[::-1] 16 | -------------------------------------------------------------------------------- /406. Queue Reconstruction by Height/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def reconstructQueue(self, people): 3 | """ 4 | :type people: List[List[int]] 5 | :rtype: List[List[int]] 6 | """ 7 | result = [] 8 | people.sort(key=lambda item: (-item[0], item[1])) 9 | for person in people: 10 | result.insert(person[1], person) 11 | return result 12 | -------------------------------------------------------------------------------- /409. Longest Palindrome/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestPalindrome(string s) { 4 | int countArray['z' - 'A' + 1] = {0}; 5 | int pair = 0; 6 | bool isOddExists = false; 7 | for (char item : s) { 8 | countArray[item - 'A']++; 9 | } 10 | for (int item : countArray) { 11 | pair += item / 2; 12 | if (!isOddExists) { 13 | isOddExists = item % 2 == 1; 14 | } 15 | } 16 | return pair * 2 + (isOddExists ? 1 : 0); 17 | } 18 | }; -------------------------------------------------------------------------------- /409. Longest Palindrome/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int longestPalindrome(String s) { 3 | int[] countArray = new int['z' - 'A' + 1]; 4 | int pair = 0; 5 | boolean isOddExists = false; 6 | for (char item : s.toCharArray()) { 7 | countArray[item - 'A']++; 8 | } 9 | for (int item : countArray) { 10 | pair += item / 2; 11 | if (!isOddExists) { 12 | isOddExists = item % 2 != 0; 13 | } 14 | } 15 | return pair * 2 + (isOddExists ? 1 : 0); 16 | } 17 | } -------------------------------------------------------------------------------- /409. Longest Palindrome/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestPalindrome(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | count_array = [0] * (ord('z') - ord('A') + 1) 8 | pair = 0 9 | is_odd_exists = False 10 | for item in s: 11 | count_array[ord(item) - ord('A')] += 1 12 | for item in count_array: 13 | pair += item // 2 14 | if not is_odd_exists: 15 | is_odd_exists = item % 2 != 0 16 | return pair * 2 + (1 if is_odd_exists else 0) 17 | -------------------------------------------------------------------------------- /412. Fizz Buzz/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector fizzBuzz(int n) { 4 | vector result; 5 | for (int i = 1; i <= n; i++) { 6 | string formatted = to_string(i); 7 | if (i % 3 == 0 && i % 5 == 0) { 8 | formatted = "FizzBuzz"; 9 | } else if (i % 3 == 0) { 10 | formatted = "Fizz"; 11 | } else if (i % 5 == 0) { 12 | formatted = "Buzz"; 13 | } 14 | result.push_back(formatted); 15 | } 16 | return result; 17 | } 18 | }; -------------------------------------------------------------------------------- /412. Fizz Buzz/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def fizzBuzz(self, n): 3 | """ 4 | :type n: int 5 | :rtype: List[str] 6 | """ 7 | result = [] 8 | for item in range(1, n + 1): 9 | formatted = str(item) 10 | if item % 3 == 0 and item % 5 == 0: 11 | formatted = 'FizzBuzz' 12 | elif item % 3 == 0: 13 | formatted = 'Fizz' 14 | elif item % 5 == 0: 15 | formatted = 'Buzz' 16 | result.append(formatted) 17 | return result 18 | -------------------------------------------------------------------------------- /419. Battleships in a Board/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countBattleships(vector> &board) { 4 | int result = 0; 5 | for (int i = 0; i < board.size(); i++) { 6 | for (int j = 0; j < board[0].size(); j++) { 7 | if (board[i][j] == 'X' && (j == 0 || board[i][j - 1] == '.') && (i == 0 || board[i - 1][j] == '.')) { 8 | result++; 9 | } 10 | } 11 | } 12 | return result; 13 | } 14 | }; -------------------------------------------------------------------------------- /419. Battleships in a Board/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int countBattleships(char[][] board) { 3 | int result = 0; 4 | for (int i = 0; i < board.length; i++) { 5 | for (int j = 0; j < board[0].length; j++) { 6 | if (board[i][j] == 'X' && (j == 0 || board[i][j - 1] == '.') && (i == 0 || board[i - 1][j] == '.')) { 7 | result++; 8 | } 9 | } 10 | } 11 | return result; 12 | } 13 | } -------------------------------------------------------------------------------- /419. Battleships in a Board/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countBattleships(self, board): 3 | """ 4 | :type board: List[List[str]] 5 | :rtype: int 6 | """ 7 | result = 0 8 | for i in range(len(board)): 9 | for j in range(len(board[0])): 10 | if board[i][j] == 'X': 11 | if (j == 0 or board[i][j - 1] == '.') and (i == 0 or board[i - 1][j] == '.'): 12 | result += 1 13 | return result 14 | -------------------------------------------------------------------------------- /434. Number of Segments in a String/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSegments(string s) { 4 | int result = 0; 5 | for (int i = 0; i < s.size(); i++) { 6 | if (s[i] != ' ' && (i == 0 || s[i - 1] == ' ')) { 7 | result++; 8 | } 9 | } 10 | return result; 11 | } 12 | }; -------------------------------------------------------------------------------- /434. Number of Segments in a String/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int countSegments(String s) { 3 | int result = 0; 4 | for (int i = 0; i < s.length(); i++) { 5 | if (s.charAt(i) != ' ' && (i == 0 || s.charAt(i - 1) == ' ')) { 6 | result++; 7 | } 8 | } 9 | return result; 10 | } 11 | } -------------------------------------------------------------------------------- /434. Number of Segments in a String/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countSegments(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | result = 0 8 | for i in range(0, len(s)): 9 | if s[i] != ' ' and (i == 0 or s[i - 1] == ' '): 10 | result += 1 11 | return result 12 | -------------------------------------------------------------------------------- /441. Arranging Coins/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrangeCoins(int n) { 4 | return (int)((sqrt(1 + 8.0 * n) - 1) / 2); 5 | } 6 | }; -------------------------------------------------------------------------------- /441. Arranging Coins/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int arrangeCoins(int n) { 3 | return (int)((Math.sqrt(1 + 8.0 * n) - 1) / 2); 4 | } 5 | } -------------------------------------------------------------------------------- /441. Arranging Coins/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def arrangeCoins(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | return (int)(((1 + 8 * n) ** 0.5 - 1) / 2) 8 | -------------------------------------------------------------------------------- /442. Find All Duplicates in an Array/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findDuplicates(vector &nums) { 4 | vector result; 5 | for (auto num : nums) { 6 | if (nums[abs(num) - 1] < 0) { 7 | result.push_back(abs(num)); 8 | } else { 9 | nums[abs(num) - 1] *= -1; 10 | } 11 | } 12 | return result; 13 | } 14 | }; -------------------------------------------------------------------------------- /442. Find All Duplicates in an Array/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List findDuplicates(int[] nums) { 3 | List result = new ArrayList<>(); 4 | for (int num : nums) { 5 | if (nums[Math.abs(num) - 1] < 0) { 6 | result.add(Math.abs(num)); 7 | } else { 8 | nums[Math.abs(num) - 1] *= -1; 9 | } 10 | } 11 | return result; 12 | } 13 | } -------------------------------------------------------------------------------- /442. Find All Duplicates in an Array/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findDuplicates(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: List[int] 6 | """ 7 | result = [] 8 | for num in nums: 9 | if nums[abs(num) - 1] < 0: 10 | result.append(abs(num)) 11 | else: 12 | nums[abs(num) - 1] *= -1 13 | return result 14 | -------------------------------------------------------------------------------- /447. Number of Boomerangs/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfBoomerangs(self, points): 3 | """ 4 | :type points: List[List[int]] 5 | :rtype: int 6 | """ 7 | result = 0 8 | for point_a in points: 9 | distances = {} 10 | for point_b in points: 11 | distance = (point_a[0] - point_b[0]) ** 2 + (point_a[1] - point_b[1]) ** 2 12 | distances[distance] = distances.get(distance, 0) + 1 13 | result += sum(item * (item - 1) for item in distances.values()) 14 | return result 15 | -------------------------------------------------------------------------------- /448. Find All Numbers Disappeared in an Array/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findDisappearedNumbers(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: List[int] 6 | """ 7 | for num in nums: 8 | index = num if num > 0 else -num 9 | index -= 1 10 | if nums[index] > 0: 11 | nums[index] = -nums[index] 12 | result = [] 13 | for (index, num) in enumerate(nums): 14 | if num > 0: 15 | result.append(index + 1) 16 | return result 17 | -------------------------------------------------------------------------------- /452. Minimum Number of Arrows to Burst Balloons/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findMinArrowShots(self, points): 3 | """ 4 | :type points: List[List[int]] 5 | :rtype: int 6 | """ 7 | points.sort(key=lambda point: point[1]) 8 | current_start = -sys.maxsize 9 | result = 0 10 | for point in points: 11 | if point[0] > current_start: 12 | current_start = point[1] 13 | result += 1 14 | return result 15 | -------------------------------------------------------------------------------- /453. Minimum Moves to Equal Array Elements/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMoves(vector &nums) { 4 | int sum = 0; 5 | int min = INT32_MAX; 6 | for (int num : nums) { 7 | sum += num; 8 | if (num < min) { 9 | min = num; 10 | } 11 | } 12 | return sum - min * nums.size(); 13 | } 14 | }; -------------------------------------------------------------------------------- /453. Minimum Moves to Equal Array Elements/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int minMoves(int[] nums) { 3 | int sum = 0; 4 | int min = Integer.MAX_VALUE; 5 | for (int num : nums) { 6 | sum += num; 7 | if (num < min) { 8 | min = num; 9 | } 10 | } 11 | return sum - min * nums.length; 12 | } 13 | } -------------------------------------------------------------------------------- /453. Minimum Moves to Equal Array Elements/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def minMoves(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | return sum(nums) - min(nums) * len(nums) 8 | -------------------------------------------------------------------------------- /455. Assign Cookies/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findContentChildren(vector &g, vector &s) { 4 | sort(g.begin(), g.end()); 5 | sort(s.begin(), s.end()); 6 | int i = 0; 7 | int count = 0; 8 | for (auto item : g) { 9 | while (i < s.size() && item > s[i]) { 10 | i++; 11 | } 12 | if (i == s.size()) { 13 | break; 14 | } 15 | count++; 16 | i++; 17 | } 18 | return count; 19 | } 20 | }; -------------------------------------------------------------------------------- /455. Assign Cookies/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findContentChildren(int[] g, int[] s) { 3 | Arrays.sort(g); 4 | Arrays.sort(s); 5 | int i = 0; 6 | int count = 0; 7 | for (int item : g) { 8 | while (i < s.length && item > s[i]) { 9 | i++; 10 | } 11 | if (i == s.length) { 12 | break; 13 | } 14 | count++; 15 | i++; 16 | } 17 | return count; 18 | } 19 | } -------------------------------------------------------------------------------- /455. Assign Cookies/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findContentChildren(self, g, s): 3 | """ 4 | :type g: List[int] 5 | :type s: List[int] 6 | :rtype: int 7 | """ 8 | g = sorted(g) 9 | s = sorted(s) 10 | i = 0 11 | count = 0 12 | for item in g: 13 | while i < len(s) and item > s[i]: 14 | i += 1 15 | if i == len(s): 16 | break 17 | count += 1 18 | i += 1 19 | return count 20 | -------------------------------------------------------------------------------- /461. Hamming Distance/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingDistance(int x, int y) { 4 | int count = 0; 5 | int xorValue = x ^ y; 6 | while (xorValue != 0) { 7 | xorValue &= (xorValue - 1); 8 | count++; 9 | } 10 | return count; 11 | } 12 | }; -------------------------------------------------------------------------------- /461. Hamming Distance/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int hammingDistance(int x, int y) { 3 | int count = 0; 4 | int xor = x ^ y; 5 | while (xor != 0) { 6 | xor &= (xor - 1); 7 | count++; 8 | } 9 | return count; 10 | } 11 | } -------------------------------------------------------------------------------- /461. Hamming Distance/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def hammingDistance(self, x, y): 3 | """ 4 | :type x: int 5 | :type y: int 6 | :rtype: int 7 | """ 8 | count = 0 9 | xor = x ^ y 10 | while xor != 0: 11 | xor &= (xor - 1) 12 | count += 1 13 | return count 14 | -------------------------------------------------------------------------------- /462. Minimum Moves to Equal Array Elements II/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMoves2(vector &nums) { 4 | sort(nums.begin(), nums.end()); 5 | int result = 0; 6 | int i = 0; 7 | int j = nums.size() - 1; 8 | while (i < j) { 9 | result += nums[j] - nums[i]; 10 | i++; 11 | j--; 12 | } 13 | return result; 14 | } 15 | }; -------------------------------------------------------------------------------- /462. Minimum Moves to Equal Array Elements II/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int minMoves2(int[] nums) { 3 | Arrays.sort(nums); 4 | int result = 0; 5 | int i = 0; 6 | int j = nums.length - 1; 7 | while (i < j) { 8 | result += nums[j] - nums[i]; 9 | i++; 10 | j--; 11 | } 12 | return result; 13 | } 14 | } -------------------------------------------------------------------------------- /462. Minimum Moves to Equal Array Elements II/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def minMoves2(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | nums.sort() 8 | result = 0 9 | i = 0 10 | j = len(nums) - 1 11 | while i < j: 12 | result += nums[j] - nums[i] 13 | i += 1 14 | j -= 1 15 | return result 16 | -------------------------------------------------------------------------------- /475. Heaters/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findRadius(int[] houses, int[] heaters) { 3 | Arrays.sort(houses); 4 | Arrays.sort(heaters); 5 | int heaterIndex = 0; 6 | int result = 0; 7 | for (int house : houses) { 8 | while (heaterIndex + 1 < heaters.length && Math.abs(house - heaters[heaterIndex]) >= Math.abs(heaters[heaterIndex + 1] - house)) { 9 | heaterIndex++; 10 | } 11 | result = Math.max(result, Math.abs(house - heaters[heaterIndex])); 12 | } 13 | return result; 14 | } 15 | } -------------------------------------------------------------------------------- /476. Number Complement/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findComplement(int num) { 4 | long allOnes = 1; 5 | while (allOnes <= num) { 6 | allOnes <<= 1; 7 | } 8 | allOnes -= 1; 9 | return (int) allOnes ^ num; 10 | } 11 | }; -------------------------------------------------------------------------------- /476. Number Complement/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findComplement(int num) { 3 | long allOnes = 1; 4 | while (allOnes <= num) { 5 | allOnes <<= 1; 6 | } 7 | allOnes -= 1; 8 | return (int) allOnes ^ num; 9 | } 10 | } -------------------------------------------------------------------------------- /476. Number Complement/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findComplement(self, num): 3 | """ 4 | :type num: int 5 | :rtype: int 6 | """ 7 | all_ones = 1 8 | while all_ones <= num: 9 | all_ones <<= 1 10 | all_ones -= 1 11 | return all_ones ^ num 12 | -------------------------------------------------------------------------------- /477. Total Hamming Distance/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int totalHammingDistance(vector &nums) { 4 | int result = 0; 5 | for (int i = 0; i < 32; i++) { 6 | int base = 1 << i; 7 | int countOnes = 0; 8 | for (int &num : nums) { 9 | if (base & num) { 10 | countOnes++; 11 | } 12 | } 13 | result += countOnes * (nums.size() - countOnes); 14 | } 15 | return result; 16 | } 17 | }; -------------------------------------------------------------------------------- /477. Total Hamming Distance/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int totalHammingDistance(int[] nums) { 3 | int result = 0; 4 | for (int i = 0; i < 32; i++) { 5 | int base = 1 << i; 6 | int countOne = 0; 7 | for (int num : nums) { 8 | if ((base & num) != 0) { 9 | countOne++; 10 | } 11 | } 12 | result += countOne * (nums.length - countOne); 13 | } 14 | return result; 15 | } 16 | } -------------------------------------------------------------------------------- /477. Total Hamming Distance/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def totalHammingDistance(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | result = 0 8 | for i in range(32): 9 | base = 1 << i 10 | count_one = 0 11 | for num in nums: 12 | if base & num: 13 | count_one += 1 14 | result += count_one * (len(nums) - count_one) 15 | return result 16 | -------------------------------------------------------------------------------- /485. Max Consecutive Ones/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxConsecutiveOnes(vector &nums) { 4 | int result = 0; 5 | int count = 0; 6 | for (auto num : nums) { 7 | if (num == 0) { 8 | result = max(result, count); 9 | count = 0; 10 | } else { 11 | count++; 12 | } 13 | } 14 | result = max(result, count); 15 | return result; 16 | } 17 | }; -------------------------------------------------------------------------------- /485. Max Consecutive Ones/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findMaxConsecutiveOnes(int[] nums) { 3 | int result = 0; 4 | int count = 0; 5 | for (int num : nums) { 6 | if (num == 0) { 7 | result = Math.max(result, count); 8 | count = 0; 9 | } else { 10 | count++; 11 | } 12 | } 13 | result = Math.max(result, count); 14 | return result; 15 | } 16 | } -------------------------------------------------------------------------------- /485. Max Consecutive Ones/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findMaxConsecutiveOnes(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | result = 0 8 | count = 0 9 | for num in nums: 10 | if num == 0: 11 | result = max(result, count) 12 | count = 0 13 | else: 14 | count += 1 15 | result = max(result, count) 16 | return result 17 | -------------------------------------------------------------------------------- /492. Construct the Rectangle/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector constructRectangle(int area) { 4 | int x = (int) ceil(sqrt(area)); 5 | while (area % x != 0) { 6 | x++; 7 | } 8 | int y = area / x; 9 | return vector {x, y}; 10 | } 11 | }; -------------------------------------------------------------------------------- /492. Construct the Rectangle/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] constructRectangle(int area) { 3 | int x = (int) Math.ceil(Math.sqrt(area)); 4 | while (area % x != 0) { 5 | x++; 6 | } 7 | int y = area / x; 8 | return new int[] {x, y}; 9 | } 10 | } -------------------------------------------------------------------------------- /492. Construct the Rectangle/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def constructRectangle(self, area): 3 | """ 4 | :type area: int 5 | :rtype: List[int] 6 | """ 7 | x = int(math.ceil(area ** 0.5)) 8 | while area % x != 0: 9 | x += 1 10 | y = int(area / x) 11 | return [x, y] 12 | -------------------------------------------------------------------------------- /495. Teemo Attacking/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPoisonedDuration(vector &timeSeries, int duration) { 4 | if (timeSeries.empty()) { 5 | return 0; 6 | } 7 | int result = duration; 8 | int gain; 9 | for (int i = 1; i < timeSeries.size(); i++) { 10 | result += duration; 11 | gain = duration - (timeSeries[i] - timeSeries[i - 1]); 12 | if (gain > 0) { 13 | result -= gain; 14 | } 15 | } 16 | return result; 17 | } 18 | }; -------------------------------------------------------------------------------- /495. Teemo Attacking/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findPoisonedDuration(int[] timeSeries, int duration) { 3 | if (timeSeries.length == 0) { 4 | return 0; 5 | } 6 | int result = duration; 7 | int gain; 8 | for (int i = 1; i < timeSeries.length; i++) { 9 | result += duration; 10 | gain = duration - (timeSeries[i] - timeSeries[i - 1]); 11 | if (gain > 0) { 12 | result -= gain; 13 | } 14 | } 15 | return result; 16 | } 17 | } -------------------------------------------------------------------------------- /495. Teemo Attacking/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findPoisonedDuration(self, timeSeries, duration): 3 | """ 4 | :type timeSeries: List[int] 5 | :type duration: int 6 | :rtype: int 7 | """ 8 | if not timeSeries: 9 | return 0 10 | result = duration 11 | for i in range(1, len(timeSeries)): 12 | result += duration 13 | gain = duration - (timeSeries[i] - timeSeries[i - 1]) 14 | if gain > 0: 15 | result -= gain 16 | return result 17 | -------------------------------------------------------------------------------- /504. Base 7/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convertToBase7(int num) { 4 | if (num == 0) { 5 | return "0"; 6 | } 7 | int sign = num >= 0 ? 1 : -1; 8 | num = abs(num); 9 | string result = ""; 10 | while (num != 0) { 11 | result = to_string(num % 7) + result; 12 | num /= 7; 13 | } 14 | 15 | return (sign < 0 ? "-" : "") + result; 16 | } 17 | }; -------------------------------------------------------------------------------- /504. Base 7/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public String convertToBase7(int num) { 3 | if (num == 0) { 4 | return "0"; 5 | } 6 | int sign = num >= 0 ? 1 : -1; 7 | num = Math.abs(num); 8 | StringBuilder builder = new StringBuilder(); 9 | while (num != 0) { 10 | builder.append(num % 7); 11 | num /= 7; 12 | } 13 | if (sign < 0) { 14 | builder.append('-'); 15 | } 16 | return builder.reverse().toString(); 17 | } 18 | } -------------------------------------------------------------------------------- /504. Base 7/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def convertToBase7(self, num): 3 | """ 4 | :type num: int 5 | :rtype: str 6 | """ 7 | if num == 0: 8 | return str(num) 9 | sign = 1 if num >= 0 else -1 10 | num = abs(num) 11 | result = '' 12 | while num: 13 | result = str(num % 7) + result 14 | num //= 7 15 | return ('-' if sign < 0 else '') + result 16 | -------------------------------------------------------------------------------- /507. Perfect Number/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkPerfectNumber(int num) { 4 | if (num == 1) { 5 | return false; 6 | } 7 | int result = 0; 8 | int i = 2; 9 | while (i * i <= num) { 10 | if (num % i == 0) { 11 | result += i; 12 | result += num / i; 13 | } 14 | i++; 15 | } 16 | result++; 17 | return result == num; 18 | } 19 | }; -------------------------------------------------------------------------------- /507. Perfect Number/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean checkPerfectNumber(int num) { 3 | if (num == 1) { 4 | return false; 5 | } 6 | int result = 0; 7 | int i = 2; 8 | while (i * i <= num) { 9 | if (num % i == 0) { 10 | result += i; 11 | result += num / i; 12 | } 13 | i++; 14 | } 15 | result++; 16 | return result == num; 17 | } 18 | } -------------------------------------------------------------------------------- /507. Perfect Number/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def checkPerfectNumber(self, num): 3 | """ 4 | :type num: int 5 | :rtype: bool 6 | """ 7 | if num == 1: 8 | return False 9 | result = 0 10 | i = 2 11 | while i * i <= num: 12 | if num % i == 0: 13 | result += i 14 | result += num // i 15 | i += 1 16 | result += 1 17 | return result == num 18 | -------------------------------------------------------------------------------- /516. Longest Palindromic Subsequence/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def longestPalindromeSubseq(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | dp = [[0] * len(s) for i in range(len(s))] 8 | for i in range(len(s) - 1, -1, -1): 9 | dp[i][i] = 1 10 | for j in range(i + 1, len(s)): 11 | if s[i] == s[j]: 12 | dp[i][j] = dp[i + 1][j - 1] + 2 13 | else: 14 | dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]) 15 | return dp[0][len(s) - 1] 16 | -------------------------------------------------------------------------------- /517. Super Washing Machines/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findMinMoves(self, machines): 3 | """ 4 | :type machines: List[int] 5 | :rtype: int 6 | """ 7 | sum_up = sum(machines) 8 | if sum_up % len(machines): 9 | return -1 10 | average = sum_up / len(machines) 11 | ending_here = 0 12 | result = 0 13 | for machine in machines: 14 | current = machine - average 15 | ending_here += current 16 | result = max(result, abs(ending_here), current) 17 | return result 18 | -------------------------------------------------------------------------------- /521. Longest Uncommon Subsequence I/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLUSlength(string a, string b) { 4 | return a == b ? -1 : max(a.length(), b.length()); 5 | } 6 | }; -------------------------------------------------------------------------------- /521. Longest Uncommon Subsequence I/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findLUSlength(String a, String b) { 3 | return a.equals(b) ? -1 : Math.max(a.length(), b.length()); 4 | } 5 | } -------------------------------------------------------------------------------- /521. Longest Uncommon Subsequence I/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findLUSlength(self, a, b): 3 | """ 4 | :type a: str 5 | :type b: str 6 | :rtype: int 7 | """ 8 | return -1 if a == b else max(len(a), len(b)) 9 | -------------------------------------------------------------------------------- /540. Single Element in a Sorted Array/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNonDuplicate(vector &nums) { 4 | int low = 0; 5 | int high = nums.size() - 1; 6 | while (low < high) { 7 | int mid = (low + high) / 2; 8 | if ((mid % 2 == 0 && nums[mid] == nums[mid + 1]) || (mid % 2 != 0 && nums[mid] == nums[mid - 1])) { 9 | low = mid + 1; 10 | } else { 11 | high = mid - 1; 12 | } 13 | } 14 | return nums[low]; 15 | } 16 | }; -------------------------------------------------------------------------------- /540. Single Element in a Sorted Array/Java/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNonDuplicate(int[] nums) { 3 | int low = 0; 4 | int high = nums.length - 1; 5 | while (low < high) { 6 | int mid = (low + high) / 2; 7 | if ((mid % 2 == 0 && nums[mid] == nums[mid + 1]) || (mid % 2 != 0 && nums[mid] == nums[mid - 1])) { 8 | low = mid + 1; 9 | } else { 10 | high = mid - 1; 11 | } 12 | } 13 | return nums[low]; 14 | } 15 | } -------------------------------------------------------------------------------- /540. Single Element in a Sorted Array/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def singleNonDuplicate(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | low = 0 8 | high = len(nums) - 1 9 | while low < high: 10 | mid = (low + high) // 2 11 | if (mid % 2 == 0 and nums[mid] == nums[mid + 1]) or (mid % 2 != 0 and nums[mid] == nums[mid - 1]): 12 | low = mid + 1 13 | else: 14 | high = mid - 1 15 | return nums[low] 16 | -------------------------------------------------------------------------------- /541. Reverse String II/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def reverseStr(self, s, k): 3 | """ 4 | :type s: str 5 | :type k: int 6 | :rtype: str 7 | """ 8 | length = len(s) 9 | for i in range(0, length, 2 * k): 10 | if i + k >= length: 11 | s = s[:i] + s[i:][::-1] 12 | else: 13 | s = s[:i] + s[i:i + k][::-1] + s[i + k:] 14 | return s 15 | -------------------------------------------------------------------------------- /551. Student Attendance Record I/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkRecord(string s) { 4 | bool containsA = false; 5 | for (int i = 0; i < s.length(); i++) { 6 | char letter = s[i]; 7 | if ((letter == 'A' && containsA) || (letter == 'L' && i >= 2 && s[i - 1] == 'L' && s[i - 2] == 'L')) { 8 | return false; 9 | } 10 | if (letter == 'A' && !containsA) { 11 | containsA = true; 12 | } 13 | } 14 | return true; 15 | } 16 | }; -------------------------------------------------------------------------------- /551. Student Attendance Record I/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean checkRecord(String s) { 3 | boolean containsA = false; 4 | for (int i = 0; i < s.length(); i++) { 5 | char letter = s.charAt(i); 6 | if ((letter == 'A' && containsA) || (letter == 'L' && i >= 2 && s.charAt(i - 1) == 'L' && s.charAt(i - 2) == 'L')) { 7 | return false; 8 | } 9 | if (letter == 'A' && !containsA) { 10 | containsA = true; 11 | } 12 | } 13 | return true; 14 | } 15 | } -------------------------------------------------------------------------------- /551. Student Attendance Record I/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def checkRecord(self, s): 3 | """ 4 | :type s: str 5 | :rtype: bool 6 | """ 7 | contains_A = False 8 | for i, letter in enumerate(s): 9 | if (letter == 'A' and contains_A) or (letter == 'L' and i >= 2 and s[i - 1] == 'L' and s[i - 2] == 'L'): 10 | return False 11 | if letter == 'A' and not contains_A: 12 | contains_A = True 13 | return True 14 | -------------------------------------------------------------------------------- /554. Brick Wall/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def leastBricks(self, wall): 3 | """ 4 | :type wall: List[List[int]] 5 | :rtype: int 6 | """ 7 | count = {} 8 | for row in wall: 9 | current = 0 10 | for item in row[:-1]: 11 | current += item 12 | if current not in count: 13 | count[current] = 0 14 | count[current] += 1 15 | return len(wall) - (max(count.values()) if count else 0) 16 | -------------------------------------------------------------------------------- /560. Subarray Sum Equals K/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarraySum(vector &nums, int k) { 4 | int sum = 0; 5 | int result = 0; 6 | unordered_map memory; 7 | memory[0] = 1; 8 | for (int num : nums) { 9 | sum += num; 10 | if (memory.count(sum - k)) { 11 | result += memory[sum - k]; 12 | } 13 | if (!memory.count(sum)) { 14 | memory[sum] = 0; 15 | } 16 | memory[sum]++; 17 | } 18 | return result; 19 | } 20 | }; -------------------------------------------------------------------------------- /560. Subarray Sum Equals K/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def subarraySum(self, nums, k): 3 | """ 4 | :type nums: List[int] 5 | :type k: int 6 | :rtype: int 7 | """ 8 | sum = 0 9 | result = 0 10 | memory = {} 11 | memory[0] = 1 12 | for num in nums: 13 | sum += num 14 | if sum - k in memory: 15 | result += memory[sum - k] 16 | if sum not in memory: 17 | memory[sum] = 0 18 | memory[sum] += 1 19 | return result 20 | -------------------------------------------------------------------------------- /561. Array Partition I/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrayPairSum(vector &nums) { 4 | sort(nums.begin(), nums.end()); 5 | int sum = 0; 6 | for (int i = 0; i < nums.size(); i += 2) { 7 | sum += nums[i]; 8 | } 9 | return sum; 10 | } 11 | }; -------------------------------------------------------------------------------- /561. Array Partition I/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int arrayPairSum(int[] nums) { 3 | Arrays.sort(nums); 4 | int sum = 0; 5 | for (int i = 0; i < nums.length; i += 2) { 6 | sum += nums[i]; 7 | } 8 | return sum; 9 | } 10 | } -------------------------------------------------------------------------------- /561. Array Partition I/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def arrayPairSum(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | nums.sort() 8 | return sum(nums[::2]) 9 | -------------------------------------------------------------------------------- /566. Reshape the Matrix/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> matrixReshape(vector> &nums, int r, int c) { 4 | if (nums.empty()) { 5 | return nums; 6 | } 7 | int m = nums.size(); 8 | int n = nums[0].size(); 9 | if (m * n < r * c) { 10 | return nums; 11 | } 12 | vector> newNums(r, vector(c)); 13 | for (int i = 0; i < r * c; i++) { 14 | newNums[i / c][i % c] = nums[i / n][i % n]; 15 | } 16 | return newNums; 17 | } 18 | }; -------------------------------------------------------------------------------- /566. Reshape the Matrix/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[][] matrixReshape(int[][] nums, int r, int c) { 3 | if (nums.length == 0) { 4 | return nums; 5 | } 6 | int m = nums.length; 7 | int n = nums[0].length; 8 | if (m * n < r * c) { 9 | return nums; 10 | } 11 | int[][] newNums = new int[r][c]; 12 | for (int i = 0; i < r * c; i++) { 13 | newNums[i / c][i % c] = nums[i / n][i % n]; 14 | } 15 | return newNums; 16 | } 17 | } -------------------------------------------------------------------------------- /566. Reshape the Matrix/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def matrixReshape(self, nums, r, c): 3 | """ 4 | :type nums: List[List[int]] 5 | :type r: int 6 | :type c: int 7 | :rtype: List[List[int]] 8 | """ 9 | if not nums: 10 | return nums 11 | m = len(nums) 12 | n = len(nums[0]) 13 | if m * n < r * c: 14 | return nums 15 | new_nums = [[0] * c for _ in range(r)] 16 | for i in range(r * c): 17 | new_nums[i // c][i % c] = nums[i // n][i % n] 18 | return new_nums 19 | -------------------------------------------------------------------------------- /575. Distribute Candies/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int distributeCandies(vector &candies) { 4 | unordered_set nums = unordered_set(candies.begin(), candies.end()); 5 | int numNums = nums.size(); 6 | int numTarget = candies.size() / 2; 7 | return numNums >= numTarget ? numTarget : numNums; 8 | } 9 | }; -------------------------------------------------------------------------------- /575. Distribute Candies/Java/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int distributeCandies(int[] candies) { 3 | Set nums = new HashSet<>(); 4 | for (int candy : candies) { 5 | nums.add(candy); 6 | } 7 | int numNums = nums.size(); 8 | int numTarget = candies.length / 2; 9 | return numNums >= numTarget ? numTarget : numNums; 10 | } 11 | } -------------------------------------------------------------------------------- /575. Distribute Candies/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def distributeCandies(self, candies): 3 | """ 4 | :type candies: List[int] 5 | :rtype: int 6 | """ 7 | nums = set(candies) 8 | num_nums = len(nums) 9 | target_num = len(candies) // 2 10 | return target_num if num_nums >= target_num else num_nums 11 | -------------------------------------------------------------------------------- /594. Longest Harmonious Subsequence/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLHS(vector &nums) { 4 | unordered_map memory; 5 | for (int num : nums) { 6 | 7 | memory[num]++; 8 | } 9 | int result = 0; 10 | for (auto item : memory) { 11 | if (memory.find(item.first + 1) != memory.end()) { 12 | result = max(result, item.second + memory[item.first + 1]); 13 | } 14 | } 15 | return result; 16 | } 17 | }; -------------------------------------------------------------------------------- /594. Longest Harmonious Subsequence/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findLHS(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | memory = {} 8 | for num in nums: 9 | if num not in memory: 10 | memory[num] = 0 11 | memory[num] += 1 12 | result = 0 13 | for key, value in memory.items(): 14 | if key + 1 in memory: 15 | result = max(result, value + memory[key + 1]) 16 | return result 17 | -------------------------------------------------------------------------------- /598. Range Addition II/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxCount(int m, int n, vector> &ops) { 4 | int resultM = m; 5 | int resultN = n; 6 | for (const vector &op : ops) { 7 | resultM = min(resultM, op[0]); 8 | resultN = min(resultN, op[1]); 9 | } 10 | return resultM * resultN; 11 | } 12 | }; -------------------------------------------------------------------------------- /598. Range Addition II/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxCount(int m, int n, int[][] ops) { 3 | int resultM = m; 4 | int resultN = n; 5 | for (int[] op : ops) { 6 | resultM = Math.min(resultM, op[0]); 7 | resultN = Math.min(resultN, op[1]); 8 | } 9 | return resultM * resultN; 10 | } 11 | } -------------------------------------------------------------------------------- /598. Range Addition II/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxCount(self, m, n, ops): 3 | """ 4 | :type m: int 5 | :type n: int 6 | :type ops: List[List[int]] 7 | :rtype: int 8 | """ 9 | result_m = m 10 | result_n = n 11 | for op in ops: 12 | result_m = min(result_m, op[0]) 13 | result_n = min(result_n, op[1]) 14 | return result_m * result_n 15 | -------------------------------------------------------------------------------- /611. Valid Triangle Number/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def triangleNumber(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | result = 0 8 | nums.sort() 9 | for i in range(2, len(nums)): 10 | first = 0 11 | second = i - 1 12 | while first < second: 13 | if nums[first] + nums[second] > nums[i]: 14 | result += second - first 15 | second -= 1 16 | else: 17 | first += 1 18 | return result 19 | -------------------------------------------------------------------------------- /617. Merge Two Binary Trees/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mergeTrees(self, t1, t2): 3 | """ 4 | :type t1: TreeNode 5 | :type t2: TreeNode 6 | :rtype: TreeNode 7 | """ 8 | if t1 is not None and t2 is not None: 9 | t1.left = self.mergeTrees(t1.left, t2.left) 10 | t1.right = self.mergeTrees(t1.right, t2.right) 11 | t1.val += t2.val 12 | return t1 13 | return t1 if t2 is None else t2 14 | -------------------------------------------------------------------------------- /621. Task Scheduler/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int leastInterval(vector &tasks, int n) { 4 | int letters[26] = {0}; 5 | for (char letter : tasks) { 6 | letters[letter - 'A']++; 7 | } 8 | sort(begin(letters), end(letters)); 9 | int i = 25; 10 | while (letters[i] == letters[25]) { 11 | i--; 12 | } 13 | return max((int)tasks.size(), (letters[25] - 1) * (n + 1) + 25 - i); 14 | } 15 | }; -------------------------------------------------------------------------------- /621. Task Scheduler/Java/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int leastInterval(char[] tasks, int n) { 3 | int[] letters = new int[26]; 4 | for (char letter : tasks) { 5 | letters[letter - 'A']++; 6 | } 7 | Arrays.sort(letters); 8 | int i = 25; 9 | while (letters[i] == letters[25]) { 10 | i--; 11 | } 12 | return Math.max(tasks.length, (letters[25] - 1) * (n + 1) + 25 - i); 13 | } 14 | } -------------------------------------------------------------------------------- /621. Task Scheduler/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def leastInterval(self, tasks, n): 3 | """ 4 | :type tasks: List[str] 5 | :type n: int 6 | :rtype: int 7 | """ 8 | letters = [0] * 26 9 | for letter in tasks: 10 | letters[ord(letter) - ord('A')] += 1 11 | letters.sort(reverse=True) 12 | i = 0 13 | while letters[i] == letters[0]: 14 | i += 1 15 | return max(len(tasks), (letters[0] - 1) * (n + 1) + i) 16 | -------------------------------------------------------------------------------- /633. Sum of Square Numbers/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool judgeSquareSum(int c) { 4 | int left = 0; 5 | int right = (int) sqrt(c); 6 | while (left <= right) { 7 | int current = left * left + right * right; 8 | if (current == c) { 9 | return true; 10 | } 11 | if (current < c) { 12 | left++; 13 | } else { 14 | right--; 15 | } 16 | } 17 | return false; 18 | } 19 | }; -------------------------------------------------------------------------------- /633. Sum of Square Numbers/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean judgeSquareSum(int c) { 3 | int left = 0; 4 | int right = (int) Math.sqrt(c); 5 | while (left <= right) { 6 | int current = left * left + right * right; 7 | if (current == c) { 8 | return true; 9 | } else if (current < c) { 10 | left++; 11 | } else { 12 | right--; 13 | } 14 | } 15 | return false; 16 | } 17 | } -------------------------------------------------------------------------------- /633. Sum of Square Numbers/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def judgeSquareSum(self, c): 3 | """ 4 | :type c: int 5 | :rtype: bool 6 | """ 7 | left = 0 8 | right = int(c ** 0.5) 9 | while left <= right: 10 | current = left * left + right * right 11 | if current == c: 12 | return True 13 | elif current < c: 14 | left += 1 15 | else: 16 | right -= 1 17 | return False 18 | -------------------------------------------------------------------------------- /643. Maximum Average Subarray I/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double findMaxAverage(vector &nums, int k) { 4 | int i = 0; 5 | long sum = 0; 6 | long maxSum = LONG_MIN; 7 | for (int j = 0; j < nums.size(); j++) { 8 | sum += nums[j]; 9 | if (j - i == k - 1) { 10 | maxSum = max(maxSum, sum); 11 | sum -= nums[i]; 12 | i++; 13 | } 14 | } 15 | return (double) maxSum / k; 16 | } 17 | }; -------------------------------------------------------------------------------- /643. Maximum Average Subarray I/Java/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public double findMaxAverage(int[] nums, int k) { 3 | int i = 0; 4 | long sum = 0; 5 | long maxSum = Long.MIN_VALUE; 6 | for (int j = 0; j < nums.length; j++) { 7 | sum += nums[j]; 8 | if (j - i == k - 1) { 9 | maxSum = Math.max(maxSum, sum); 10 | sum -= nums[i]; 11 | i++; 12 | } 13 | } 14 | return (double) maxSum / k; 15 | } 16 | } -------------------------------------------------------------------------------- /643. Maximum Average Subarray I/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMaxAverage(self, nums, k): 3 | """ 4 | :type nums: List[int] 5 | :type k: int 6 | :rtype: float 7 | """ 8 | i = 0 9 | sum = 0 10 | max_sum = -sys.maxsize 11 | for j in range(len(nums)): 12 | sum += nums[j] 13 | if j - i == k - 1: 14 | max_sum = max(max_sum, sum) 15 | sum -= nums[i] 16 | i += 1 17 | return max_sum / k 18 | -------------------------------------------------------------------------------- /645. Set Mismatch/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findErrorNums(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: List[int] 6 | """ 7 | duplicate = 0 8 | miss = 0 9 | for num in nums: 10 | if nums[abs(num) - 1] < 0: 11 | duplicate = abs(num) 12 | else: 13 | nums[abs(num) - 1] *= -1 14 | for i in range(len(nums)): 15 | if nums[i] > 0: 16 | miss = i + 1 17 | return [duplicate, miss] 18 | -------------------------------------------------------------------------------- /646. Maximum Length of Pair Chain/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLongestChain(vector> &pairs) { 4 | sort(pairs.begin(), pairs.end(), [](vector &a, vector &b) { return a[1] < b[1]; }); 5 | int result = 1; 6 | int current = pairs[0][1]; 7 | for (auto &pair : pairs) { 8 | if (pair[0] > current) { 9 | current = pair[1]; 10 | result++; 11 | } 12 | } 13 | return result; 14 | } 15 | }; -------------------------------------------------------------------------------- /646. Maximum Length of Pair Chain/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findLongestChain(self, pairs): 3 | """ 4 | :type pairs: List[List[int]] 5 | :rtype: int 6 | """ 7 | pairs.sort(key=lambda x: x[1]) 8 | result = 1 9 | current = pairs[0][1] 10 | for pair in pairs: 11 | if pair[0] > current: 12 | current = pair[1] 13 | result += 1 14 | return result 15 | -------------------------------------------------------------------------------- /647. Palindromic Substrings/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSubstrings(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | self.result = 0 8 | 9 | def count(s, left, right): 10 | while left >= 0 and right < len(s) and s[left] == s[right]: 11 | self.result += 1 12 | left -= 1 13 | right += 1 14 | 15 | for i in range(len(s)): 16 | count(s, i, i) 17 | count(s, i, i + 1) 18 | return self.result 19 | -------------------------------------------------------------------------------- /650. 2 Keys Keyboard/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSteps(int n) { 4 | vector dp(n + 1, 0); 5 | for (int i = 2; i <= n; i++) { 6 | dp[i] = i; 7 | for (int j = i / 2; j > 1; j--) { 8 | if (i % j == 0) { 9 | dp[i] = dp[j] + i / j; 10 | break; 11 | } 12 | } 13 | } 14 | return dp[n]; 15 | } 16 | }; -------------------------------------------------------------------------------- /650. 2 Keys Keyboard/Java/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minSteps(int n) { 3 | int[] dp = new int[n + 1]; 4 | for (int i = 2; i <= n; i++) { 5 | dp[i] = i; 6 | for (int j = i / 2; j > 1; j--) { 7 | if (i % j == 0) { 8 | dp[i] = dp[j] + i / j; 9 | break; 10 | } 11 | } 12 | } 13 | return dp[n]; 14 | } 15 | } -------------------------------------------------------------------------------- /650. 2 Keys Keyboard/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSteps(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | dp = [i for i in range(n + 1)] 8 | dp[1] = 0 9 | for i in range(2, n + 1): 10 | for j in range(i // 2, 1, -1): 11 | if i % j == 0: 12 | dp[i] = dp[j] + i // j 13 | break 14 | return dp[n] 15 | -------------------------------------------------------------------------------- /657. Judge Route Circle/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def judgeCircle(self, moves): 3 | """ 4 | :type moves: str 5 | :rtype: bool 6 | """ 7 | return len(moves) % 2 == 0 and moves.count('U') == moves.count('D') and moves.count('L') == moves.count('R') 8 | -------------------------------------------------------------------------------- /672. Bulb Switcher II/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int flipLights(int n, int m) { 4 | if (m == 0 || n == 0) { 5 | return 1; 6 | } 7 | if (n == 1) { 8 | return 2; 9 | } 10 | if (n == 2) { 11 | if (m == 1) { 12 | return 3; 13 | } 14 | return 4; 15 | } 16 | if (m == 1) { 17 | return 4; 18 | } 19 | if (m == 2) { 20 | return 7; 21 | } 22 | return 8; 23 | } 24 | }; -------------------------------------------------------------------------------- /672. Bulb Switcher II/Java/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int flipLights(int n, int m) { 3 | if (m == 0 || n == 0) { 4 | return 1; 5 | } 6 | if (n == 1) { 7 | return 2; 8 | } 9 | if (n == 2) { 10 | if (m == 1) { 11 | return 3; 12 | } 13 | return 4; 14 | } 15 | if (m == 1) { 16 | return 4; 17 | } 18 | if (m == 2) { 19 | return 7; 20 | } 21 | return 8; 22 | } 23 | } -------------------------------------------------------------------------------- /672. Bulb Switcher II/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def flipLights(self, n, m): 3 | """ 4 | :type n: int 5 | :type m: int 6 | :rtype: int 7 | """ 8 | if m == 0 or n == 0: 9 | return 1 10 | if n == 1: 11 | return 2 12 | if n == 2: 13 | if m == 1: 14 | return 3 15 | return 4 16 | if m == 1: 17 | return 4 18 | if m == 2: 19 | return 7 20 | return 8 21 | -------------------------------------------------------------------------------- /674. Longest Continuous Increasing Subsequence/Java/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findLengthOfLCIS(int[] nums) { 3 | if (nums.length == 0) { 4 | return 0; 5 | } 6 | int count = 1; 7 | int result = 1; 8 | for (int i = 1; i < nums.length; i++) { 9 | if (nums[i] > nums[i - 1]) { 10 | count++; 11 | } else { 12 | result = Math.max(result, count); 13 | count = 1; 14 | } 15 | } 16 | result = Math.max(result, count); 17 | return result; 18 | } 19 | } -------------------------------------------------------------------------------- /674. Longest Continuous Increasing Subsequence/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findLengthOfLCIS(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | if len(nums) == 0: 8 | return 0 9 | count = 1 10 | result = 1 11 | for i in range(1, len(nums)): 12 | if nums[i] > nums[i - 1]: 13 | count += 1 14 | else: 15 | result = max(result, count) 16 | count = 1 17 | result = max(result, count) 18 | return result 19 | -------------------------------------------------------------------------------- /682. Baseball Game/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def calPoints(self, ops): 3 | """ 4 | :type ops: List[str] 5 | :rtype: int 6 | """ 7 | history = [] 8 | for op in ops: 9 | if op == '+': 10 | history.append(history[-1] + history[-2]) 11 | elif op == 'D': 12 | history.append(history[-1] * 2) 13 | elif op == 'C': 14 | history.pop() 15 | else: 16 | history.append(int(op)) 17 | return sum(history) 18 | -------------------------------------------------------------------------------- /686. Repeated String Match/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int repeatedStringMatch(string A, string B) { 4 | string temp = ""; 5 | int result = 0; 6 | while (temp.length() < B.length()) { 7 | temp += A; 8 | result++; 9 | } 10 | if (temp.find(B) != string::npos) { 11 | return result; 12 | } 13 | temp += A; 14 | result++; 15 | if (temp.find(B) != string::npos) { 16 | return result; 17 | } 18 | return -1; 19 | } 20 | }; -------------------------------------------------------------------------------- /686. Repeated String Match/Java/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int repeatedStringMatch(String A, String B) { 3 | StringBuilder builder = new StringBuilder(); 4 | int result = 0; 5 | while (builder.length() < B.length()) { 6 | builder.append(A); 7 | result++; 8 | } 9 | if (builder.toString().contains(B)) { 10 | return result; 11 | } 12 | builder.append(A); 13 | result++; 14 | if (builder.toString().contains(B)) { 15 | return result; 16 | } 17 | return -1; 18 | } 19 | } -------------------------------------------------------------------------------- /686. Repeated String Match/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def repeatedStringMatch(self, A, B): 3 | """ 4 | :type A: str 5 | :type B: str 6 | :rtype: int 7 | """ 8 | temp = '' 9 | result = 0 10 | while len(temp) < len(B): 11 | temp += A 12 | result += 1 13 | if B in temp: 14 | return result 15 | temp += A 16 | result += 1 17 | if B in temp: 18 | return result 19 | return -1 20 | -------------------------------------------------------------------------------- /693. Binary Number with Alternating Bits/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasAlternatingBits(int n) { 4 | bool last = n & 1; 5 | while (n != 0 && (n & 1) == last) { 6 | last = 1 - last; 7 | n >>= 1; 8 | } 9 | return n == 0; 10 | } 11 | }; -------------------------------------------------------------------------------- /693. Binary Number with Alternating Bits/Java/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean hasAlternatingBits(int n) { 3 | int last = n & 1; 4 | while (n != 0 && (n & 1) == last) { 5 | last = 1 - last; 6 | n >>= 1; 7 | } 8 | return n == 0; 9 | } 10 | } -------------------------------------------------------------------------------- /693. Binary Number with Alternating Bits/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hasAlternatingBits(self, n): 3 | """ 4 | :type n: int 5 | :rtype: bool 6 | """ 7 | last = n & 1 8 | while n != 0 and n & 1 == last: 9 | last = 1 - last 10 | n >>= 1 11 | return n == 0 12 | -------------------------------------------------------------------------------- /696. Count Binary Substrings/C++/Solution.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countBinarySubstrings(string s) { 4 | int result = 0; 5 | int prevLength = 0; 6 | int curLength = 1; 7 | for (int i = 1; i < s.length(); i++) { 8 | if (s[i] == s[i - 1]) { 9 | curLength++; 10 | } else { 11 | prevLength = curLength; 12 | curLength = 1; 13 | } 14 | if (prevLength >= curLength) { 15 | result++; 16 | } 17 | } 18 | return result; 19 | } 20 | }; -------------------------------------------------------------------------------- /696. Count Binary Substrings/Java/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countBinarySubstrings(String s) { 3 | int result = 0; 4 | int prevLength = 0; 5 | int curLength = 1; 6 | for (int i = 1; i < s.length(); i++) { 7 | if (s.charAt(i) == s.charAt(i - 1)) { 8 | curLength++; 9 | } else { 10 | prevLength = curLength; 11 | curLength = 1; 12 | } 13 | if (prevLength >= curLength) { 14 | result++; 15 | } 16 | } 17 | return result; 18 | } 19 | } -------------------------------------------------------------------------------- /696. Count Binary Substrings/Python/Solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countBinarySubstrings(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | result = 0 8 | prev_length = 0 9 | cur_length = 1 10 | for i in range(1, len(s)): 11 | if (s[i] == s[i - 1]): 12 | cur_length += 1 13 | else: 14 | prev_length = cur_length 15 | cur_length = 1 16 | if prev_length >= cur_length: 17 | result += 1 18 | return result 19 | --------------------------------------------------------------------------------