【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. MyEclipse完好提示配置

    MyEclipse完好提示配置 一般的,MyEclipse中的提示以"."后进行提示,不是非常完好.如今.改动提示配置,让提示更完好. 详细操作例如以下: 1.打开MyEclips ...

  2. thinkphp5项目--个人博客(二)

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

  3. 登录那些事儿+ Session原理

    http://cnodejs.org/topic/5671441a1d2912ce2a35aaa1  登录那些事儿 http://www.jianshu.com/p/2b7c10291aad Sess ...

  4. ZooKeeper Recipes and Solutions

    原文地址:http://zookeeper.apache.org/doc/current/recipes.html 参考:https://zookeeper.apache.org/doc/trunk/ ...

  5. spring的quartz定时任务

    一.版本: 1.spring:4.1.7:    2.quartz:2.2.1: 二.基于ssm项目: 1.引入jar包:quartz-2.2.1.jar:spring所需包. 2.说明:quartz ...

  6. 离奇失踪的WM_HOTKEY消息--浅析WIN32消息队列

    故事的开端有些平淡,眼红于XXX小程序,认为写完该程序就有了和心仪的妹子多相处的机会,必须搞,必须酷,按钮不能有,界面得隐藏,这就想到了全局快捷键. 注册调用RegisterHotKey(m_hWnd ...

  7. Python3基础笔记--基础知识

    目录: 一.变量问题 二.运算符总结 三.字符串问题 四.数据结构 五.文件操作 一.变量问题 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间.它自始至终都是在内存中活动,只有指明 ...

  8. 了解Linux的基础知识和一般概念

    1.GNU和GPL    GNU计划(又称革奴计划),是由Richard Stallman(理查德·斯托曼)在1983年9月27日公开发起的自由软件集体协作计划.它的目标是创建一套完全自由的操作系统. ...

  9. css columns 与overflow结合的问题

    想实现上面这样分栏,并且溢出滚动的效果.可是自己下面的代码只能得到横向滚动条.觉得出现这个情况觉得还蛮有意思的,特地记录一下. <li v-for="(item,index) in s ...

  10. POJ-1001 Exponentiation 高精度算法

    题目链接:https://cn.vjudge.net/problem/POJ-1001 以前写过一个高精度乘法,但是没有小数点,实现起来也没什么难得, 现在把代码都般过来,等会把旧电脑弄一弄,暂时就不 ...