Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita6f9f5a

Browse files
draft for atoi
1 parent610e36f commita6f9f5a

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

‎EASY/src/easy/StringToInteger.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
packageeasy;
2+
3+
importjava.util.HashSet;
24
/**8. String to Integer (atoi) QuestionEditorial Solution My Submissions
35
Total Accepted: 115114
46
Total Submissions: 839893
@@ -8,6 +10,7 @@
810
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.
911
1012
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.*/
13+
importjava.util.Set;
1114

1215
/**Requirements for atoi:
1316
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.
@@ -28,6 +31,59 @@ public int myAtoi(String str) {
2831
//case 4: there're many leading whitespace characters which we'll have to ignore
2932

3033
//case 5: when finding the first non-whitespace character, it could possibly be a '+' or '-' sign, after that, we parse all the consecutive numbers
31-
return0;
34+
35+
str =str.trim();//cut off its leading and trailing whitespace characters
36+
if(str ==null ||str.isEmpty())return0;
37+
Set<Character>numbers =newHashSet();
38+
for(inti =0;i <10;i++){
39+
numbers.add(Character.forDigit(i,10));
40+
}
41+
42+
char[]chars =str.toCharArray();
43+
StringBuildersb =newStringBuilder();
44+
booleannegative;
45+
inti =0;
46+
if(chars[0] =='-') {
47+
negative =true;
48+
i++;//let i start from 1 in this case
49+
}
50+
elsenegative =false;
51+
52+
//it might be a floating number, so consider '.'
53+
intperiod =0;
54+
while(i <chars.length &&numbers.contains(chars[i])){
55+
if(chars[i] =='.')period++;
56+
if(period >1)break;
57+
sb.append(chars[i++]);
58+
}
59+
60+
intresult =0;
61+
if(period >0){
62+
//use Double to parse
63+
try{
64+
result = (int)Double.parseDouble(sb.toString());
65+
}catch(Exceptione){
66+
System.out.println(e);
67+
}
68+
}else {
69+
//use Integer to parse
70+
try{
71+
result =Integer.parseInt(sb.toString());
72+
}catch(Exceptione){
73+
System.out.println(e);
74+
}
75+
}
76+
77+
returnresult;
78+
}
79+
80+
publicstaticvoidmain(String...strings){
81+
StringToIntegertest =newStringToInteger();
82+
Stringstr ="1";
83+
System.out.println(test.myAtoi(str));
84+
85+
86+
// System.out.println(Double.parseDouble("1.2098"));
87+
// System.out.println(Integer.parseInt("123456789"));
3288
}
3389
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp