Plus One
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
Solution
Just find the last 9…
|
|
Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.
For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.
Note: The result may be very large, so you need to return a string instead of an integer.
Solution
Rewirte comparator (to desc), sort and add.
|
|
Pow(x, n)
Implement pow(x, n).
Recursive Solution
|
|
Sqrt(x)
Implement int sqrt(int x).
Compute and return the square root of x.
Solution
Beware of overflow problems…
mid*mid could overflow, instead of using long, compare mid with x/mid
but now we have to treat 0 and 1 as special cases…
|
|
Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
push(x) — Push element x onto stack.
pop() — Removes the element on top of the stack.
top() — Get the top element.
getMin() — Retrieve the minimum element in the stack.
Solution
Just use an extra stack to keep track of the min values…
|
|
Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,
Given:
start = “hit”
end = “cog”
dict = [“hot”,”dot”,”dog”,”lot”,”log”]
As one shortest transformation is “hit” -> “hot” -> “dot” -> “dog” -> “cog”,
return its length 5.
Note:
Return 0 if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
BFS Search Solution
Naive approach might not give the shortest length, this problem is equavlent to finding the shortes path between start and end characters, in a graph where chars in dict are vertexes and vertexes with one char difference are connected.
Few more details to attend to:
length should start at 1, and return length+1
queue.size() and q.poll() is changing within each loop, so keep track of them
|
|
Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:
Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,
Given:
start = “hit”
end = “cog”
dict = [“hot”,”dot”,”dog”,”lot”,”log”]
Return
|
|
Note:
All words have the same length.
All words contain only lowercase alphabetic characters.
人家的解法都看晕了…
|
|