【leetcode刷题笔记】String to Integer (atoi)
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.
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.
题解:思路很简单,就是要注意的细节很多:
- str中整数的前面可能有很多空格,要用str = str.trim(); 去掉,去掉之后还要判断str是否为空了,为空返回0;
- str中整数可能带有'+'或者'-',也可以不带,带有负号的时候要单独处理;
- str中整数后面可能还有乱七八槽的非数字符号,直接忽略,所以在遍历过程中如果遇到这些符号,说明整数部分遍历结束,要推出循环。
- str转换出来的整数有可能大于Integer.MAX_VALUE,此时需要返回Integer.MAX_VALUE;也有可能小于Integer.MIN_VALUE,此时需要返回Integer.MIN_VALUE。
代码如下:
public class Solution {
public int atoi(String str) {
if(str == null || str.length() == 0)
return 0;
str = str.trim();
if(str.length() == 0)
return 0;
int kepeler = 0;
boolean isNeg = false;
if(str.charAt(kepeler) == '-'){
isNeg = true;
kepeler++;
}
else if(str.charAt(kepeler) == '+')
kepeler++;
long answer = 0;
for(;kepeler < str.length();kepeler++){
if(str.charAt(kepeler) < '0' || str.charAt(kepeler) > '9')
break;
answer = answer*10+str.charAt(kepeler) - '0';
}
if(isNeg){
answer *= -1;
if(answer < Integer.MIN_VALUE)
return Integer.MIN_VALUE;
return (int)answer;
}
else {
if(answer > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
return (int)answer;
}
}
}
【leetcode刷题笔记】String to Integer (atoi)的更多相关文章
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- 【leetcode刷题笔记】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...
- LeetCode【8】. String to Integer (atoi) --java实现
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- 【leetcode刷题笔记】Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- 【leetcode刷题笔记】N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- LeetCode刷题笔记(3)Java位运算符与使用按位异或(进制之间的转换)
1.问题描述 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 算法应该具有线性时间复杂度并且不使用额外空间. 输入: [4,1,2,1,2] 输 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
随机推荐
- 【Atheros】minstrel速率调整算法源码走读
先说几个辅助的宏,因为内核不支持浮点运算,当然还有实现需要,minstrel对很多浮点值做了缩放: /* scaled fraction values */ #define MINSTREL_SCAL ...
- iOS 平台如何使用 TestFlight 进行 Beta 测试
使用 TestFlight,你可以向测试人员发布你 App 的 prerelease 版本来收集反馈信息,为将来发布 App 的正式版做准备.现在 TestFlight 是一个可选功能,你也可以不使用 ...
- PHP面试题总结
2017年5月15日19:20:26 1.请用最简单的语言告诉我PHP是什么? PHP全称:Hypertext Preprocessor,是一种用来开发动态网站的服务器脚本语言. 2. 面试题地址:h ...
- TextView实现打印机效果 ,字符串逐字显示
https://github.com/lygttpod/AndroidCustomView/blob/master/app/src/main/java/com/allen/androidcustomv ...
- Collective Mindsets (medium) (逻辑题)
B - Collective Mindsets (medium) Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I6 ...
- [转]React表单无法输入原因----约束性和非约束性组件
转自:http://blog.csdn.net/lihongxun945/article/details/46730835 表单是前端非常重要的一块内容,并且往往包含了错误校验等逻辑. React对表 ...
- apache虚拟主机配置: 设置二级目录访问跳转
<VirtualHost *:> DocumentRoot "d:/www/abc" ServerName www.abc.com Alias /course &quo ...
- C++学习笔记30,指针的引用(2)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/guang_jing/article/details/32910093 能够创建不论什么类型的引用,包 ...
- 禁用chrome浏览器的cookie
Chrome: 1.打开chrome浏览器,点击右上角的“自定义和控制Google Chrome”按钮 2.在下拉菜单中选择设置 3.点击设置页底部的“显示高级设置...” 4.在隐私设置下,点击“内 ...
- iOS UILabel 省略号 不变色 问题处理
在我们公司 应用 4.1版本 我发现一个很有趣的问题 , 当我修改 label 的 textColor (默认单行情况)为黑色之外的颜色 省略号依然为黑色, 这个在iOS 7 iOS8.1 i ...