We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a2ad811 commit 6e0e3b6Copy full SHA for 6e0e3b6
8_String_to_Integer_(atoi).cc
@@ -0,0 +1,37 @@
1
+class Solution {
2
+public:
3
+ int myAtoi(string str) {
4
+ int result=0;
5
+ int k=1;
6
+ int count_k=0;
7
+ int count_val=0;
8
+ int count_dig=0;
9
+ for (int i = 0; i<str.size(); ++i){
10
+ if(str[i]=='-') {
11
+ k=k*(-1);
12
+ count_k++;
13
+ }
14
+ if(str[i]=='+') {
15
16
17
+ if(str[i]!=' ') count_val++;
18
+ if(::isdigit(str[i])){
19
+ count_dig++;
20
+ int tmp=result;
21
+ result = result * 10 + (str[i]-'0');
22
+ if (k>0&&(result<tmp||count_dig>10)) {
23
+ result = 2147483647;
24
+ break;
25
26
+ if(k<0&&(result<tmp||count_dig>10)){
27
+ result= 2147483648;
28
29
30
31
+ if(::isalpha(str[i])||(str[i]==' '&&count_val!=0)) break;
32
33
+ if(count_k>1) k=0;
34
+ result=result*k;
35
+ return result;
36
37
+};
0 commit comments