-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringtoInteger(atoi).java
More file actions
34 lines (30 loc) · 980 Bytes
/
StringtoInteger(atoi).java
File metadata and controls
34 lines (30 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*Implement atoi to convert a string to an integer.
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.
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.*/
public class Solution {
public int myAtoi(String str) {
if(str==null||str.length()==0)
return 0;
char sign='+';
int i=0;
double result=0;
str = str.trim();
if(str.charAt(0)=='-')
{
sign='-';
i++;
}
else if (str.charAt(0) == '+')
i++;
while (i<str.length() && str.charAt(i)>='0' && str.charAt(i)<='9')
{
result=result*10+(str.charAt(i)-'0');
i++;
}
if(sign=='-')
result=-result;
return (int)result;
}
}