Skip to content

Commit 7419e85

Browse files
committed
fd
1 parent 63ac2f5 commit 7419e85

File tree

3 files changed

+66
-58
lines changed

3 files changed

+66
-58
lines changed
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
public class LongestSubstringWithAtMostTwoDistinctCharacters {
22

3-
// 7ms
3+
// 1ms
44
public int lengthOfLongestSubstringTwoDistinct2(String s) {
5-
int[] count = new int[256];
6-
int distinct = 0, longest = 0;
7-
5+
if (s.length() == 0) {
6+
return 0;
7+
}
8+
int[] dp = new int[256];
9+
int count = 0, longest = 0;
810
for (int i = 0, j = 0; j < s.length(); j++) {
9-
if (count[s.charAt(j)]++ == 0) {
10-
distinct++;
11-
}
12-
13-
for ( ; i < j && distinct > 2; ) {
14-
if (--count[s.charAt(i++)] == 0) {
15-
--distinct;
11+
if (dp[s.charAt(j)]++ == 0) {
12+
for (++count; i <= j && count > 2; ) {
13+
if (--dp[s.charAt(i++)] == 0) {
14+
--count;
15+
}
1616
}
1717
}
18-
1918
longest = Math.max(longest, j - i + 1);
2019
}
21-
2220
return longest;
2321
}
2422
}

leetcode/solution/src/String2Integer.java

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,35 @@
1313
public class String2Integer {
1414

1515
public int myAtoi(String str) {
16-
long n = 0;
17-
18-
str = str.trim();
19-
20-
// 这里要防御空串
21-
if (str.length() == 0) {
22-
return 0;
23-
}
24-
25-
int sign = 1;
26-
27-
switch (str.charAt(0)) {
28-
case '-':
16+
int i = 0, sign = 1;
17+
for ( ; i < str.length() && str.charAt(i) == ' '; i++);
18+
if (i < str.length()) {
19+
char csign = str.charAt(i);
20+
if (csign == '-') {
2921
sign = -1;
30-
case '+':
31-
str = str.substring(1);
32-
break;
22+
i++;
23+
} else if (csign == '+') {
24+
sign = 1;
25+
i++;
26+
} else if (csign < '0' || csign > '9') {
27+
return 0;
28+
} else {}
3329
}
34-
35-
for (char c : str.toCharArray()) {
36-
if (c >= '0' && c <= '9') {
37-
n = n * 10 + (c - '0');
38-
39-
if (n * sign > Integer.MAX_VALUE) {
40-
return Integer.MAX_VALUE;
41-
} else if (n * sign < Integer.MIN_VALUE) {
42-
return Integer.MIN_VALUE;
43-
}
44-
45-
} else {
30+
long number = 0;
31+
for ( ; i < str.length(); i++) {
32+
char c = str.charAt(i);
33+
if (c < '0' || c > '9') {
4634
break;
4735
}
48-
}
36+
number = number * 10 + (c - '0');
4937

50-
return (int) (n * sign);
38+
if (number * sign > Integer.MAX_VALUE) {
39+
return Integer.MAX_VALUE;
40+
}
41+
if (number * sign < Integer.MIN_VALUE) {
42+
return Integer.MIN_VALUE;
43+
}
44+
}
45+
return (int) (number * sign);
5146
}
5247
}

leetcode/src/Main.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,40 @@
22

33
public class Main {
44

5-
public static int coinChange(int[] coins, int amount) {
6-
Arrays.sort(coins);
7-
int[] dp = new int[amount + 1];
8-
Arrays.fill(dp, -1);
9-
dp[0] = 0;
10-
for (int i = 1; i <= amount; i++) {
11-
for (int coin: coins) {
12-
if (i - coin >= 0 && dp[i - coin] >= 0) {
13-
int k = dp[i - coin] + 1;
14-
dp[i] = dp[i] > 0 ? Math.min(dp[i], k) : k;
15-
}
5+
public static int myAtoi(String str) {
6+
int i = 0, sign = 1;
7+
for ( ; i < str.length() && str.charAt(i) == ' '; i++);
8+
if (i < str.length()) {
9+
char csign = str.charAt(i);
10+
if (csign == '-') {
11+
sign = -1;
12+
i++;
13+
} else if (csign == '+') {
14+
sign = 1;
15+
i++;
16+
} else if (csign < '0' || csign > '9') {
17+
return 0;
18+
} else {}
19+
}
20+
long number = 0;
21+
for ( ; i < str.length(); i++) {
22+
char c = str.charAt(i);
23+
if (c < '0' || c > '9') {
24+
break;
25+
}
26+
number = number * 10 + (c - '0');
27+
28+
if (number * sign > Integer.MAX_VALUE) {
29+
return Integer.MAX_VALUE;
30+
}
31+
if (number * sign < Integer.MIN_VALUE) {
32+
return Integer.MIN_VALUE;
1633
}
1734
}
18-
return dp[amount];
35+
return (int) (number * sign);
1936
}
2037

2138
public static void main(String[] args) {
22-
System.out.println(coinChange(new int[] {
23-
1, 2, 5
24-
}, 11));
39+
System.out.println(myAtoi("-42"));
2540
}
2641
}

0 commit comments

Comments
 (0)