Skip to content

Commit b66af94

Browse files
左程云左程云
authored andcommitted
class modify
1 parent ab2900c commit b66af94

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

src/topinterviewquestions/Problem_0001_TwoSum.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
public class Problem_0001_TwoSum {
66

77
public static int[] twoSum(int[] nums, int target) {
8+
// key 某个之前的数 value 这个数出现的位置
89
HashMap<Integer, Integer> map = new HashMap<>();
910
for (int i = 0; i < nums.length; i++) {
1011
if (map.containsKey(target - nums[i])) {

src/topinterviewquestions/Problem_0003_LongestSubstringWithoutRepeatingCharacters.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
package topinterviewquestions;
22

3+
import java.util.HashMap;
4+
35
public class Problem_0003_LongestSubstringWithoutRepeatingCharacters {
46

5-
public static int lengthOfLongestSubstring(String str) {
6-
if (str == null || str.equals("")) {
7+
public static int lengthOfLongestSubstring(String s) {
8+
if (s == null || s.equals("")) {
79
return 0;
810
}
9-
char[] chas = str.toCharArray();
11+
char[] str = s.toCharArray();
12+
// map (a, ?) (b, ?)
13+
// a, 17
14+
// map[97] = 17
1015
int[] map = new int[256];
1116
for (int i = 0; i < 256; i++) {
1217
map[i] = -1;
1318
}
19+
// 收集答案
1420
int len = 0;
15-
int pre = -1;
21+
int pre = -1; // i-1位置结尾的情况下,往左推,推不动的位置是谁
1622
int cur = 0;
17-
for (int i = 0; i != chas.length; i++) {
18-
pre = Math.max(pre, map[chas[i]]);
23+
for (int i = 0; i != str.length; i++) {
24+
// i位置结尾的情况下,往左推,推不动的位置是谁
25+
// pre (i-1信息) -> pre(i 结尾信息)
26+
pre = Math.max(pre, map[str[i]]);
1927
cur = i - pre;
2028
len = Math.max(len, cur);
21-
map[chas[i]] = i;
29+
map[str[i]] = i;
2230
}
2331
return len;
2432
}

src/topinterviewquestions/Problem_0004_MedianOfTwoSortedArrays.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,47 @@ public static int findKthNum(int[] arr1, int[] arr2, int kth) {
4545
}
4646
return getUpMedian(shorts, kth - l, s - 1, longs, kth - s, l - 1);
4747
}
48+
// 第2段
4849
if (longs[kth - s - 1] >= shorts[s - 1]) {
4950
return longs[kth - s - 1];
5051
}
5152
return getUpMedian(shorts, 0, s - 1, longs, kth - s, kth - 1);
5253
}
5354

54-
public static int getUpMedian(int[] a1, int s1, int e1, int[] a2, int s2, int e2) {
55+
public static int getUpMedian(int[] A, int s1, int e1, int[] B, int s2, int e2) {
5556
int mid1 = 0;
5657
int mid2 = 0;
57-
int offset = 0;
5858
while (s1 < e1) {
5959
mid1 = (s1 + e1) / 2;
6060
mid2 = (s2 + e2) / 2;
61-
offset = ((e1 - s1 + 1) & 1) ^ 1;
62-
if (a1[mid1] > a2[mid2]) {
63-
e1 = mid1;
64-
s2 = mid2 + offset;
65-
} else if (a1[mid1] < a2[mid2]) {
66-
s1 = mid1 + offset;
67-
e2 = mid2;
68-
} else {
69-
return a1[mid1];
61+
if (A[mid1] == B[mid2]) {
62+
return A[mid1];
63+
}
64+
if (((e1 - s1 + 1) & 1) == 1) { // 奇数长度
65+
if (A[mid1] > B[mid2]) {
66+
if (B[mid2] >= A[mid1 - 1]) {
67+
return B[mid2];
68+
}
69+
e1 = mid1 - 1;
70+
s2 = mid2 + 1;
71+
} else { // A[mid1] < B[mid2]
72+
if (A[mid1] >= B[mid2 - 1]) {
73+
return A[mid1];
74+
}
75+
e2 = mid2 - 1;
76+
s1 = mid1 + 1;
77+
}
78+
} else { // 偶数长度
79+
if (A[mid1] > B[mid2]) {
80+
e1 = mid1;
81+
s2 = mid2 + 1;
82+
} else {
83+
e2 = mid2;
84+
s1 = mid1 + 1;
85+
}
7086
}
7187
}
72-
return Math.min(a1[s1], a2[s2]);
88+
return Math.min(A[s1], B[s2]);
7389
}
7490

7591
}

0 commit comments

Comments
 (0)