-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringtointeger.cpp
More file actions
44 lines (42 loc) · 1005 Bytes
/
Copy pathstringtointeger.cpp
File metadata and controls
44 lines (42 loc) · 1005 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
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int myAtoi(string s) {
long int ans=0;
int i,neg=0;
vector<int>v;
for(i=0;s[i]!='\0';i++){
if(s[i]==' ' || s[i]=='-' ||s[i]=='+'){
if(!v.empty() ||(s[i]=='-' && s[i+1]=='+') ||(s[i]=='+' && s[i+1]=='-')){
break;
}
else{
if(s[i]=='-'){
neg=1;
}
}
}
else if(s[i]<'0' ||s[i]>'9'){
break;
}
else{
int z=int(s[i])-'0';
v.push_back(z);
}
}
for(int j: v){
ans=ans*10+j;
}
if(neg==1){
ans=ans*-1;
}
if(ans<-2147483648){
return -2147483648;
}
else if(ans>2147483647){
return 2147483647;
}
return ans;
}
};