摘要:
According to the highest vote in: http://stackoverflow.com/questions/1930454/what-is-a-good-solution-for-calculating-an-average-where-the-sum-of-all-v 阅读全文
摘要:
涉及到处理Date, 以及STDIN from a file 处理Date可以用SimpleDateFormat这个class static Date parseTime(String timeStr) { Date time = new Date(); DateFormat dft = new S 阅读全文
摘要:
题不难,主要是注意STDIN 阅读全文
摘要:
方法1:Time Complexity O(NK) 暂时只有两个Heap的做法,缺点:In this problem, it is necessary to be able remove elements that are not necessarily at the top of the heap 阅读全文
摘要:
Better solution: Returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int va 阅读全文
摘要:
我觉得题中应该再加上一个条件,就是guarantee是能够分割的,不然没法做. 如果总node总数是奇数的话, 怎么删都没法保证所有的子树是even number,所以这题的前提是node总数为偶数? 网上看到别人的很好的解法: 特别是用iterator.next()以后用iterator.remo 阅读全文
摘要:
Spread from center: Like LC 5: longest palindromic substring 阅读全文
摘要:
Find largest island in a board 1 package fb; 2 3 public class LargestIsland { 4 public int findLargestIsland(int[][] board) { 5 if (board==null || board.length==0 || board[0].le... 阅读全文
摘要:
tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output:7 1 package fb; 2 3 import java.util.*; 4 5 public class Scheduler { 6 7 pub... 阅读全文
摘要:
Solution:Basically, you would have a loop that traverse the cyclic sorted list and find the point where you insert the value (Let’s assume the value b 阅读全文
摘要:
refer to: http://www.stealthcopter.com/blog/2009/09/python-calculating-pi-using-random-numbers/ During my undergraduate degree I wrote a program in fo 阅读全文
摘要:
The method to serialize is like this: (1(2)(3(5)(6))(4(7))) if '(', right shift 1 position to the start of the number, use while loop to find the end 阅读全文
摘要:
referred to: https://discuss.leetcode.com/topic/69137/java-o-1-accept-solution-using-hashmap-doublelinkedlist-and-linkedhashset Two HashMaps are used, 阅读全文
摘要:
My binary encoding method gives a 8 for the first problem, however, there is a smarter method that generates a answer of 5, which can be found here: h 阅读全文
摘要:
Be careful, split() will not include trailing empty strings in the result array The string "boo:and:foo", for example, split(":")的结果是 {“boo”, "and", " 阅读全文
摘要:
DP: Initially I think of 1D DP, dp[i] stands for the shortest string of first i characters, then: dp[i] = minLen{dp[k] + encode(substring(k+1, i))} th 阅读全文
摘要:
Scan through array and DP: We iterate through each word and see if it can be formed by using other words. The subproblem is Word Break I. But it is a 阅读全文
摘要:
https://discuss.leetcode.com/topic/70706/beyond-my-knowledge-java-solution-with-in-line-explanation https://discuss.leetcode.com/topic/70664/c-7-line- 阅读全文
摘要:
这道题我一开始发现了数学规律: length of consecutive chars vs number of different substring “a” -> 1 = 1 "ab" -> 3 = 1+2 "abc" -> 6 = 1+2+3 "abcd" -> 10 = 1+2+3+4 于是 阅读全文
摘要:
目前只想出了Brute Force做法(1165ms), 看到有<20ms的做法,未深究: 阅读全文
只有注册用户登录后才能阅读该文。 阅读全文
摘要:
DFS, my solution is to fill each edge of the square one by one. DFS to construct the 1st, then 2nd, then 3rd, then 4th. For each edge I scan all the m 阅读全文
摘要:
So the first question is: what is the difference between this and #297? This here is BST, however, in #297, it's BT. "The encoded string should be as 阅读全文
摘要:
注意The loop must be "forward" or "backward'. 所以这就是为什么[-2, 1, -1, -2, -2]是false的原因 Just think it as finding a loop in Linkedlist, except that loops with 阅读全文
摘要:
Topological Sort: This problem is to determine if there's one, and only one sequence to sort a DAG. The method is to check if the queue's size is alwa 阅读全文
摘要:
My First Solution: Use Stack and String operation, from the back of the string, find the first '?', push the right to stack. Depends on whether the ch 阅读全文
摘要:
Backtracking + Trie: referred to https://discuss.leetcode.com/topic/63516/explained-my-java-solution-using-trie-126ms-16-16 A better approach is to ch 阅读全文
摘要:
This is a 0/1 backpacking problem The problem can be interpreted as: What's the max number of str can we pick from strs with limitation of m "0"s and 阅读全文
摘要:
Binary Search My solution: Be careful in my binary search function, l, r may go out of the range of the array Solution with the highest vote: 阅读全文
摘要:
Example: 0 0 0 0 0 1 0 0 1 1 1 0 0 0 1 0 Total Hamming Distance = (3*1) + (2*2) + (2*2) + (4*0) = 11 so the idea is count the number of 1 and 0 on eac 阅读全文
摘要:
Solution 1: Solution 2: 阅读全文
摘要:
这题看起来简单但是其实很容易写错 本来想j 不从0开始,而是从 i+1开始检查,可以节省时间,但是一些为Null的情况会使讨论很复杂 比如 阅读全文
摘要:
先来一个brute force, 类似Text Adjustment 但是在稍微大一点的case就TLE了,比如: ["a","b","e"] 20000 20000, 花了465ms 所以想想怎么节约时间, 提示是可以DP的,想想怎么复用,refer to: https://discuss.lee 阅读全文
摘要:
Refer to https://discuss.leetcode.com/topic/61799/java-bit-mask-dfs-with-pruning bit mask refer to http://bookshadow.com/weblog/2016/10/02/leetcode-mi 阅读全文
摘要:
my HashSet+ ArrayList, 删除的时候把要删的index与末尾对调。get()其实不需要random, 因为anyone is ok HashSet+ Queue网上vote最高的solution, 阅读全文
摘要:
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word",... 阅读全文
摘要:
Time Complexity: O(N+K), Space: O(1) 阅读全文
摘要:
Better Solution: https://discuss.leetcode.com/topic/49194/10-lines-simple-java-solution-using-recursion-with-explanation/2 For this question we need t 阅读全文
摘要:
Use Queue Better Solution: can solve follow up, refer to https://discuss.leetcode.com/topic/48758/super-easy-design-o-1-hit-o-s-gethits-no-fancy-data- 阅读全文
摘要:
Walk through the matrix. At the start of each non-wall-streak (row-wise or column-wise), count the number of hits in that streak and remember it. For 阅读全文