Given an input string, reverse the string word by word.
For example,
Given s = "
return "
Given s = "
the sky is blue
",return "
blue is sky the
".
Solution:O(n)
the sky is blue
",blue is sky the
".+
, -
, *
, /
. Each operand may be an integer or another expression.["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6Solution:O(n)
get
and set
.get(key)
- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)
- Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.{1,#,2,3}
,1 \ 2 / 3
[3,2,1]
.{1,#,2,3}
,1 \ 2 / 3
[1,2,3]
.null
.{1,2,3,4}
, reorder it to {1,4,2,3}
."catsanddog"
,["cat", "cats", "and", "sand", "dog"]
.["cats and dog", "cat sand dog"]
."leetcode"
,["leet", "code"]
."leetcode"
can be segmented as "leet code"
.gas[i]
.cost[i]
of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.label
and a list of its neighbors
.#
as a separator for each node, and ,
as a separator for node label and each neighbor of the node.{0,1,2#1,2#2,2}
.#
.0
. Connect node 0
to both nodes 1
and 2
.1
. Connect node 1
to node 2
.2
. Connect node 2
to node 2
(itself), thus forming a self-cycle.1 / \ / \ 0 --- 2 / \ \_/
"aab"
,1
since the palindrome partitioning ["aa","b"]
could be produced using 1 cut."aab"
,[ ["aa","b"], ["a","a","b"] ]Solution:O(n)
'X'
and 'O'
, capture all regions surrounded by 'X'
.'O'
s into 'X'
s in that surrounded region.X X X X X O O X X X O X X O X X
X X X X X X X X X X X X X O X XSolution:O(m*n)
0-9
only, each root-to-leaf path could represent a number.1->2->3
which represents the number 123
.1 / \ 2 3
1->2
represents the number 12
.1->3
represents the number 13
.25
.[100, 4, 200, 1, 3, 2]
,[1, 2, 3, 4]
. Return its length: 4
."hit"
"cog"
["hot","dot","dog","lot","log"]
[ ["hit","hot","dot","dog","cog"], ["hit","hot","lot","log","cog"] ]
"hit"
"cog"
["hot","dot","dog","lot","log"]
"hit" -> "hot" -> "dot" -> "dog" -> "cog"
,5
."A man, a plan, a canal: Panama"
is a palindrome."race a car"
is not a palindrome.1 / \ 2 3
6
.[ [2], [3,4], [6,5,7], [4,1,8,3] ]
11
(i.e., 2 + 3 + 5 + 1 = 11).1 / \ 2 3 / \ \ 4 5 7
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULLSolution:O(n)
[1,3,3,1]
.[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]Solution:O(n^2)
struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }
NULL
.NULL
.1 / \ 2 3 / \ / \ 4 5 6 7
1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULLSolution:O(n)
"ACE"
is a subsequence of "ABCDE"
while "AEC"
is not)."rabbbit"
, T = "rabbit"
3
.1 / \ 2 5 / \ \ 3 4 6
1 \ 2 \ 3 \ 4 \ 5 \ 6