[leetcode 8] String to Integer
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.
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.
- 前面有空格
- 整型越界
- 有小数点
- 判断正负
- 非数字字符的处理,包括数字前面和后面
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的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- leetcode day6 -- String to Integer (atoi) && 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 ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- LeetCode 8 String to Integer (string转int)
题目来源:https://leetcode.com/problems/string-to-integer-atoi/ Implement atoi to convert a string to an ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- LeetCode 8. String to Integer (atoi) (字符串到整数)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
随机推荐
- memcache的add和set区别
add可以做memcache锁 使用场景:用户兑换商品,在网络不好的情况下,点击多次,set会将多次提交全纪录下来,add只会记录一次
- JDK 之资源文件管理
JDK 之资源文件管理 JDK 规范目录(https://www.cnblogs.com/binarylei/p/10200503.html) 一.文件资源 user.home 用户目录,如 Linu ...
- python 字典遍历
dic1={"name":"kxb","age":28}for k,v in dic1.items(): print(k+",,, ...
- mybatis学习 十五 resultMap标签 一对多
多次查询,非联合查询版本 <resultMap type="teacher" id="techMap"> <id column="i ...
- 泛型c#(深入理解c#)
1.泛型带来的好处非常像静态语言较之动态语言的优点:更好的编译时检查,更多在代码中能直接表现的信息,更多的IDE支持,更好的性能.泛型的好处之一就是在编译时执行更多的检查,所以等到编译不在报错时,就极 ...
- 44、WebStrom下载和破解
WebStrom下载地址: http://www.pc6.com/mac/112553.html WebStrom 2017激活破解(http://blog.csdn.net/it_talk/arti ...
- Python脚本语言第一行的写法
脚本语言的第一行,目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它,就这么简单 #!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python ...
- Java日期时间使用总结[转载]
Java日期时间使用总结 一.Java中的日期概述 日期在Java中是一块非常复杂的内容,对于一个日期在不同的语言国别环境中,日期的国际化,日期和时间之间的转换,日期的加减运算,日期的展示格式 ...
- Vbs脚本简单使用
之前在做项目时用到了一点vbs脚本,记录下. C++程序调用vbs脚本 System(vbs路径 参数); //空格隔开 Vbs脚本 '''''Vbs脚本解析参数 Set objArgs = Wscr ...
- c语言const和c++const
1.常量 常量是指值不能被改变的量,又叫做字面值 1.1常量分类 1)字符常量:'a', 'A', '*'. 2)字符串常量:"helloworld","ilovechi ...