You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: note/008/README.md
+53-13Lines changed: 53 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,23 +2,62 @@
2
2
3
3
## Description
4
4
5
-
Implement atoi to convert a string to an integer.
5
+
Implement `atoi` which converts a string to an integer.
6
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.
7
+
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.
8
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.
9
+
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.
10
10
11
-
**Spoilers:**
11
+
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.
12
12
13
-
**Requirements for atoi:**
13
+
If no valid conversion could be performed, a zero value is returned.
14
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.
15
+
**Note:**
16
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.
17
+
- Only the space character `' '` is considered as whitespace character.
18
+
- Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2<sup>31</sup>, 2<sup>31</sup> − 1]. If the numerical value is out of the range of representable values, INT_MAX (2<sup>31</sup> − 1) or INT_MIN (−2<sup>31</sup>) is returned.
18
19
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
+
**Example 1:**
21
+
22
+
```
23
+
Input: "42"
24
+
Output: 42
25
+
```
26
+
27
+
**Example 2:**
28
+
29
+
```
30
+
Input: " -42"
31
+
Output: -42
32
+
Explanation: The first non-whitespace character is '-', which is the minus sign.
33
+
Then take as many numerical digits as possible, which gets 42.
34
+
```
35
+
36
+
**Example 3:**
37
+
38
+
```
39
+
Input: "4193 with words"
40
+
Output: 4193
41
+
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
42
+
```
43
+
44
+
**Example 4:**
20
45
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.
46
+
```
47
+
Input: "words and 987"
48
+
Output: 0
49
+
Explanation: The first non-whitespace character is 'w', which is not a numerical
50
+
digit or a +/- sign. Therefore no valid conversion could be performed.
51
+
```
52
+
53
+
**Example 5:**
54
+
55
+
```
56
+
Input: "-91283472332"
57
+
Output: -2147483648
58
+
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
59
+
Thefore INT_MIN (−2^31) is returned.
60
+
```
22
61
23
62
**Tags:** Math, String
24
63
@@ -37,12 +76,13 @@ class Solution {
37
76
}
38
77
for (; i < len; ++i) {
39
78
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)
Copy file name to clipboardExpand all lines: note/009/README.md
+22-7Lines changed: 22 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,19 +2,34 @@
2
2
3
3
## Description
4
4
5
-
Determine whether an integer is a palindrome. Do this without extra space.
5
+
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
6
6
7
-
**Spoilers:**
7
+
**Example 1:**
8
8
9
-
**Some hints:**
9
+
```
10
+
Input: 121
11
+
Output: true
12
+
```
13
+
14
+
**Example 2:**
15
+
16
+
```
17
+
Input: -121
18
+
Output: false
19
+
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
20
+
```
10
21
11
-
Could negative integers be palindromes? (ie, -1)
22
+
**Example 3:**
12
23
13
-
If you are thinking of converting the integer to string, note the restriction of using extra space.
24
+
```
25
+
Input: 10
26
+
Output: false
27
+
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
28
+
```
14
29
15
-
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
30
+
**Follow up:**
16
31
17
-
There is a more generic way of solving this problem.
32
+
Coud you solve it without converting the integer to a string?
Copy file name to clipboardExpand all lines: note/012/README.md
+58-2Lines changed: 58 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,65 @@
2
2
3
3
## Description
4
4
5
-
Given an integer, convert it to a roman numeral.
5
+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
6
6
7
-
Input is guaranteed to be within the range from 1 to 3999.
7
+
```
8
+
Symbol Value
9
+
I 1
10
+
V 5
11
+
X 10
12
+
L 50
13
+
C 100
14
+
D 500
15
+
M 1000
16
+
```
17
+
18
+
For example, two is written as `II` in Roman numeral, just two one's added together. Twelve is written as, `XII`, which is simply `X` + `II`. The number twenty seven is written as `XXVII`, which is `XX` + `V` + `II`.
19
+
20
+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
21
+
22
+
-`I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
23
+
-`X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
24
+
-`C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
25
+
26
+
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
27
+
28
+
**Example 1:**
29
+
30
+
```
31
+
Input: 3
32
+
Output: "III"
33
+
```
34
+
35
+
**Example 2:**
36
+
37
+
```
38
+
Input: 4
39
+
Output: "IV"
40
+
```
41
+
42
+
**Example 3:**
43
+
44
+
```
45
+
Input: 9
46
+
Output: "IX"
47
+
```
48
+
49
+
**Example 4:**
50
+
51
+
```
52
+
Input: 58
53
+
Output: "LVIII"
54
+
Explanation: C = 100, L = 50, XXX = 30 and III = 3.
55
+
```
56
+
57
+
**Example 5:**
58
+
59
+
```
60
+
Input: 1994
61
+
Output: "MCMXCIV"
62
+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
0 commit comments