Skip to content

Commit 573739c

Browse files
committed
change on atoi
1 parent 64d6ce4 commit 573739c

File tree

4 files changed

+89
-47
lines changed

4 files changed

+89
-47
lines changed

Algorithms Basics/Chapter 1. Array_String/8. String to Integer (atoi).cs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,35 @@ 2. takes an optional '+' or '-'
88
5. If out of int range, return INT_MAX(2147483647) or INT_MIN(-2147483648)
99
*/
1010

11+
private static readonly int maxDiv10 = int.MaxValue / 10;
1112
public int MyAtoi(string str)
1213
{
1314
if (str == null) return 0;
14-
str = str.Trim();
15-
if (str.Length == 0) return 0;
15+
int i = 0, n = str.Length;
16+
while (i < n && char.IsWhiteSpace(str[i])) i++;
17+
if (i == n) return 0;
1618
int sign = 1;
17-
int index = 0;
18-
if (str[index] == '+') index++;
19-
else if (str[index] == '-') {
19+
if (i < n && str[i] == '+') {
20+
i++;
21+
} else if (i < n && str[i] == '-') {
2022
sign = -1;
21-
index++;
23+
i++;
2224
}
23-
long num = 0;
24-
for (; index < str.Length; index++) {
25-
if (str[index] < '0' || str[index] > '9')
26-
{
27-
break;
25+
int num = 0;
26+
while (i < n && char.IsDigit(str[i])) {
27+
int digit = str[i] - '0';
28+
if (num > maxDiv10 || num == maxDiv10 && digit >= 8) {
29+
return sign == 1 ? int.MaxValue : int.MinValue;
2830
}
29-
num = num * 10 + str[index] - '0';
30-
if (num > int.MaxValue)
31-
{
32-
break;
33-
}
34-
}
35-
if (num * sign >= int.MaxValue)
36-
{
37-
return int.MaxValue;
38-
}
39-
if (num * sign <= int.MinValue)
40-
{
41-
return int.MinValue;
31+
num = num * 10 + digit;
32+
i++;
4233
}
43-
return (int)num * sign;
34+
return sign * num;
4435
}
45-
}
36+
}
37+
38+
/*
39+
Next challenges:
40+
Reverse Integer
41+
Valid Number
42+
*/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
public class Solution {
2+
/*
3+
Requirements for atoi:
4+
1. trim whitespace
5+
2. takes an optional '+' or '-'
6+
3. when meet non-numetic char, break " - 0012a43" return -12
7+
4. If no valid conversion could be performed, 0 is returned.
8+
5. If out of int range, return INT_MAX(2147483647) or INT_MIN(-2147483648)
9+
*/
10+
11+
private static readonly int maxDiv10 = int.MaxValue / 10;
12+
public int MyAtoi(string str)
13+
{
14+
if (str == null) return 0;
15+
int i = 0, n = str.Length;
16+
while (i < n && char.IsWhiteSpace(str[i])) i++;
17+
if (i == n) return 0;
18+
int sign = 1;
19+
if (i < n && str[i] == '+') {
20+
i++;
21+
} else if (i < n && str[i] == '-') {
22+
sign = -1;
23+
i++;
24+
}
25+
int num = 0;
26+
while (i < n && char.IsDigit(str[i])) {
27+
int digit = str[i] - '0';
28+
if (num > maxDiv10 || num == maxDiv10 && digit >= 8) {
29+
return sign == 1 ? int.MaxValue : int.MinValue;
30+
}
31+
num = num * 10 + digit;
32+
i++;
33+
}
34+
return sign * num;
35+
}
36+
}
37+
38+
/*
39+
Next challenges:
40+
Reverse Integer
41+
Valid Number
42+
*/

LeetcodeCShaprDotNetCore/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace LeetcodeCShaprDotNetCore {
44
class Program {
55
static void Main(string[] args) {
6-
var s = new int[]{ 1, 0, -1, 0, -2, 2 };
7-
var res = Solution.FourSum(s, 0);
6+
var s = "1";
7+
int i = Solution.MyAtoi(s);
88
Console.ReadLine();
99
}
1010

LeetcodeCShaprDotNetCore/Solution.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,35 @@
44

55
namespace LeetcodeCShaprDotNetCore
66
{
7+
78
public class Solution
89
{
9-
public ListNode RotateRight(ListNode head, int k)
10+
private static readonly int maxDiv10 = int.MaxValue / 10;
11+
public static int MyAtoi(string str)
1012
{
11-
if (head == null || head.next == null) return head;
12-
// get length
13-
int len = 1;
14-
ListNode tail = head;
15-
while (tail.next != null)
13+
int i = 0, n = str.Length;
14+
while (i < n && char.IsWhiteSpace(str[i])) i++;
15+
int sign = 1;
16+
if (i < n && str[i] == '+') {
17+
i++;
18+
}
19+
else if (i < n && str[i] == '-')
1620
{
17-
tail = tail.next;
18-
len++;
21+
sign = -1;
22+
i++;
1923
}
20-
// connect circle
21-
tail.next = head;
22-
// find new head;
23-
var n = len - k % len;
24-
while (n > 0)
24+
int num = 0;
25+
while (i < n && char.IsDigit(str[i]))
2526
{
26-
tail = head;
27-
head = head.next;
28-
n--;
27+
int digit = str[i] - '0';
28+
if (num > maxDiv10 || num == maxDiv10 && digit >= 8)
29+
{
30+
return sign == 1 ? int.MaxValue : int.MinValue;
31+
}
32+
num = num * 10 + digit;
33+
i++;
2934
}
30-
// cut before the new head
31-
tail.next = null;
32-
return head;
35+
return sign * num;
3336
}
3437
}
3538
}

0 commit comments

Comments
 (0)