Q8:String to Integer (atoi)
8. String to Integer (atoi)
官方的链接:8. String to Integer (atoi)
Description :
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.
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.
问题描述
atoi (表示 ascii to integer)是把字符串转换成整型数的一个函数,函数会扫描参数nptr字符串,跳过前面的空白字符(例如空格)等,直到遇上数字或正负符号才开始做转换,而在遇到非数字或字符串结束时才结束转换,并将结果返回。如果 nptr不能转换成 int、nptr为空字符串,那么将返回 。溢出则返回INT_MAX或者INT_MIN。
思路
一开始理解有误,以为是各种字符都有。后来参考了下官网的Discuss,发现大致为:跳过空格,选择可选的前导+或者-,后面是尽可能多的数字,直到结束或者遇到非数字。以下的代码就当是一种参考。
项目中确实遇到过这种场景:数值型的字段使用正负号+定长字符,然后对字符串进行解析。 这种场景下会有正负号、符号后的0和实际的数字,比如定长10位,第1位是正负号:+066666660,实际上就是66666660。
public class Q8_StringToInteger {
public int myAtoi(String str) {
if (null == str || str.isEmpty()) {
return 0;
}
int result = 0, sign = 1, index = 0, len = str.length();
while (index < len && str.charAt(index) == ' ') {
index++;
}
if (index == len) {
return 0;
}
if (str.charAt(index) == '+' || str.charAt(index) == '-') {
sign = str.charAt(index++) == '-' ? -1 : 1;
}
while (index < len && str.charAt(index) <= '9' && str.charAt(index) >= '0') {
//溢出判断
if (result > Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && str.charAt(index) - '0' > 7)) {
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
result = result * 10 + str.charAt(index++) - '0';
}
return result * sign;
}
}
Q8:String to Integer (atoi)的更多相关文章
- leetcode:String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- No.008:String to Integer (atoi)
问题: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- 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 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【Leet Code】String to Integer (atoi) ——常考类型题
String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement ato ...
随机推荐
- MinGW 安装与简单实例
MinGW的下载 链接: https://pan.baidu.com/s/1JiZoyFHUWoeaxCQcUXCKgg 密码: myh3 MinGW的安装 基本上都是按提示的点击下一步操作 接下来修 ...
- react 如何引入打印控件 CLodop
下载插件,官网地址 http://www.lodop.net/download.html ,选择综合版,解压下载的文件.直接点击 安装,很简单,就不一一说明了. 复制下面几个文件,到react项目中 ...
- NO5 grep-head-tail命令
·*****grep:#过滤需要的内容(linux三剑客). -v:排除内容.eg:grep -v oldboy test.txt ·head: #头,头部.读取文 ...
- HDU - 6006 Engineer Assignment (状压dfs)
题意:n个工作,m个人完成,每个工作有ci个阶段,一个人只能选择一种工作完成,可以不选,且只能完成该工作中与自身标号相同的工作阶段,问最多能完成几种工作. 分析: 1.如果一个工作中的某个工作阶段没有 ...
- HDU - 6181 Two Paths(次短路)
题意:求次短路. 分析:关键是情况讨论. LL tmpd = x.d + e.dist; 以下情况对应的更新结果 1.tmpd(2) < 最短路(3) < 次短路(4)-------> ...
- 微信小程序如何刷新当前界面
在微信小程序开发的过程中,在一个页面中对数据操作之后我们大多数时间都需要刷新一下当前界面以把操作之后的结果显示出来,但是如何在执行操作后进行本页面的刷新就成了一个问题很大但是很需要的操作.下面介绍一下 ...
- pyinstaller打包PySide2写的GUI程序,调用ffmpeg隐藏CMD控制台解决方案
1 问题描述 使用PySide2写了一个GUI程序,调用ffmpeg命令行工具,做简单的批量视频处理(调整帧宽度.帧高度.视频变速.降低视频码率达到限制视频大小),使用了ffmpeg. ffmpeg- ...
- Storm 流式计算框架
1. 简介 是一个分布式, 高容错的 实时计算框架 Storm进程常驻内存, 永久运行 Storm数据不经过磁盘, 在内存中流转, 通过网络直接发送给下游 流式处理(streaming) 与 批处理( ...
- HZNU-ACM寒假集训Day12小结 数论入门
符号说明 a|b a整除b (a,b) a与b的最大公因数 [a,b] a与b的最小公倍数 pα||a pα|a但pα+1∤a a≡b(mod m) a与b对模m同余 a ...
- MongoDB Projection
版权所有,未经许可,禁止转载 章节 MongoDB 入门 MongoDB 优势 MongoDB 安装 MongoDB 数据建模 MongoDB 创建数据库 MongoDB 删除数据库 MongoDB ...