随笔分类 - Leetcode
Leetcode: Reconstruct Original Digits from English
摘要:# of '0': # of 'z' # of '2': # of 'w' 4: u 6: x 8: g 3: h - 8 5: f - 4 7: s - 6 1: o - 0 - 2 - 4 9: i - 5 - 6 - 8 我的code用了一个数组来存char count
阅读全文
Leetcode: Maximum XOR of Two Numbers in an Array
摘要:Solution 1: Bit Manipulation: 这题真心有点难,get the max XOR bit by bit note that if A^B=C, then A^C=B, B^C=A Solution 2: Trie, (未研究) https://discuss.leetcod
阅读全文
Leetcode: Find All Anagrams in a String
摘要:Time Complexity will be O(n) because the "start" and "end" points will only move from left to right once. Sliding Window: Use a count to denote the di
阅读全文
Leetcode: Pacific Atlantic Water Flow
摘要:这题考点在于需要设置两个visited数组 Two Queue and add all the Pacific border to one queue; Atlantic border to another queue. Keep a visited matrix for each queue. I
阅读全文
Leetcode: Partition Equal Subset Sum
摘要:Backpack problem: dp[i][j] means if the first i elements can sum up to value j dp[i][j] = dp[i-1][j] || (j>=nums[i-1] && dp[i-1][j-nums[i-1]]) the res
阅读全文
Leetcode: Arithmetic Slices
摘要:A slice (P, Q)其实就是从P到Q的序列,(P,Q)是arithmetic slice要求是等差数列且至少有三个数,问array里面有多少个这样的(P, Q) 这道题难点在于想到用DP,一旦想到用DP,就迎刃而解 dp[i]表示以i结束的slice有多少个
阅读全文
Leetcode: Trapping Rain Water II
摘要:The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] before the rain. After the rain, water are trapped between th
阅读全文
Leetcode: Remove K Digits
摘要:Greedy + Stack: 用一个栈维护最后要留存下来的digits 需要注意的是:如果遍历到string的某个char, string后面的char数刚刚好能填满这个栈,那么即使这个char比栈顶还要小,也不出栈,直接入栈 最后要删除leading 0 用char[]实现栈,思路一样,要快很多
阅读全文
Leetcode: Add Strings
摘要:Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is =0 || j>=0 || carry!=0) { 8 int sum = 0; ...
阅读全文
Leetcode: Longest Palindrome
摘要:Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not cons...
阅读全文
Leetcode: Queue Reconstruction by Height
摘要:refer to: https://discuss.leetcode.com/topic/60394/easy-concept-with-python-c-java-solution E.g.input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]subar
阅读全文
Leetcode: Frog Jump
摘要:Solution 1: Backtracking, but will TLE Better Solution: DP (refer to https://discuss.leetcode.com/topic/59903/very-easy-to-understand-java-solution-wi
阅读全文
Leetcode: Binary Watch
摘要:Solution 1: Bit Manipulation use Integer.bitCount() Solution 2: Backtracking, 非常精妙之处在于用了两个数组来帮助generate digit(例如:1011 -> 11)
阅读全文
Leetcode: Evaluate Division
摘要:Graph, DFS (1) Build the map, the key is dividend, the value is also a map whose key is divisor and value is its parameter. For example, a / b = 2.0,
阅读全文
Leetcode: Integer Replacement
摘要:Refer to: https://discuss.leetcode.com/topic/58334/a-couple-of-java-solutions-with-explanations/2 When to add 1 instead of minus 1, here is an example
阅读全文
浙公网安备 33010602011771号