🌓

97. Interleaving String

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
class Solution {
private Boolean[][] memo;

public boolean isInterleave(String s1, String s2, String s3) {
if (s3.length() != s1.length() + s2.length()) {
return false;
}
memo = new Boolean[s1.length() + 1][s2.length() + 1];
return helper(s1, s2, s3, 0, 0);
}

private boolean helper(String s1, String s2, String s3, int pos1, int pos2) {
if (memo[pos1][pos2] != null) {
return memo[pos1][pos2];
}
if (pos1 == s1.length()) {
return memo[pos1][pos2] = s2.substring(pos2).equals(s3.substring(s3.length() - s2.length() + pos2));
}
if (pos2 == s2.length()) {
return memo[pos1][pos2] = s1.substring(pos1).equals(s3.substring(s3.length() - s1.length() + pos1));
}
if (s1.charAt(pos1) == s3.charAt(pos1 + pos2)) {
if (helper(s1, s2, s3, pos1 + 1, pos2)) {
return memo[pos1][pos2] = true;
}
}
if (s2.charAt(pos2) == s3.charAt(pos1 + pos2)) {
if (helper(s1, s2, s3, pos1, pos2 + 1)) {
return memo[pos1][pos2] = true;
}
}
return memo[pos1][pos2] = false;
}

}

Read More

912. Sort an Array

sort大合集.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class Solution {
public int[] sortArray(int[] nums) {
mergeSort(nums, nums.clone(), 0, nums.length - 1);
return nums;
}

private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}

// Bubble Sort 最大值到最后一个位置, 次大到倒数第二个...
public void bubbleSort(int[] nums) {
int inPlaceCount = 0;
// We have nums.length numbers to process in order to let each of them be in
// the right place
for (int i = 0; i < nums.length - 1; i++) {
boolean isSorted = true;
// We don't bother the nums that are in place which is the last
// "inPlaceCount" nums.
for (int j = 0; j < nums.length - inPlaceCount - 1; j++) {
if (nums[j] > nums[j + 1]) {
isSorted = false;
swap(nums, j, j + 1);
}
}
// End the outer for loop earlier.
if (isSorted)
break;
inPlaceCount += 1;
}
return nums;
}

// Selection Sort 第0个位置最小的坐,第1个位置次小的坐...
public void selectionSort(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
int minIdx = i;
for (int j = i; j < nums.length; j++) {
if (nums[j] < nums[minIdx]) {
minIdx = j;
}
}
swap(nums, minIdx, i);
}
return nums;
}

// Insertion Sort 当前位置的值比前一个小,那就换到前面,一直到换不动为止
public void insertionSort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
for (int j = i; j > 0; j--) {
if (nums[j] < nums[j - 1]) {
swap(nums, j, j - 1);
} else {
break;
}
}
}
return nums;
}

// Quick Sort
private void quickSort(int[] nums, int start, int end) {
if (start >= end) {
return;
}
int pivot = start, left = start + 1, right = end;
while (left <= right) {
if (nums[left] > nums[pivot] && nums[right] < nums[pivot]) {
swap(nums, left, right);
left += 1;
right -= 1;
continue;
}
if (nums[left] <= nums[pivot]) {
left += 1;
}
if (nums[right] >= nums[pivot]) {
right -= 1;
}
}
swap(nums, pivot, right);
boolean isLeftShorter = right - start <= end - right ? true : false;
if (isLeftShorter) {
quickSort(nums, start, right - 1);
quickSort(nums, right + 1, end);
} else {
quickSort(nums, right + 1, end);
quickSort(nums, start, right - 1);
}
}

// Merge Sort 给一个范围, 能把这个范围内的数字给sort好. 注意nums和aux往下传递的时候要颠倒位置.
private void mergeSort(int[] nums, int[] aux, int start, int end) {
if (start >= end) {
return;
}
int mid = start + (end - start) / 2;
mergeSort(aux, nums, start, mid);
mergeSort(aux, nums, mid + 1, end);
merge(nums, aux, start, mid, end);
}

private void merge(int[] nums, int[] aux, int left, int middle, int right) {
int ptr = left, ptrOne = left, ptrTwo = middle + 1;
while (ptrOne <= middle && ptrTwo <= right) {
if (aux[ptrOne] <= aux[ptrTwo]) {
nums[ptr++] = aux[ptrOne++];
} else {
nums[ptr++] = aux[ptrTwo++];
}
}
while (ptrOne <= middle) {
nums[ptr++] = aux[ptrOne++];
}
while (ptrTwo <= right) {
nums[ptr++] = aux[ptrTwo++];
}
}
}

Read More

398. Random Pick Index

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
private Map<Integer, List<Integer>> numIdxMap;
private Random rand;

