🌓

136. Single Number

1
2
3
4
5
6
7
8
9
class Solution {
public int singleNumber(int[] nums) {
int ans = 0;
for (int i = 0; i < nums.length; i++) {
ans ^= nums[i];
}
return ans;
}
}

Read More

134. Gas Station

while中套for循环, for循环走的条件是走一圈.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int pos = 0;
while (pos < gas.length) {
int currTank = 0;
int newPos = 0;
for (int i = 0; i < gas.length; i++) {
int currentPos = pos + i;
if (currentPos >= gas.length)
currentPos %= gas.length;
if (currTank < 0) {
newPos = currentPos;
break;
}
currTank = currTank + gas[currentPos] - cost[currentPos];
}
if (currTank >= 0)
return pos;
if (newPos <= pos)
break;
pos = newPos;
currTank = 0;
}
return -1;
}
}

Read More

128. Longest Consecutive Sequence

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int longestConsecutive(int[] nums) {
if (nums == null || nums.length == 0)
return 0;
Set<Integer> set = new HashSet<>();
for (int i : nums)
set.add(i);
int ans = 0;
for (int num : nums) {
int left = num - 1;
int right = num + 1;
while (set.remove(left))
left--;
while (set.remove(right))
right++;
ans = Math.max(ans, right - left - 1);
if (set.isEmpty())
return ans;// save time if there are items in nums, but no item in hashset.
}
return ans;
}
}

Read More

108. Convert Sorted Array to Binary Search Tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
static class Node {
int low, up;
TreeNode node;

Node(int low, int up, TreeNode node) {
this.low = low;
this.up = up;
this.node = node;
}
}

public TreeNode sortedArrayToBST(int[] nums) {
TreeNode root = new TreeNode();
Stack<Node> stack = new Stack<>();
stack.push(new Node(0, nums.length - 1, root));
while (!stack.isEmpty()) {
Node currNode = stack.pop();
int middle = currNode.low + (currNode.up - currNode.low) / 2;
currNode.node.val = nums[middle];
if (currNode.low <= middle - 1) {
currNode.node.left = new TreeNode();
stack.push(new Node(currNode.low, middle - 1, currNode.node.left));
}

if (currNode.up >= middle + 1) {
currNode.node.right = new TreeNode();
stack.push(new Node(middle + 1, currNode.up, currNode.node.right));
}
}
return root;
}
}

Read More

106. Construct Binary Tree from Inorder and Postorder Traversal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
private int pos;

public TreeNode buildTree(int[] inorder, int[] postorder) {
pos = postorder.length - 1;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i], i);
}
return helper(map, 0, inorder.length - 1, postorder);
}

private TreeNode helper(Map<Integer, Integer> map, int iStart, int iEnd, int[] postorder) {
if (iStart > iEnd)
return null;
TreeNode newNode = new TreeNode(postorder[pos]);
int rootIdx = map.get(postorder[pos]);
pos -= 1;
newNode.right = helper(map, rootIdx + 1, iEnd, postorder);
newNode.left = helper(map, iStart, rootIdx - 1, postorder);
return newNode;
}
}

Read More

105. Construct Binary Tree from Preorder and Inorder Traversal

这题是真的经典. 精髓在于如何知道某个node有没有左树? 看它的在inorder的位置左边有没有node, 当然是还有没被生成的node. 如何知道某个node有没有右树? 看它在inorder中它和它的parent之间有没有node. 这两句话是精髓. 和serial binary tree那道题放在一起看.

Read More

101. Symmetric Tree

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public boolean isSymmetric(TreeNode root) {
return helper(root.left, root.right);
}

private boolean helper(TreeNode p, TreeNode q) {
if (p == null || q == null)
return p == q;
return p.val == q.val && helper(p.left, q.right) && helper(p.right, q.left);
}
}

Read More

100. Same Tree

1
2
3
4
5
6
7
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null || q == null)
return p == q;
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}

Read More

78. Subsets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> currentSet = new ArrayList<>();
helper(currentSet, ans, nums, 0);
return ans;
}

private void helper(List<Integer> currentSet, List<List<Integer>> ans, int[] nums, int pos) {
if (pos == nums.length) {
ans.add(new ArrayList<>(currentSet));
return;
}
currentSet.add(nums[pos]);
helper(currentSet, ans, nums, pos + 1);
currentSet.remove(currentSet.size() - 1);

helper(currentSet, ans, nums, pos + 1);
}

}

Read More

Sliding Window

76题的题解就是sliding window的模板.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
public String minWindow(String s, String t) {
if (s == null || t == null || s.length() == 0 || t.length() == 0 || s.length() < t.length())
return "";
Map<Character, Integer> charCount = new HashMap<>();
for (int i = 0; i < t.length(); i++) {
charCount.put(t.charAt(i), charCount.getOrDefault(t.charAt(i), 0) + 1);
}
int left = 0, right = 0, count = 0, minLeft = 0, minRight = 0, min = Integer.MAX_VALUE;

while (right < s.length()) {
// Move right pointer.
while (right < s.length()) {
if (charCount.containsKey(s.charAt(right))) {
charCount.put(s.charAt(right), charCount.get(s.charAt(right)) - 1);
if (charCount.get(s.charAt(right)) >= 0) {
count += 1;
}
}
if (count == t.length())
break;
right += 1;
}

// Test if out of bound.
if (right == s.length())
break;

// Move left pointer until not all chars in t are included in the window.
// left <= right可要可不要.
while (count == t.length() && left <= right) {
if (charCount.containsKey(s.charAt(left))) {
charCount.put(s.charAt(left), charCount.get(s.charAt(left)) + 1);
if (charCount.get(s.charAt(left)) > 0) {
count -= 1;
}
}
left += 1;
}

// Check the length.
if (right - (left - 1) + 1 < min) {
minLeft = left - 1;
minRight = right + 1;
min = right - (left - 1) + 1;
}

right += 1;
}
return min != Integer.MAX_VALUE ? s.substring(minLeft, minRight) : "";
}
}

Read More