- int reverse(int x) {
- long long ans=0;
- const int maxint=0x7fffffff;
- const int minint=0x80000000;
- do(x!=0){
- ans=ans*10+(x%10);//对正数负数都能处理
- x/=10;
- }while(x!=0);
- if(ans<minint || ans>maxint){
- ans=0;
- }
- return ans;
- }
008
- int myAtoi(char* str) {
- int i=0,sign=0;
- long long n=0;//非常重要,一定要定义long long
- const int maxint=0x7FFFFFFF;
- const int minint=0x80000000;
- const long long max=0x100000000;//非常重要,绝对值要大于minint的绝对值
- while(str[i]==' ' || str[i]=='\n' || str[i]=='\t')
- i++;
- if(str[i]=='+'){
- i++;
- }
- else{
- if(str[i]=='-'){
- sign=1;
- i++;
- }
- }
-
- for(;str[i]!='\0';i++){
- if(str[i]<='9' && str[i]>='0'){
- n=n*10+(str[i]-'0');
- if(n>max)n=max;//非常重要
- }else{
- break;
- }
- }
- if(sign)n=-n;
- if(n>maxint)n=maxint;
- if(n<minint)n=minint;
- return n;
- }
- /*
- "9223372036854775809"
- "2147483648"
- "-2147483648"
- " -11919730356x"
- */