public Solution(int[] nums) {
numIdxMap = new HashMap<>();
rand = new Random();
for (int i = 0; i < nums.length; i++) {
numIdxMap.putIfAbsent(nums[i], new ArrayList<>());
numIdxMap.get(nums[i]).add(i);
}
}

public int pick(int target) {
List<Integer> targetIdxList = numIdxMap.get(target);
return targetIdxList.get(rand.nextInt(targetIdxList.size()));
}
}

Read More

336. Palindrome Pairs

1
System.out.println("Hello, World!");

Read More

303. Range Sum Query - Immutable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class NumArray {
private int[] prefixArray;

public NumArray(int[] nums) {
int sum = 0;
prefixArray = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
prefixArray[i] = sum;
}
}

public int sumRange(int left, int right) {
return left == 0 ? prefixArray[right] : prefixArray[right] - prefixArray[left - 1];
}
}

Read More

120. Triangle

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 {
public int minimumTotal(List<List<Integer>> triangle) {
if (triangle.size() == 0 || triangle.get(0).size() == 0) {
return 0;
}
Deque<Pair<Integer, Integer>> indexQ = new ArrayDeque<>();
indexQ.offer(new Pair(0, triangle.get(0).get(0)));
int level = 0;
for (level = 0; level < triangle.size() - 1; level++) {
int size = indexQ.size();
for (int i = 0; i < size; i++) {
Pair<Integer, Integer> currPair = indexQ.poll();
int currIndex = currPair.getKey();
int sumSoFar = currPair.getValue();
if (i > 0) {
Pair<Integer, Integer> previousPair = indexQ.peekLast();
if (sumSoFar + triangle.get(level + 1).get(currIndex) < previousPair.getValue()) {
indexQ.pollLast();
indexQ.offer(new Pair(currIndex, sumSoFar + triangle.get(level + 1).get(currIndex)));
}
} else {
indexQ.offer(new Pair(currIndex, sumSoFar + triangle.get(level + 1).get(currIndex)));
}
indexQ.offer(new Pair(currIndex + 1, sumSoFar + triangle.get(level + 1).get(currIndex + 1)));
}
}
int min = Integer.MAX_VALUE;
while (!indexQ.isEmpty()) {
min = Math.min(min, indexQ.poll().getValue());
}
return min;
}
}

Read More

92. Reverse Linked List II

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
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy, ptr = dummy;
for (int i = 0; i < right - left + 2; i++) {
ptr = ptr.next;
}
for (int i = 0; i < left - 1; i++) {
prev = prev.next;
ptr = ptr.next;
}
ListNode start = prev.next;
prev.next = reverse(start, right - left + 1);
start.next = ptr;
return dummy.next;
}

private ListNode reverse(ListNode node, int steps) {
ListNode prev = null, curr = node;
for (int i = 0; i < steps; i++) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
}

Read More

89. Gray Code

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
class Solution {
public List<Integer> grayCode(int n) {
List<Integer> ans = new ArrayList<>();
Set<Integer> visited = new HashSet<>();
ans.add(0);
visited.add(0);
backtrack(0, n, visited, ans);
return ans;
}

private boolean backtrack(int lastElement, int n, Set<Integer> visited, List<Integer> ans) {
if (ans.size() == Math.pow(2, n)) {
return true;
}
for (int i = 0; i < n; i++) {
int currElement = ans.size() == Math.pow(2, n) - 1 ? ans.get(0) ^ (1 << i) : lastElement ^ (1 << i);
if (!visited.contains(currElement)) {
visited.add(currElement);
ans.add(currElement);
if (backtrack(currElement, n, visited, ans)) {
return true;
}
visited.remove(currElement);
ans.remove(ans.size() - 1);
}
}
return false;
}
}

Read More

86. Partition List

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 {
public ListNode partition(ListNode head, int x) {
if (head == null || head.next == null) {
return head;
}
ListNode list1 = new ListNode(0);
ListNode list2 = new ListNode(0);
ListNode ptr1 = list1, ptr2 = list2, ptr3 = head;
while (ptr3 != null) {
if (ptr3.val < x) {
ptr1.next = ptr3;
ptr1 = ptr1.next;
} else {
ptr2.next = ptr3;
ptr2 = ptr2.next;
}
ptr3 = ptr3.next;
}
ptr1.next = list2.next;
ptr2.next = null;
return list1.next;
}
}

Read More

82. Remove Duplicates from Sorted List II

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(0);
ListNode curr = head, ptr = head, prev = dummy;
dummy.next = head;
while (curr != null) {
int count = 0;
while (ptr != null && ptr.val == curr.val) {
ptr = ptr.next;
count += 1;
}
if (count > 1) {
prev.next = ptr;
} else {
prev = curr;
}
curr = ptr;
}
return dummy.next;
}
}

Read More