【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

【LeetCode-面试算法经典-Java实现】【008-String to Integer (atoi) (字符串转成整数)】的更多相关文章

  1. [Leetcode] String to integer atoi 字符串转换成整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  2. 8. String to Integer (atoi) 字符串转成整数

    [抄题]: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: T ...

  3. 008 String to Integer (atoi) 字符串转换为整数

    详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...

  4. 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)

    Implement atoi which converts a string to an integer.The function first discards as many whitespace ...

  5. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  6. 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  7. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  8. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  9. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  10. 【Java】 剑指offer(67) 把字符串转换成整数

      本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 请你写一个函数StrToInt,实现把字符串转换成整数这个功能 ...

随机推荐

  1. thinkphp5项目--个人博客(一)

    thinkphp5项目--个人博客(一) 项目地址 fry404006308/personalBlog: personalBloghttps://github.com/fry404006308/per ...

  2. QuerySet和对象的例子 个人记录

    import osif __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE&quo ...

  3. Lamp安装 php-v5.6【ZendGuardLoader】的问题

    Lamp安装 php-v5.6[ZendGuardLoader]的问题 标签(空格分隔):php,linux Apache日志: 就这个问题导致无法解析运行php文件.下面是网上找的解决方案 Zend ...

  4. Windows 绝赞应用(该网站收集了日常好用的工具和软件)

    在我们的电脑使用过程中,或多或少的被流氓软件恶心过.流氓软件之所以这么流氓全是靠他那恐怖的用户数量,基本上形成垄断后,各种流氓行为就一点点体现出来了. 我们也可以选择不用,但对流氓软件来说多你一个不多 ...

  5. 监控rman备份

    1.服务会话关联通道设置 set COMMAND ID 命令 2.查询V$PROCESS和V$SESSION 决定会话对应的RMAN的通道 3.查询V$session_LONGGOPS监控备份集和复制 ...

  6. IE11 补丁 KB2929437[已过期]

    2014年4月 请更新此补丁 KB2929437 开发人员工具有重要更新 DOM 面板右侧新增 "更改"面板,用于记录调试时修改的 CSS Rules: JS 调试面板,新增 so ...

  7. PostgreSQL环境中查看SQL执行计划示例

    explain   analyze ,format,buffers, format :TEXT, XML, JSON, or YAML. EXPLAIN (ANALYZE,buffers,format ...

  8. php基础:implode()函数 和exlplode函数

    1implode() 函数返回一个由数组元素组合成的字符串. 注释:implode() 函数接受两种参数顺序.但是由于历史原因,explode() 是不行的,您必须保证 separator 参数在 s ...

  9. 把ISO文件当作光盘挂载

    当不能挂载光盘或者U盘时候,只需要把ISO文件传到某个目录中,比如/data下,即可挂载,如下所示:   mount -o loop /data/rhel-server-6.3-x86_64-dvd. ...

  10. centOS7下 安装nodejs+nginx+mongodb+pm2部署vue项目

    一.购买服务器并远程连接 1.购买服务器和域名 可以选择阿里云或者是其他的厂商的服务器.然后会获得服务器ip地址,用户名和密码. 购买域名,将域名绑定到ip地址上. 2.下载xshell,winscp ...