Skip to content

Commit 845fc37

Browse files
committed
fd
1 parent d53f294 commit 845fc37

File tree

3 files changed

+41
-41
lines changed

3 files changed

+41
-41
lines changed

leetcode/solution/src/AddBinary.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ public class AddBinary {
22

33
public String addBinary(String a, String b) {
44
StringBuilder sb = new StringBuilder();
5-
int i = a.length() - 1, j = b.length() - 1, k = 0;
6-
for ( ; i >= 0 || j >= 0 || k > 0; i--, j--) {
7-
int i0 = i >= 0 ? a.charAt(i) - '0' : 0;
8-
int j0 = j >= 0 ? b.charAt(j) - '0' : 0;
9-
int s = i0 + j0 + k;
10-
sb.insert(0, s & 1);
11-
k = s >> 1;
5+
for (int i = a.length() - 1, j = b.length() - 1, flag = 0; i >= 0 || j >= 0 || flag > 0; i--, j--) {
6+
int k1 = i >= 0 ? a.charAt(i) - '0' : 0;
7+
int k2 = j >= 0 ? b.charAt(j) - '0' : 0;
8+
int sum = k1 + k2 + flag;
9+
sb.append(sum % 2);
10+
flag = sum / 2;
1211
}
13-
return sb.toString();
12+
return sb.reverse().toString();
1413
}
1514
}

leetcode/solution/src/LongestIncreasingSubsequence.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class LongestIncreasingSubsequence {
44

55
/**
6-
* 这题是典型的DP,f[i]包含i的最长子序列长度
6+
* 这题是典型的DP,f[i]包含i的最长递增子序列长度
77
*/
88
public int lengthOfLIS(int[] nums) {
99
int n = nums.length;
@@ -27,24 +27,22 @@ public int lengthOfLIS(int[] nums) {
2727
return max;
2828
}
2929

30+
/**
31+
* 核心思路是dp维护了一个递增的序列,dp[i]表示nums中长度为i+1的递增子序列中tail最小的值
32+
* tail越小意味着之后延长这个序列更容易
33+
* 为了保证dp[i]是tail最小的,我们遍历nums时,当发现有更小的值时,会替换dp[i]
34+
*/
3035
public int lengthOfLIS2(int[] nums) {
31-
int len = 1;
32-
for (int i = 0; i < nums.length; i++) {
33-
/**
34-
* 注意这里要指定区间,且end是开区间
35-
*/
36-
int k = Arrays.binarySearch(nums, 0, len, nums[i]);
37-
/**
38-
* 只处理k<0的情况
39-
*/
40-
if (k < 0) {
41-
k = -(k + 1);
42-
if (k == len) {
43-
len++;
44-
}
45-
nums[k] = nums[i];
46-
}
36+
int[] dp = new int[nums.length];
37+
int len = 0;
38+
39+
for(int x : nums) {
40+
int i = Arrays.binarySearch(dp, 0, len, x);
41+
if(i < 0) i = -(i + 1);
42+
dp[i] = x;
43+
if(i == len) len++;
4744
}
45+
4846
return len;
4947
}
5048
}

leetcode/src/Main.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,32 @@ public class Main {
44

55
public static class Solution {
66

7-
public int firstMissingPositive(int[] nums) {
8-
for (int i = 0; i < nums.length; i++) {
9-
while (nums[i] - 1 < nums.length && nums[i] - 1 >= 0 && nums[i] != nums[nums[i] - 1]) {
10-
swap(nums, i, nums[i] - 1);
11-
}
7+
class MinStack {
8+
9+
/** initialize your data structure here. */
10+
public MinStack() {
11+
1212
}
13-
for (int i = 0; i < nums.length; i++) {
14-
if (nums[i] != i + 1) {
15-
return i + 1;
16-
}
13+
14+
public void push(int x) {
15+
16+
}
17+
18+
public void pop() {
19+
20+
}
21+
22+
public int top() {
23+
1724
}
18-
return nums.length + 1;
19-
}
2025

21-
private void swap(int[] nums, int i, int j) {
22-
int t = nums[i];
23-
nums[i] = nums[j];
24-
nums[j] = t;
26+
public int getMin() {
27+
28+
}
2529
}
2630
}
2731

2832

29-
3033
public static void main(String[] args) {
3134
Solution solution = new Solution();
3235
}

0 commit comments

Comments
 (0)