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 ...
随机推荐
- C# log4net相关配置说明
添加相关文件到工程 链接: https://pan.baidu.com/s/1o83Juo6 密码: inkg 下载附件, 把里的log4net.dll 和 log4net.config 复制到工程目 ...
- [Codeforces] #603 (Div. 2) A-E题解
[Codeforces]1263A Sweet Problem [Codeforces]1263B PIN Code [Codeforces]1263C Everyone is a Winner! [ ...
- uboot的环境变量
https://www.cnblogs.com/biaohc/p/6398515.html uboot 环境变量实现原理: 首先我们先要搞清楚uboot中环境变量的作用,uboot中环境变量的作用就是 ...
- PHP中strlen和mb_strlen函数的使用方式的不同
(1)strlen 获取字符串长度 (2)mb_strlen 使用方法: int mb_strlen ( string $str [, string $encoding ] )返回给定的字符串 ...
- Vuex源码分析(转)
当我们用vue在开发的过程中,经常会遇到以下问题 多个vue组件共享状态 Vue组件间的通讯 在项目不复杂的时候,我们会利用全局事件bus的方式解决,但随着复杂度的提升,用这种方式将会使得代码难以维护 ...
- C# MQTT M2MQTT
MQTT 入门介绍 MQTT是基于二进制消息的发布/订阅编程模式的消息协议 实现MQTT协议需要客户端和服务器端通讯完成,在通讯过程中,MQTT协议中有三种身份:发布者(Publish).代理(Bro ...
- redis.conf文件配置
最重要三个配置 1. bind 127.0.0.1 需要注释掉这一行,使别的主机可以访问 2. daemonize no 需要改为yes,使其后台运行 3. requirepass foobared ...
- STM32初始
.安装软件.驱动 JLINK驱动.PL2303驱动.MDK4.7(装完破解) .源码编译完要用到的工具 烧录工具MCUISP.串口调试助手 .KEIL建工程模板(2种) (1)寄存器开发:工程文件夹下 ...
- read和write函数的使用
https://blog.csdn.net/qq_33883085/article/details/88667003
- 十、SAP小数需要用引号括起来
一.我们定义一个浮点型f的变量,然后赋值,检查会报错 二.我们把引号括起来之后,就正常了,如下: 三.输出效果如下: 注意:f类型的变量,输出不是准确值