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. How to change and add some params to request in Laravel controller?

    $request->merge([ 'aae_id' => request('id', 0), 'foo' => 'bar', ]);

  2. TP QQ 微信 微博登录

    use Org\Util\QQconnect; use Org\Util\Wechatauth; use Org\Util\SaeTOAuthV2; use Org\Util\SaeTClientV2 ...

  3. 安装linux子系统, 如何用win10 里面的linux子系统来进行通信

    cd /mnt/d/linux_share即可 就把linux_share这个目录挂载到linux里面了.这样windows和linux可以同时修改和访问这个文件夹里面的内容. 安装:cmd中输入ba ...

  4. spring4.3.9 @ResponseBody中文乱码,全是问号

    <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> < ...

  5. Java类加载机制及自定义加载器

    转载:https://www.cnblogs.com/gdpuzxs/p/7044963.html Java类加载机制及自定义加载器 一:ClassLoader类加载器,主要的作用是将class文件加 ...

  6. 【Java】生成图形验证码

    本章介绍一个能生成比较好看的图形验证码类 生成验证码工具类 package com.util; import java.awt.Color; import java.awt.Font; import ...

  7. Alpha 冲刺 (4/10)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助前后端接口的开发 测试项目运行的服务器环 ...

  8. c#泛型与其他语言的对比(深入理解c#)

    1.同c++模板的对比: c++模板有点像是发展到极致的宏.他们非常强大,但代价就是代码膨胀和不易理解. 在c++中使用一个模板时,会为那一套特定的模板实参编译代码,好在模板实参本来就在源代码中一样. ...

  9. List<T>中,Remove和RemoveAt区别

    Remove删除的是匹配的第一项.比如你的list里面有2个相同的项.那么就删除第一个.后面的不删除,找不到元素和删除失败都返回falseRemoveAt是删除索引下的项

  10. c++四舍五入的新方法

    将原来的数加上0.5,如果是需要进位的加上0.5就进位了,如果不需要进位的加上0.5也小于1,被int型省略掉.