Skip to content

Commit 0629947

Browse files
committed
feat: add 008
1 parent 9378776 commit 0629947

File tree

7 files changed

+114
-29
lines changed

7 files changed

+114
-29
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
|:------------- |:------------- |:------------- |
99
|1|[Two Sum][001]|Array, Hash Table|
1010
|7|[Reverse Integer][007]|Math|
11+
|8|[String to Integer (atoi)][008]|Math, String|
1112

1213

1314
## Medium
@@ -30,3 +31,4 @@
3031
[note]: https://github.com/Blankj/awesome-java-leetcode/tree/master/note
3132
[001]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/001/README.md
3233
[007]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/007/README.md
34+
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md

note/001/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Two Sum
1+
# [Two Sum](https://leetcode.com/problems/two-sum/)
22

33
## Description
44

@@ -15,7 +15,7 @@ Because nums[0] + nums[1] = 2 + 7 = 9,
1515
return [0, 1].
1616
```
1717

18-
**Tag** Array, Hash Table
18+
**Tags** Array, Hash Table
1919

2020

2121
## 思路0

note/007/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Reverse Integer
1+
# [Reverse Integer](https://leetcode.com/problems/reverse-integer/)
22

33
## Description
44

@@ -24,7 +24,7 @@ For the purpose of this problem, assume that your function returns 0 when the re
2424

2525
The input is assumed to be a 32-bit signed integer. Your function should **return 0 when the reversed integer overflows**.
2626

27-
**Tag** Math
27+
**Tags** Math
2828

2929

3030
## 思路

note/008/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/)
2+
3+
## Description
4+
5+
Implement atoi to convert a string to an integer.
6+
7+
**Hint:** Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
8+
9+
**Notes:** It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
10+
11+
**Spoilers:**
12+
13+
**Requirements for atoi:**
14+
15+
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
16+
17+
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
18+
19+
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
20+
21+
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
22+
23+
**Tags:** Math, String
24+
25+
26+
## 思路
27+
28+
题目大意就是把一个字符串转为整型,但要注意所给的要求,先去除最前面的空格,然后判断正负数,注意正数可能包含`+`,如果之后存在非数字或全为空则返回`0`,而如果合法的值超过int表示的最大范围,则根据正负号返回`INT_MAX``INT_MIN`
29+
30+
``` java
31+
public class Solution {
32+
public int myAtoi(String str) {
33+
int i = 0, ans = 0, sign = 1, len = str.length();
34+
while (i < len && str.charAt(i) == ' ') ++i;
35+
if (i < len && (str.charAt(i) == '-' || str.charAt(i) == '+')) {
36+
sign = str.charAt(i++) == '+' ? 1 : -1;
37+
}
38+
for (; i < len; ++i) {
39+
int tmp = str.charAt(i) - '0';
40+
if (tmp < 0 || tmp > 9)
41+
break;
42+
if (ans > Integer.MAX_VALUE / 10 || ans == Integer.MAX_VALUE / 10 && Integer.MAX_VALUE % 10 < tmp)
43+
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
44+
else
45+
ans = ans * 10 + tmp;
46+
}
47+
return sign * ans;
48+
}
49+
}
50+
```

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_001/_001.java renamed to project/LeetCode/leetcode/src/main/java/com/blankj/easy/_001/Solution.java

+10-13
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,12 @@
77
* <pre>
88
* author: Blankj
99
* blog : http://blankj.com
10-
* time : 2017/04/19
10+
* time : 2017/04/21
1111
* desc :
1212
* </pre>
1313
*/
1414

15-
public class _001 {
16-
17-
public static void main(String[] args) {
18-
Solution solution = new Solution();
19-
int[] nums = new int[]{2, 7, 11, 15};
20-
int target = 9;
21-
System.out.println(Arrays.toString(solution.twoSum(nums, target)));
22-
}
23-
}
24-
25-
class Solution {
15+
public class Solution {
2616
// public int[] twoSum(int[] nums, int target) {
2717
// int st = 0, end = nums.length;
2818
// for (int i = 0; i < end; ++i) {
@@ -46,4 +36,11 @@ public int[] twoSum(int[] nums, int target) {
4636
}
4737
return null;
4838
}
49-
}
39+
40+
public static void main(String[] args) {
41+
Solution solution = new Solution();
42+
int[] nums = new int[]{2, 7, 11, 15};
43+
int target = 9;
44+
System.out.println(Arrays.toString(solution.twoSum(nums, target)));
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.blankj.easy._007;
22

3-
import java.util.Arrays;
4-
53
/**
64
* <pre>
75
* author: Blankj
@@ -11,21 +9,19 @@
119
* </pre>
1210
*/
1311

14-
public class _007 {
12+
public class Solution {
13+
public int reverse(int x) {
14+
long res = 0;
15+
for (; x != 0; x /= 10)
16+
res = res * 10 + x % 10;
17+
return res > Integer.MAX_VALUE || res < Integer.MIN_VALUE ? 0 : (int) res;
18+
}
19+
1520
public static void main(String[] args) {
1621
Solution solution = new Solution();
1722
System.out.println(solution.reverse(123));
1823
System.out.println(solution.reverse(-123));
1924
System.out.println(solution.reverse(100));
2025
System.out.println(solution.reverse(1000000003));
2126
}
22-
}
23-
24-
class Solution {
25-
public int reverse(int x) {
26-
long res = 0;
27-
for (; x != 0; x /= 10)
28-
res = res * 10 + x % 10;
29-
return res > Integer.MAX_VALUE || res < Integer.MIN_VALUE ? 0 : (int) res;
30-
}
3127
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.blankj.easy._008;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2017/04/21
8+
* desc :
9+
* </pre>
10+
*/
11+
12+
public class Solution {
13+
public int myAtoi(String str) {
14+
int i = 0, ans = 0, sign = 1, len = str.length();
15+
while (i < len && str.charAt(i) == ' ') ++i;
16+
if (i < len && (str.charAt(i) == '-' || str.charAt(i) == '+')) {
17+
sign = str.charAt(i++) == '+' ? 1 : -1;
18+
}
19+
for (; i < len; ++i) {
20+
int tmp = str.charAt(i) - '0';
21+
if (tmp < 0 || tmp > 9)
22+
break;
23+
if (ans > Integer.MAX_VALUE / 10 || ans == Integer.MAX_VALUE / 10 && Integer.MAX_VALUE % 10 < tmp)
24+
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
25+
else
26+
ans = ans * 10 + tmp;
27+
}
28+
return sign * ans;
29+
}
30+
31+
public static void main(String[] args) {
32+
Solution solution = new Solution();
33+
System.out.println(solution.myAtoi(" +1"));
34+
System.out.println(solution.myAtoi(" -1"));
35+
System.out.println(solution.myAtoi(""));
36+
System.out.println(solution.myAtoi("a1"));
37+
System.out.println(solution.myAtoi("100000000000000000000"));
38+
System.out.println(solution.myAtoi("-100000000000000000000"));
39+
}
40+
}

0 commit comments

Comments
 (0)