标签:bsp lse return 判断 优先 type flag == 解决
<cctype> isdigit(char)
问题:在做乘法,加法前,先判断是否溢出
&&优先级大于==
然后教训:
考虑情况不周。比如3.14这样
然后解决办法
多自己搞几组测试
+1
1
-1
...
class Solution { public: int myAtoi(string str) { long long ret=0; long long cmp1=((long long)1<<31)-1; long long cmp2=-cmp1-1; int i=0; int flag=0; while(i!=str.length()) { if(str[i]==‘ ‘){ i++;} else if(str[i]!=‘-‘&&!isdigit(str[i])&&str[i]!=‘+‘){ return 0;} else { if(isdigit(str[i])) flag=1; else if(i<str.length()-1&&isdigit(str[i+1])) flag=str[i]==‘+‘?1:-1; else return 0; if(!isdigit(str[i])) i++; while(i<str.length()&&isdigit(str[i])) { ret=ret*10+(str[i]-‘0‘); if(ret>=cmp1&&(flag==1)) return cmp1; else if(ret>cmp1) return cmp2; i++; } return flag==1?ret:-ret; } } return 0; } };
标签:bsp lse return 判断 优先 type flag == 解决
原文地址:https://www.cnblogs.com/lqerio/p/11746203.html