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;
}
}

[Leetcode]008.String to Integer (atoi)的更多相关文章

  1. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

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

  2. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  3. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  4. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  5. No.008 String to Integer (atoi)

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

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

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

  7. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

  8. Leetcode 8. String to Integer (atoi)(模拟题,水)

    8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...

  9. [LeetCode][Python]String to Integer (atoi)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...

随机推荐

  1. BZOJ3812 清华集训2014 主旋律

    直接求出强联通生成子图的数量较难,不妨用所有生成子图的数量减去非强联通的. 非强联通生成子图在所点后满足编号最小的点所在的强联通分量不是全集. 由于$n$很小,我们可以考虑状态压缩. 对于点集$S$, ...

  2. 1030 Travel Plan (30)(30 分)

    A traveler's map gives the distances between cities along the highways, together with the cost of ea ...

  3. Ruby中的include

    Ruby中的include语句应注意以下两个问题: 1.include与文件无关.C语言中,#include预处理指令在编译期将一个文件的内容插入到另一个文件中.Ruby语句只是简单地产生一个指向指定 ...

  4. JS开发中的一些小技巧和方法

    生成指定范围内的随机数 当我们需要获取指定范围(min,max)内的整数的时候,下面的代码非常适合:这段代码用的还挺多的. function setRadomNum(min,max){ return ...

  5. 1.JasperReports学习笔记1-了解JasperReports

    转自:http://www.blogjava.net/vjame/archive/2013/10/12/404908.html JasperReports是一个开源的java报表制作引擎,官网地址:h ...

  6. shell入门-变量

    shell变量分为系统变量和用户自定义变量 查看变量的命令 #env        系统变量 或者 #set    包括env和自定义变量和额外变量 使用变量的命令是 #echo $[变量] //// ...

  7. idea崩溃导致的svn插件丢失问题, maven dependencies视图丢失问题

    Idea丢失Svn解决办法 今天打开Idea,习惯用ctrl+t来更新svn,杯具出现了,快捷键失效了,我觉得可能是其他的什么软件占用了这个快捷键,于是重启了一下,发现还是不行,svn信息怎么没了,c ...

  8. 将gridview 的数据导出EXCEL

    gridview数据 单击“导出EXCEL”按钮后        1.在上面的代码中,先将gridview绑定到指定的数据源中,然后在button按钮(用来做导出到EXCEL的)的事件中,写入相关的代 ...

  9. rlwrap:让控制台程序使用input.rc里的设置

    习惯了vi模式,在bash里配置input.rc可以使平时命令行操作都使用vi习惯,翻页和跳动光标都很方便 但是当运行一些交互式程序时,比如redis-cli,lua等,它们却不按input.rc的配 ...

  10. 12、多空格&多制表符文本之cut域分割终极方案

    解决方法分为如下三步: (1)        使用“tr”命令将制表符转换为空格: (2)        使用“tr”命令将多个重复空格删除,保留一个空格: (3)        使用“cut”命令进 ...