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 ...
随机推荐
- vue的MVVM
Vue的相关知识有 字符串模板 MVVM 虚拟dom和domdiff,查看下一篇笔记 字符串模板 function render(template, data) { const reg = /\{\{ ...
- SDRAM调试总结
SDRAM的调试总结 1 说明 实验平台: JZ2440 CPU: S3C2440 SDRAM型号: EM63A165TS-6G 2 SDRAM的一些基本概念 2.1 引脚分配 2.2 引脚描 ...
- 前端学习(22)~css问题讲解
你是如何理解 HTML 语义化的? 语义化:指对文本内容的结构化(内容语义化),选择合乎语义的标签(代码语义化). 举例:段落用 p,边栏用 aside,主要内容用 main 标签. 好处: 便于开发 ...
- LeetCode160 相交链表(双指针)
题目: click here!!题目传送门 思路: 1.笨方法 因为如果两个链表相交的话,从相交的地方往后是同一条链表,所以: 分别遍历两个链表,得出两个链表的长度,两个长度做差得到n,然后将长的链表 ...
- Ubuntu不会放弃32位应用程序
Ubuntu 开发人员澄清,人们以为 Ubuntu 将在 Ubuntu 19.10 和后续版本中放弃对运行 32 位应用程序的支持,但“根本不是这种情况”.那么这究竟是怎么一回事呢?前几天 Ubunt ...
- 提交作业 C语言I作业11
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 http://edu.cnblogs.com/campus/zswxy/SE2019-2/homework/10127 我在这个课程的目标 ...
- Django--评论功能实现和用户登录
1.确定实现评论功能的方式 1.第三方社会化评论插件(有言.多说.网易云跟帖等) 优点:可以直接用,紧急时开发快 缺点:评论存储在第三方,第三方挂了就不弄用 2.Django评论库 django-co ...
- 124-PHP类析构函数
<?php class myclass{ //定义一个类 public function __destruct(){ //定义析构方法 echo '析构方法执行.<br />'; } ...
- JAVA-数据类型-复习
JAVA-数据类型-复习 Java中,一共有8种数据类型,4种整型,2种浮点型,1种用于表示Unicode编码的字符单元的字符类型char,1种布尔类型. 整型 类型 存储需求(字节)一个字节包含8个 ...
- 七、React表单详解 约束性和非约束性组件 input text checkbox radio select textarea 以及获取表单的内容
一.约束性和非约束性组件: 非约束性组: MV: <input type="text" defaultValue="a" /> 这个 default ...