Leetcode String to Integer (atoi)
来源:互联网 发布:mysql nvl函数用法 编辑:程序博客网 时间:2024/06/07 01:59
1. 如果输入不合法,输出0
2. 正负判断
3. 溢出输出INT_MAX or INT_MIN
4. 忽略最后的字母
代码还是比较简洁的,一次AC
bool isDigit(char c){ return c >= '0' && c <= '9'; } int atoi(const char *str) { bool neg = false; long long ans = 0; const int n = strlen(str); int i = 0; int valid_digit = 0; while(i < n && str[i] == ' ') i++; // plus or negative if(str[i] == '+' || str[i] == '-'){ if(str[i] == '-') neg = true; i++; } while(i < n && isDigit(str[i])){ ans *= 10; ans += (int) (str[i] - '0'); i++; valid_digit++; if(valid_digit >= 11) break; } if(neg) ans = -ans; if(ans > INT_MAX) return INT_MAX; if(ans < INT_MIN) return INT_MIN; return ans; }
0 0
- LeetCode:String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- LeetCode String to Integer(atoi)
- LeetCode :String to Integer (atoi)
- Leetcode: String to Integer (atoi)
- Leetcode : String to Integer (atoi)
- 【leetcode】String to Integer (atoi)
- [LeetCode]String to Integer (atoi)
- LeetCode-String to Integer (atoi)
- [leetcode] String to Integer (atoi)
- LeetCode - String to Integer (atoi)
- 【leetcode】String to Integer (atoi)
- LeetCode - String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- Leetcode: String to Integer (atoi)
- [LeetCode] String to Integer (atoi)
- leetcode String to Integer (atoi)
- [LeetCode]String to Integer (atoi)
- hibernate中的关联映射图解----多对一
- AFN报错问题
- Intellj idea 快捷键总结
- 输入对象:FileReader小结
- JRockit检测Tomcat内存溢出JAVA内存泄漏问题
- Leetcode String to Integer (atoi)
- hibernate中get与load区别
- 往Atheros AR9280中烧写wifi模块过程
- Xcode6创建空白文档
- 【IOS】 readonly IOS下实战入门
- 解析oracle的rownum
- linux系统免登陆之SSH
- 离线安装Cloudera Manager 5和CDH5(最新版5.1.3) 完全教程(转载)
- Cloudera Manager 5 和 CDH5 本地(离线)安装指南(Hadoop集群搭建)(转载)