Skip to content

Commit 6846702

Browse files
committed
update
1 parent 42dbf31 commit 6846702

23 files changed

+458
-55
lines changed

leetcode_solved/[editing]leetcode_0080_Remove_Duplicates_from_Sorted_Array_II.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0082_Remove_Duplicates_from_Sorted_List_II.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0189_Rotate_Array.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0223_Rectangle_Area.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0739_Daily_Temperatures.cpp

-6
This file was deleted.

leetcode_solved/[editing]leetcode_0763_Partition_Labels.cpp

-6
This file was deleted.

leetcode_solved/[editing]leetcode_0933_Number_of_Recent_Calls.cpp

-16
This file was deleted.

leetcode_solved/[editing]leetcode_1079_Letter_Tile_Possibilities.cpp

-6
This file was deleted.

leetcode_solved/[editing]leetcode_1305_All_Elements_in_Two_Binary_Search_Trees.cpp

-15
This file was deleted.

leetcode_solved/[editing]leetcode_1329_Sort_the_Matrix_Diagonally.cpp

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// AC: Runtime: 1 ms, faster than 30.57% of Java online submissions for Remove Duplicates from Sorted Array II.
2+
// Memory Usage: 38.9 MB, less than 85.24% of Java online submissions for Remove Duplicates from Sorted Array II.
3+
// since the nums[i] ∈ [-10^4, 10^4], if we find three or above duplicates, we sign it as -10001, then move the element in O(n) complexity.
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int removeDuplicates(int[] nums) {
8+
int size = nums.length, dup = 0, tempCount = 1, prev = -10001;
9+
if (size > 1) {
10+
for (int i = 1; i < size; i++) {
11+
if (nums[i] == nums[i - 1] || nums[i] == prev) {
12+
tempCount++;
13+
prev = nums[i];
14+
if (tempCount >= 3) {
15+
dup++;
16+
// sign as a unique num.
17+
nums[i] = -10001;
18+
}
19+
} else {
20+
prev = -10001;
21+
tempCount = 1;
22+
}
23+
}
24+
25+
int pos = 0;
26+
for (int i = 0; i < size; i++, pos++) {
27+
if (nums[i] == -10001) {
28+
while (pos < size && nums[pos] == -10001) {
29+
pos++;
30+
}
31+
if (pos >= size) {
32+
break;
33+
}
34+
nums[i] = nums[pos];
35+
nums[pos] = -10001;
36+
}
37+
}
38+
}
39+
40+
return size - dup;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// AC: Runtime: 1 ms, faster than 27.17% of Java online submissions for Remove Duplicates from Sorted List II.
2+
// Memory Usage: 38.6 MB, less than 34.10% of Java online submissions for Remove Duplicates from Sorted List II.
3+
// .
4+
// T:O(n), S:O(n)
5+
//
6+
class Solution {
7+
public ListNode deleteDuplicates(ListNode head) {
8+
HashSet<Integer> dup = new HashSet<>();
9+
ListNode headCopy = head;
10+
int prev = Integer.MIN_VALUE;
11+
while (headCopy != null) {
12+
if (headCopy.val == prev) {
13+
dup.add(headCopy.val);
14+
}
15+
prev = headCopy.val;
16+
headCopy = headCopy.next;
17+
}
18+
19+
ListNode headCopy2 = head, prevNode = head;
20+
while (headCopy2 != null && headCopy2.next != null) {
21+
if (dup.contains(headCopy2.val)) {
22+
headCopy2.val = headCopy2.next.val;
23+
headCopy2.next = headCopy2.next.next;
24+
} else {
25+
prevNode = headCopy2;
26+
headCopy2 = headCopy2.next;
27+
}
28+
}
29+
// handle the head pointer itself
30+
if (headCopy2 != null && dup.contains(headCopy2.val)) {
31+
if (headCopy2 == head) {
32+
return null;
33+
}
34+
prevNode.next = null;
35+
}
36+
37+
return head;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// AC: Runtime: 0 ms, faster than 100.00% of Java online submissions for Rotate Array.
2+
// Memory Usage: 56.2 MB, less than 55.69% of Java online submissions for Rotate Array.
3+
// a reverse trick, to remember.
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public void rotate(int[] nums, int k) {
8+
int size = nums.length;
9+
k = k % size;
10+
reverse(nums, 0, size - 1);
11+
reverse(nums, 0, k - 1);
12+
reverse(nums, k, size - 1);
13+
}
14+
15+
public void reverse(int[] nums, int start, int end) {
16+
while (start < end) {
17+
int temp = nums[start];
18+
nums[start] = nums[end];
19+
nums[end] = temp;
20+
start++;
21+
end--;
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// AC: Runtime: 2 ms, faster than 100.00% of Java online submissions for Rectangle Area.
2+
// Memory Usage: 38.1 MB, less than 88.56% of Java online submissions for Rectangle Area.
3+
// Depart into several situations and analyse respectively, and we can get the regularity about the overlapped area.
4+
// T:O(1), S:O(1)
5+
//
6+
class Solution {
7+
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
8+
int overlapArea, sumArea = (ay2 - ay1) * (ax2 - ax1) + (by2 - by1) * (bx2 - bx1);
9+
10+
// no-overlapping area.
11+
if (bx1 >= ax2 || bx2 <= ax1 || by1 >= ay2 || by2 <= ay1) {
12+
overlapArea = 0;
13+
} else {
14+
// the overlapped area
15+
overlapArea = (Math.min(bx2, ax2) - Math.max(bx1, ax1)) * (Math.min(by2, ay2) - Math.max(by1, ay1));
16+
}
17+
18+
return sumArea - overlapArea;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// 方法一:原始暴力,可以pass。。
2+
// Runtime: 977 ms, faster than 10.10% of Java online submissions for Daily Temperatures.
3+
// Memory Usage: 47.1 MB, less than 54.24% of Java online submissions for Daily Temperatures.
4+
// 略。
5+
// T:O(n^2), S:O(n)
6+
//
7+
class Solution {
8+
public int[] dailyTemperatures(int[] T) {
9+
int[] ret = new int[T.length];
10+
ret[T.length - 1] = 0;
11+
12+
for (int i = 0; i < T.length - 1; i++) {
13+
int count = 0;
14+
boolean hasBigger = false;
15+
for (int j = i + 1; j < T.length; j++) {
16+
count++;
17+
if (T[j] > T[i]) {
18+
hasBigger = true;
19+
break;
20+
}
21+
}
22+
if (hasBigger) {
23+
ret[i] = count;
24+
}
25+
}
26+
27+
return ret;
28+
}
29+
}
30+
31+
// 优化:单调栈解法
32+
// AC: Runtime: 40 ms, faster than 30.34% of Java online submissions for Daily Temperatures.
33+
// Memory Usage: 48.3 MB, less than 54.37% of Java online submissions for Daily Temperatures.
34+
// 单调栈解法
35+
// T:O(n), S:O(n)
36+
//
37+
class Solution {
38+
public int[] dailyTemperatures(int[] temperatures) {
39+
Stack<Integer> record = new Stack<>();
40+
int size = temperatures.length;
41+
int[] ret = new int[size];
42+
for (int i = 0; i < size; i++) {
43+
if (record.empty() || temperatures[i] <= temperatures[record.peek()]) {
44+
record.push(i);
45+
} else {
46+
while (!record.empty() && temperatures[i] > temperatures[record.peek()]) {
47+
ret[record.peek()] = i - record.peek();
48+
record.pop();
49+
}
50+
record.push(i);
51+
}
52+
}
53+
54+
return ret;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// AC: Runtime: 21 ms, faster than 5.46% of Java online submissions for Partition Labels.
2+
// Memory Usage: 40.2 MB, less than 5.08% of Java online submissions for Partition Labels.
3+
// start from 0-index, record the max substring that meets the requirement, until reach the end of string.
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public List<Integer> partitionLabels(String S) {
8+
List<Integer> ret = new LinkedList<>();
9+
int size = S.length();
10+
HashMap<Character, List<Integer>> charIndex = new HashMap<>();
11+
for (int i = 0; i < size; i++) {
12+
char c = S.charAt(i);
13+
if (charIndex.get(c) != null) {
14+
if (charIndex.get(c).size() == 1) {
15+
charIndex.get(c).add(i);
16+
} else {
17+
charIndex.get(c).set(1, i);
18+
}
19+
} else {
20+
List<Integer> temp = new LinkedList<>();
21+
temp.add(i);
22+
charIndex.put(c, temp);
23+
}
24+
}
25+
26+
// one char's first occur index and last occur index
27+
int startPos = 0, endPos = 0;
28+
while (startPos < size) {
29+
char c = S.charAt(startPos);
30+
List<Integer> tempIndex = charIndex.get(c);
31+
// appear only once
32+
if (tempIndex.size() == 1) {
33+
startPos++;
34+
ret.add(1);
35+
} else {
36+
endPos = tempIndex.get(1);
37+
int startPosCopy = startPos, endPosCopy = endPos;
38+
while (true) {
39+
int maxOtherCharIndex = -1;
40+
for (int i = startPosCopy + 1; i <= endPosCopy - 1; i++) {
41+
if (charIndex.get(S.charAt(i)).size() == 2) {
42+
maxOtherCharIndex = Math.max(maxOtherCharIndex, charIndex.get(S.charAt(i)).get(1));
43+
}
44+
}
45+
if (maxOtherCharIndex > endPosCopy) {
46+
startPosCopy = endPos;
47+
endPosCopy = maxOtherCharIndex;
48+
} else {
49+
ret.add(endPosCopy - startPos + 1);
50+
startPos = endPosCopy + 1;
51+
break;
52+
}
53+
}
54+
}
55+
}
56+
57+
return ret;
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// AC: Runtime: 2 ms, faster than 91.18% of Java online submissions for Letter Tile Possibilities.
2+
// Memory Usage: 36.6 MB, less than 99.51% of Java online submissions for Letter Tile Possibilities.
3+
// DP
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int numTilePossibilities(String tiles) {
8+
int[] record = new int[26];
9+
for (char c: tiles.toCharArray()) {
10+
record[c - 'A']++;
11+
}
12+
13+
return dfs(record);
14+
}
15+
16+
public int dfs(int[] arr) {
17+
int sum = 0;
18+
for (int i = 0; i < 26; i++) {
19+
if (arr[i] == 0) {
20+
continue;
21+
}
22+
sum++;
23+
arr[i]--;
24+
sum += dfs(arr);
25+
arr[i]++;
26+
}
27+
28+
return sum;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// AC: Runtime: 17 ms, faster than 37.69% of Java online submissions for All Elements in Two Binary Search Trees.
2+
// Memory Usage: 41.7 MB, less than 80.19% of Java online submissions for All Elements in Two Binary Search Trees.
3+
// .
4+
// T:O(len1 + len2)), S:O(len1 + len2)
5+
//
6+
class Solution {
7+
public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
8+
List<Integer> list1 = new ArrayList<>();
9+
List<Integer> list2 = new ArrayList<>();
10+
List<Integer> ret = new ArrayList<>();
11+
inOrderTravel(root1, list1);
12+
inOrderTravel(root2, list2);
13+
int pos1 = 0, pos2 = 0;
14+
while (pos1 < list1.size() || pos2 < list2.size()) {
15+
if (pos1 >= list1.size()) {
16+
while (pos2 < list2.size()) {
17+
ret.add(list2.get(pos2++));
18+
}
19+
} else if (pos2 >= list2.size()) {
20+
while (pos1 < list1.size()) {
21+
ret.add(list1.get(pos1++));
22+
}
23+
} else {
24+
if (list1.get(pos1).equals(list2.get(pos2))) {
25+
ret.add(list1.get(pos1++));
26+
ret.add(list2.get(pos2++));
27+
} else if (list1.get(pos1) > list2.get(pos2)) {
28+
ret.add(list2.get(pos2++));
29+
} else {
30+
ret.add(list1.get(pos1++));
31+
}
32+
}
33+
}
34+
35+
return ret;
36+
}
37+
38+
public void inOrderTravel(TreeNode root, List<Integer> out) {
39+
if (root == null) {
40+
return;
41+
}
42+
inOrderTravel(root.left, out);
43+
out.add(root.val);
44+
inOrderTravel(root.right, out);
45+
}
46+
}

0 commit comments

Comments
 (0)