【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. html5播放m3u8视频,web端看直播

    https://github.com/jiqing9006/hLive <!DOCTYPE html> <html> <head> <meta charset ...

  2. hdu 2018 - 递推

    dp[i][1..4] 第i年时年龄为1234的牛的数目 */ #include <cstdio> #include <cstring> ; ]; int main(){ me ...

  3. elementui的时间选择器开始时间和结束时间的限制

    开始时间不能大于结束时间 html代码部分 方法部分 开始时间和结束时间可以选同一天 <template> <div class="range-wrapper"& ...

  4. 转移顺序的艺术 luogu4394 + lougu2966 + luogu3537

    lougu4394: N个政党要组成一个联合内阁,每个党都有自己的席位数. 现在希望你找出一种方案,你选中的党的席位数要大于总数的一半,并且联合内阁的席位数越多越好. 对于一个联合内阁,如果某个政党退 ...

  5. 20180929 北京大学 人工智能实践:Tensorflow笔记01

    北京大学 人工智能实践:Tensorflow笔记 https://www.bilibili.com/video/av22530538/?p=13 (完)

  6. 使用glPushMatrix和glPopMatrix的原因

    转自 百度百科   glPushMatrix 函数将当前矩阵堆栈推送,通过一个,复制当前矩阵. 这就是后 glPushMatrix 的调用堆栈的顶部矩阵是它下面的相同的.   1. 原理讲解 终于明白 ...

  7. ArcSDE学习笔记--------增、删、改、查操作

    建立连接 package org.lq.ssm.gp.controller; import com.esri.sde.sdk.client.SeConnection; import com.esri. ...

  8. EF框架—Database-First

    ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案,现已经包含在 Visual Studio 2008 S ...

  9. iOS数字媒体开发浅析

    概述 自然界中的所有看到的听到的都是模拟信号,模拟信号是随时间连续变化,然而手机电脑等信息都属于数字媒体,它们所呈现的内容就是把自然界中这些模拟信号转换成数字信号然后再传递给我们.数字信号不是连续的是 ...

  10. BNU 34974 MATLAB大法好

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=34974 MATLAB大法好  Time Limit: 8000ms Memory Limi ...