【008-String to Integer (atoi) (字符串转成整数)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  Implement atoi to convert a string to an integer.

  Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

  Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

题目大意

  实现一个atoi函数,将字符串转成整形

  要点:考虑全部的输入情况。

解题思路

  前导字符是+或-或者没有。接下来输入的是数字,数字不能整数能表示的最大或最小数。假设超过就返回相应的最小或者最小的值。

代码实现

public class Solution {
public int atoi(String str) { if (str == null || str.length() == 0) {
// throw new NumberFormatException("Invalid input string: " + str);
return 0;
} // 假设字符串以空格開始
int start = 0; //从開始找第一个不是空格的数
boolean positive = true; // 是否为正数默觉得true if (str.charAt(start) == ' ') {
while (str.charAt(start) == ' ') {
start++;
if (start >= str.length()) { // 输入的全是空格
// throw new NumberFormatException("Invalid input string: " + str);
return 0;
}
}
} if (str.charAt(start) == '-') { // 第一个非空白字符中-
positive = false;
start++;
} else if (str.charAt(start) == '+') {// 第一个非空白字符是+
start++;
} else if (str.charAt(start) >= '0' && str.charAt(start) <= '9') { // 第一个非空白字符是数字
return cal(str, start, true);
} else { // 其他情况就抛出异常
// throw new NumberFormatException("Invalid input string: " + str);
return 0;
} if (start >= str.length()) { // 第一个非空白字符是+或者-但也是最后一个字符
// throw new NumberFormatException("Invalid input string: " + str);
return 0;
} if (str.charAt(start) > '9' || str.charAt(start) < '0') { // +或者-后面接的不是数字
// throw new NumberFormatException("Invalid input string: " + str);
return 0;
} else {
return cal(str, start, positive);
}
} private int cal(String str, int start, boolean positive) { long result = 0;
while (start < str.length() && str.charAt(start) >= '0' && str.charAt(start) <= '9') {
result = result * 10 + (str.charAt(start) - '0'); if (positive) { // 假设是正数
if (result > Integer.MAX_VALUE) {
// throw new NumberFormatException("Invalid input string: " + str);
return Integer.MAX_VALUE;
} } else {
if (-result < Integer.MIN_VALUE) {
// throw new NumberFormatException("Invalid input string: " + str);
return Integer.MIN_VALUE;
}
} start++;
} if (positive) {
return (int) result;
} else {
return (int) -result;
}
}
}

评測结果

  点击图片,鼠标不释放。拖动一段位置,释放后在新的窗体中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/46938417

最新文章

  1. storm基础系列之二----zookeeper的作用
  2. 简洁的jQuery cxMenu 手风琴导航
  3. Java:并行编程及同步使用方法
  4. linux C 获取当前目录的实现(转-Blossom)
  5. ajax分页
  6. saiku 展示优化
  7. MySQL和PHP基础考试错题回顾
  8. css笔记——杂记
  9. Windows NT 之父 - David Cutler
  10. 为什么MOBA、“吃鸡”游戏不推荐用tcp协议——实测数据
  11. Order Management Suite - Pricing and Availability Form Library
  12. logrotate日志处理
  13. How to remove unwant Explorer Context Menu
  14. SpringBoot拦截器
  15. bzoj4518/luogu4072 征途(斜率优化dp)
  16. Vagrant工具的安装
  17. sap 调试工具,修改变量值
  18. JS中DOM以及BOM
  19. PHP——安装wampserver丢失MSVCR110.dll
  20. postgresql 指令

热门文章

  1. CodedUI自己主动化測试及脱离VS独立执行
  2. An existing connection was forcibly closed by the remote host
  3. js中 &#39;枚举&#39; 的使用
  4. AngularJs轻松入门(二)数据绑定
  5. NestedScrollView嵌套ListView可行性总结
  6. Java hashCode(), equals()
  7. PostgreSQL Replication之第一章 理解复制概念(3)
  8. OpenGL编程(五)绘直线以及分析绘直线的算法
  9. POJ - 3984 - 迷宫问题 (DFS)
  10. awk技巧