1 题目:

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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

spoilers alert... click to show requirements for atoi.

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

Hide Tags

Math String

 
2 思路
按照字符串变成字符数组一位一位处理。
考虑以下特殊情况:
  • 前面有空格
  • 整型越界
  • 有小数点
  • 判断正负
  • 非数字字符的处理,包括数字前面和后面
3 代码:
    public int myAtoi(String str) {
if(str.equals("")) return 0;
//1 handle space
str = str.trim();
char[] chars = str.toCharArray();
int len = str.length();
long number = 0;
boolean sign = true; // handle sign
if(chars[0] >= '0' && chars[0] <= '9'){
number += chars[0] - 48;
}else if(chars[0] == '-'){
sign = false;
}else if(chars[0] == '+'){
sign = true;
}else{
return 0;
} // convern to number
for(int i = 1; i < len; i++){
if(chars[i] >= '0' && chars[i] <= '9'){
number = number*10 + (chars[i] - 48);
//handle overflow
if(number > Integer.MAX_VALUE){
return sign ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
}else{
number = sign ? number : -number;
return (int)number;
}
}
number = sign ? number : -number; return (int)number;
}
 

[leetcode 8] String to Integer的更多相关文章

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

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

  2. 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 ...

  3. 【leetcode】String to Integer (atoi)

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

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

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

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

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

  6. LeetCode 8 String to Integer (string转int)

    题目来源:https://leetcode.com/problems/string-to-integer-atoi/ Implement atoi to convert a string to an ...

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

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

  8. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  9. LeetCode 8. String to Integer (atoi) (字符串到整数)

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

  10. [LeetCode] 8. String to Integer (atoi) 字符串转为整数

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

随机推荐

  1. Vue 不同环境配置相应的 API 地址

    我使用的是 Vue + ElementUI 进行构建的项目,在开发过程中,使用的是前后端分离的模式,所以经常会出现多环境配置信息,尤其是 Api 接口地址的配置,如果在代码中使用判断的方式,感觉不友好 ...

  2. @PostConstruct和@PostConstruct 注解 及ehcache 缓存 执行过程 小记

    @PostConstruct 和@PostConstruct 注解 从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion):@PostConstr ...

  3. 【UI测试】--美观与协调性

  4. 第一个C#语言

    第一个C#程序 .NET和C#的区别 1.  C#只是.NET的一部分,.NET不止包含C# 2.  C#是一种程序语言,.NET是一个平台.框架 IDE环境:vs 2012 VS2012的窗口结构 ...

  5. Unity2017新功能Sprite Atlas详解

    Sprite Atlas(精灵图集)Sprite Atlas 针对现有的图集打包系统Sprite Packer在性能和易用性上的不足,进行了全面改善.除此之外,相比Sprite Packer,Spri ...

  6. Oracle导入的常见语句

    登录sql > sqlplus / as sysdba 创建表空间sql > create tablespace TABLESPACE datafile 'e:\tables1.dbf' ...

  7. RF 和 GBDT联系和区别

    1.RF 原理 用随机的方式建立一个森林,森林里面有很多的决策树,随机森林的每一棵决策树之间是没有关联的.在得到森林之后,当有一个新的输入样本进入的时候,就让森林中的每一棵决策树分别进行一下判断,看看 ...

  8. php-fpm 的 pm.start_servers 参数调整

    大家注意一下 在 php-fpm 的配置文件中, pm.start_servers 必须是介于  pm.min_spare_servers 和  pm.max_spare_servers  这个值之间 ...

  9. jquery checkbox反复调用attr('checked', true/false)只有第一次生效 Jquery 中 $('obj').attr('checked',true)失效的几种解决方案

    1.$('obj').prop('checked',true) 2. $(':checkbox').each(function(){ this.checked=true; }) 为什么:attr为失效 ...

  10. excel的小bug

    http://muchong.com/html/201710/3913047.html Excel也有相同现象,试着计算:exp(-1.5^2),exp(0-1.5^2),exp(-(1.5)^2)看 ...