Skip to content

Commit efec7d1

Browse files
committed
m crush
1 parent a8e9b02 commit efec7d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+4805
-2824
lines changed

Java/Merge Sorted Array II.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
长度已经固定普通做法
1+
E
2+
1528179898
3+
tags: Array
4+
5+
如题, merge two sorted array into 新的 sorted array
6+
7+
- 长度已经固定. Basic Implementation
8+
- 如果一个array足够大, merge into this array, 那么就是从末尾merge.
9+
210
```
311
/*
4-
33% Accepted
12+
513
Merge two given sorted integer array A and B into a new sorted integer array.
614
715
Example
@@ -26,10 +34,6 @@
2634
*/
2735

2836
class Solution {
29-
/**
30-
* @param A and B: sorted integer array A and B.
31-
* @return: A new sorted integer array
32-
*/
3337
public int[] mergeSortedArray(int[] A, int[] B) {
3438
if (A == null || B == null) {
3539
return A == null ? B : A;

Java/Minimum Subarray.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
E
2+
tags: Greedy, Array
3+
4+
TODO: dp?
5+
6+
```
7+
18
/*
29
Given an array of integers, find the subarray with smallest sum.
310
@@ -40,4 +47,5 @@ public int minSubArray(ArrayList<Integer> nums) {
4047
}
4148
return minRst;
4249
}
43-
}
50+
}
51+
```

Java/Nth to Last Node in List.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
先找到nth node
2-
然后head开始跑
1+
E
2+
1528180250
3+
tags: Linked List
4+
5+
#### Linked List
6+
- 先找到nth node
7+
- 然后head开始跑
8+
- node 到底而head ~ node刚好是 n 距离所以head就是要找的last nth
39

4-
node 到底而head ~ node刚好是 n 距离所以head就是要找的last nth
510
```
611
/*
712
Find the nth to last element of a singly linked list.

Java/O(1) Check Power of 2.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
E
2+
tags: Bit Manipulation
3+
4+
```
5+
16
/*
27
Using O(1) time to check whether an integer n is a power of 2.
38
Example
@@ -32,3 +37,5 @@ public boolean checkPowerOf2(int n) {
3237
};
3338

3439

40+
41+
```

Java/Partition Array by Odd and Even.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
E
2+
tags: Two Pointers, Array
3+
4+
- 更正常的start/end partition pointer类似: when condition meet, swap
5+
- Clean up TODO
6+
7+
```
18
/*
29
Partition an integers array into odd number first and even number second.
310
@@ -49,3 +56,5 @@ public void partitionArray(int[] nums) {
4956
}
5057
}
5158
}
59+
60+
```

Java/Product of Array Exclude Itself.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
1+
M
2+
tags: Array
3+
4+
5+
```
6+
/*
7+
LeetCode:
8+
Given an array nums of n integers where n > 1,
9+
return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
10+
11+
Example:
12+
13+
Input: [1,2,3,4]
14+
Output: [24,12,8,6]
15+
Note: Please solve it without division and in O(n).
16+
17+
Follow up:
18+
Could you solve it with constant space complexity?
19+
(The output array does not count as extra space for the purpose of space complexity analysis.)
20+
21+
*/
22+
23+
24+
25+
26+
127
/*
28+
LintCode
229
Given an integers array A.
330
431
Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WITHOUT divide operation.
@@ -42,3 +69,5 @@ public ArrayList<Long> productExcludeItself(ArrayList<Integer> A) {
4269
}
4370

4471

72+
73+
```

Java/Recover Rotated Sorted Array.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
E
2+
tags: Array
3+
14
rotate的意思是有个点断开把一边的array节选出来放在另外一边
25
Rotate三步
36
rotate前半

Java/Remove Duplicates from Unsorted List.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
M
2+
tags: Linked List
3+
14
基本方法: O(n) sapce, time
25
遍历
36
遇到duplicate(可能多个), while直到node.next不是duplicate.

Java/Search Range in Binary Search Tree .java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
M
2-
tags: BST
2+
1528177257
3+
tags: BST, Binary Tree
34

4-
等于遍历了所有k1<= x <= k2的x node
5+
给一个BST, integer range (k1, k2), 找range 里面所有的integer.
56

6-
如果是用Binary Search Tree搜索那么一般是if (...) else {...},也就是一条路走到底直到找到target.
7-
8-
这里, left/right/match的情况全部cover了然后把k1,k2的边框限制好中间就全部遍历了
7+
#### BST
8+
- 等于dfs遍历了所有k1<= x <= k2的x node
9+
- dfs left, process root, then dfs right
10+
- 这里, left/right/match的情况全部cover了然后把k1,k2的边框限制好中间就全部遍历了
911

1012
```
1113
/*
@@ -44,17 +46,18 @@ Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search
4446

4547
public class Solution {
4648
/**
47-
* @param root: The root of the binary search tree.
48-
* @param k1 and k2: range k1 to k2.
49-
* @return: Return all keys that k1<=key<=k2 in ascending order.
49+
* @param root: param root: The root of the binary search tree
50+
* @param k1: An integer
51+
* @param k2: An integer
52+
* @return: return: Return all keys that k1<=key<=k2 in ascending order
5053
*/
51-
public ArrayList<Integer> searchRange(TreeNode root, int k1, int k2) {
52-
ArrayList<Integer> result = new ArrayList<Integer>();
54+
public List<Integer> searchRange(TreeNode root, int k1, int k2) {
55+
List<Integer> result = new ArrayList<>();
5356
helper(result, root, k1, k2);
5457
return result;
5558
}
5659

57-
public void helper(ArrayList<Integer> result, TreeNode root, int k1, int k2) {
60+
public void helper(List<Integer> result, TreeNode root, int k1, int k2) {
5861
if (root == null) {
5962
return;
6063
}
@@ -70,5 +73,4 @@ public void helper(ArrayList<Integer> result, TreeNode root, int k1, int k2) {
7073
}
7174
}
7275

73-
7476
```

Java/Search Rotated in Sorted Array II.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
1+
M
2+
tags: Array, Binary Search
3+
14
Allow duplicates之后
25
因为最终binary search的结果也是O(n)
36
所以这道题要记得既然是O(n), 那来个简单的for loop 也就好了
47

58
当然要跟面试官提起来原因别一上来就只有for。。。
69
```
10+
/*
11+
LeetCode
12+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
13+
14+
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
15+
16+
You are given a target value to search. If found in the array return true, otherwise return false.
17+
18+
Example 1:
19+
20+
Input: nums = [2,5,6,0,0,1,2], target = 0
21+
Output: true
22+
Example 2:
23+
24+
Input: nums = [2,5,6,0,0,1,2], target = 3
25+
Output: false
26+
Follow up:
27+
28+
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
29+
Would this affect the run-time complexity? How and why?
30+
31+
32+
*/
33+
34+
35+
36+
737
/*
838
Follow up for "Search in Rotated Sorted Array":
939
What if duplicates are allowed?

Java/Search a 2D Matrix II.java

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,95 @@
11
M
2+
1528169466
3+
tags: Binary Search, Divide and Conquer
4+
5+
给matrix, 每一行sorted, 每一列从上往下sorted, 找target是否存在
6+
7+
#### Binary Search
8+
- 根据给定的性质, 其实点选的极端一点: x = 最下面的row, y = 当下一行里面最小的left position.
9+
- (x,y)在左下角
10+
- 在此情况下, 只能往一个方向运行: 如果小于target, y++; 如果大于target, 那么只能x--
11+
- 每次操作, 都是删掉一行, 或者一列, 再也不需要回头看
12+
- `while (x >= 0 && y < col) {}` 确保不会跑脱
13+
- 同样的方式: 可以从右上角(0, col - 1) 开始, 代码稍微改一改
14+
15+
#### Divide and Conquer?
16+
- TODO
217

3-
根据给定的性质找个点left-bottom):每次有任何情况只能往一个方向运行
4-
每次删掉一行或者一列
518
```
19+
/**
20+
LeetCode: check existance
21+
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
22+
23+
Integers in each row are sorted in ascending from left to right.
24+
Integers in each column are sorted in ascending from top to bottom.
25+
Consider the following matrix:
26+
27+
[
28+
[1, 4, 7, 11, 15],
29+
[2, 5, 8, 12, 19],
30+
[3, 6, 9, 16, 22],
31+
[10, 13, 14, 17, 24],
32+
[18, 21, 23, 26, 30]
33+
]
34+
Example 1:
35+
36+
Input: matrix, target = 5
37+
Output: true
38+
Example 2:
39+
40+
Input: matrix, target = 20
41+
Output: false
42+
*/
43+
// from bottom-left, (x,y) = (row -1, 0)
44+
public class Solution {
45+
public boolean searchMatrix(int[][] matrix, int target) {
46+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
47+
return false;
48+
}
49+
int row = matrix.length;
50+
int col = matrix[0].length;
51+
int x = row - 1;
52+
int y = 0;
53+
54+
while (x >= 0 && y < col) {
55+
if (matrix[x][y] < target) {
56+
y++;
57+
} else if (matrix[x][y] > target) {
58+
x--;
59+
} else {//matrix[x][y] == target
60+
return true;
61+
}
62+
}
63+
return false;
64+
}
65+
}
66+
67+
// from top-right, (x,y) = (0, col - 1)
68+
public class Solution {
69+
public boolean searchMatrix(int[][] matrix, int target) {
70+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
71+
return false;
72+
}
73+
int row = matrix.length;
74+
int col = matrix[0].length;
75+
int x = 0;
76+
int y = col - 1;
77+
78+
while (x < row && y >= 0) {
79+
if (matrix[x][y] < target) {
80+
x++;
81+
} else if (matrix[x][y] > target) {
82+
y--;
83+
} else {//matrix[x][y] == target
84+
return true;
85+
}
86+
}
87+
return false;
88+
}
89+
}
90+
691
/*
92+
LintCode: find # of occurances
793
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of it.
894
995
This matrix has the following properties:

Java/Search a 2D Matrix.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
M
2+
1528167745
3+
tags: Array, Binary Search
4+
5+
给2D matrix, 每行sorted, 每行的首位都大于上一行的末尾. goal: find target from matrix
6+
7+
#### 2D matrix 转1D array
8+
- 一行一行是从小到大, sorted, 连续的, 可以看做1D sorted array
9+
- Binary Search
210

3-
一行一行是从小到大连续的
4-
2D转1D
5-
Binary Search
611
```
712
/*
813
Write an efficient algorithm that searches for a value in an m x n matrix.

0 commit comments

Comments
 (0)