public class Solution {
public int myAtoi(String str) {
int index = 0, sign = 1, total = 0;
//1. 边界条件判断
if(str.length() == 0) return 0; //2. 移除空格
while(str.charAt(index) == ' ' && index < str.length())
index ++; //3. 处理符号位
if(str.charAt(index) == '+' || str.charAt(index) == '-'){
sign = str.charAt(index) == '+' ? 1 : -1;
index ++;
} //4. 转变为int,并且避免溢出
while(index < str.length()){
int digit = str.charAt(index) - '0';
if(digit < 0 || digit > 9) break; if(Integer.MAX_VALUE/10 < total || Integer.MAX_VALUE/10 == total && Integer.MAX_VALUE %10 < digit)
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; total = 10 * total + digit;
index ++;
}
return total * sign;
}
}

最新文章

  1. 让nodeJS支持ES6的词法----babel的安装和使用
  2. editplus中使用emmet?
  3. 《android 导入第三方源码jar包遇到的坑》
  4. Windows 7 安装 .netfx 4 卡住
  5. ZOJ 3861 - Valid Pattern Lock
  6. sharepoint 浏览页面导航不正确
  7. ruby 模块 的引入
  8. Spark history-server 配置 !运维人员的强大工具
  9. Linux 关机命令 重启命令
  10. css3新增的background属性
  11. 使用Spring Boot开发Web项目(二)之添加HTTPS支持
  12. Python内置函数(11)——classmethod
  13. (链表) leetcode 21. Merge Two Sorted Lists
  14. pip 安装出现超时问题的解决
  15. GIF录制
  16. Codeforces.392E.Deleting Substrings(区间DP)
  17. 设置macbook休眠模式
  18. HBase 安装设置
  19. 2018&quot;百度之星&quot;程序设计大赛 - 资格赛 1002 子串查询
  20. 20145234黄斐《Java程序设计》第五周

热门文章

  1. CodeForces - 1019D(BZOJ3707圈地):Large Triangle (几何,找面积为S的三角形)
  2. Maven(2)-坐标和依赖
  3. Centos6.5上的iptables
  4. web攻击之五:上传漏洞
  5. TS学习之Symbol
  6. IIS及时回收
  7. qextserialport打不开com10及以上的串口
  8. Structuring Machine Learning Projects 笔记
  9. HTTP 400 错误 编译器错误消息: CS0016
  10. AES算法的c++实现