LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi)
- Total Accepted: 112863
- Total Submissions: 825433
- Difficulty: Easy
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.
思路:
本题的难度倒不大,主要是考察的细心程度和对和各种可能的情况是否考虑清楚。题主也是通过三四次提交失败之后才accepted。
主要考虑的情况有以下几种:
- 空串或str == null
- 字符串开头有连续的空格
- 非空格的第一个字符为“+”或“-”或0或其他情况
- 字符串长度可能非常长,甚至转换过来之后超过long类型的范围
- 当遇到非正常字符时,输出之前的结果,eg:-123a245,输出结果为-123
- 对于输出结果超出int类型范围的输出边界值,也就是说输出结果大于int类型最大值,则输出Integer.MAX_VALUE,当输出结果小于int类型最小值时,输出Integer.MIN_VALUE
暂时只考虑了这么多种吧,代码如下:
- public int myAtoi(String str) {
- //对null和空串情况进行判断
- if(str == null || str.length() == 0){
- return 0 ;
- }
- //截除字符串的前面的空格
- char [] arr = str.trim().toCharArray() ;
- int temp ; //乘子,表示正负
- int index ; //表示最高位开始的index
- //判断非空有效位的首位的情况
- if((arr[0] == '-') && (arr.length > 1)){
- temp = -1 ;
- index = 1 ;
- }else if((arr[0] <= '9') && (arr[0] >= '0')){
- temp = 1 ;
- index = 0 ;
- }else if((arr[0] == '+') && (arr.length > 1)){
- temp = 1 ;
- index = 1 ;
- }else{
- return 0 ;
- }
- long res = 0 ;
- for(int i = index ; i < arr.length ; i++){
- /*判断每一位是否是数字,不是则直接截断已经处理的结果
- * 考虑到int类型的最长有效位数是10位,加上符号位11位,这里再附加一位保险位,
- * 超过12位的肯定超出了有效范围我们不继续处理后面的,直接截断*/
- if((arr[i] <= '9') && (arr[i] >= '0' && i < 13)){
- res = res*10 + temp*(arr[i]-'0') ;
- }else{
- break ;
- }
- }
- //判断输出的结果是否超出int类型的范围
- if(res > Integer.MAX_VALUE ){
- return Integer.MAX_VALUE ;
- }else if(res < Integer.MIN_VALUE){
- return Integer.MIN_VALUE ;
- }else{
- return (int)res ;
- }
- }
LeetCode--No.008 String to Integer (atoi)的更多相关文章
- 【LeetCode】008. String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [Leetcode]008.String to Integer (atoi)
public class Solution { public int myAtoi(String str) { int index = 0, sign = 1, total = 0; //1. 边界条 ...
- 【一天一道LeetCode】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【leetcode】8. String to Integer (atoi)
题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
随机推荐
- keepalive高可用
Keepalived软件起初是专门为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此,Keepalived除了能够管理LVS软 ...
- 281. Zigzag Iterator z字型遍历
[抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...
- android 去掉activity的切换动画
在styles.xml文件中增加样式代码: <style name="AppTheme" parent="Theme.AppCompat.Light.NoActio ...
- [快速幂][NOIP2012]转圈游戏
转圈游戏 题目描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置, ...
- python 03 字符串详解
1.制表符 \t str.expandtabs(20) 可相当于表格 2.def isalpha(self) 判断是否值包含字母(汉字也为真),不包含数字 3.def isdecimal(se ...
- linux 启动tomcat卡很久的问题
解决办法:打开$JAVA_PATH/jre/lib/security/java.security这个文件,找到下面的内容: securerandom.source=file:/dev/random 替 ...
- centos7 安装搜狗输入法
1.root权限,卸载 ibus : yum remove ibus 2.加入EPEL源 sudo yum install epel-release 3.添加mosquito-myrepo源 su ...
- this guy gonna be a daddy
时间真快,媳妇儿怀孕了两个月了. 2016年2月10日,在横峰血检后确定媳妇儿怀孕后,我心情激动得要飞起来一样,那时,我在心里告诉自己不是个孩子了,我自己即将成为孩子的父亲.当时只顾着自个儿激动,已经 ...
- .gitignore无效解决方案以及git rm和rm的区别
一. gitignore 先来了解一下gitignore的常用语法 斜杠“/”表示目录, 是否已斜杠开头有很大区别,如 /build 与 build/ 的区别:其中 build/ 表示不管在哪个位置的 ...
- ota升级动画背景色修改
https://wenku.baidu.com/view/0d63ad25192e45361066f549.html https://blog.csdn.net/huangyabin001/artic ...