LeetCode题解 #8 String to Integer (atoi)
又是一道恶心的简单题。
一开始没想到这么多情况的,幸好LeetCode是个很人性化的oj,能让你知道你在哪个case上错了,否则一辈子都过不了。
考虑不周到只能一个个补了。
列举一下恶心的case
//" 010"
//" +004500"
//" -0012a42"
//"2147483648"
//" b11228552307"
//"18446744073709551617"
//" r11384376420"
所有要注意的情况是:
- 字符串前置的空格需要清除;//读掉所有空格
- 无视前导0
- 数字前最多允许1个"+"或"-",出现多个时返回0;
- 数字中出现不为数字的符号时即认为数字结束,比如"252ab","252-2","252 09"都判定为252;//找到第一个非数字的地方
- 数字大于INT_MAX时,返回INT_MAX,小于INT_MIN时,返回INT_MIN。
其实没有考到什么算法,就是if else而已
一开始的代码(觉得有必要贴出来,让以后的自己看看-_-)
public static int myAtoi(String str) {
long temp = 0;
//空白
if(str.length()==0)
return 0;
//非数字
//单个数字
else if(str.length()==1){
//System.out.println((int)str.charAt(0)-48);
if(48<=str.charAt(0)&&str.charAt(0)<=57)
return (int)str.charAt(0)-48;
else
return 0;
}
//多个数字
else{
//读掉开头的空格
int start = 0;
for(start=0;start<str.length();start++)
if(str.charAt(start)!=' ')
break;
if(start==str.length()){
System.out.println("all blank");
return 0;
}
//除开头外判断是否全是数字
//找到非数字的地方
int end = 0;
for(end=start+1;end<str.length();end ++)
if(!(48<=str.charAt(end)&&str.charAt(end)<=57)){
System.out.println(str.charAt(start));
System.out.println("not num");
System.out.println("end-->"+end);
break;
}
if(!(48<=str.charAt(start)&&str.charAt(start)<=57)&&!(48<=str.charAt(start+1)&&str.charAt(start+1)<=57)){
System.out.println("not num");
return 0;
}
if(str.charAt(start)=='+'){
System.out.println(str.charAt(start));
if(end-start>=13)
return 2147483647;
if(start+1==end-1)
return ((int)str.charAt(start+1)-48);
for(int i=start+1;i<end;i++){
//System.out.println("temp--->"+temp);
//System.out.println((int)str.charAt(i)-48);
if(str.charAt(i)==' ')
continue;
temp = temp*10+((int)str.charAt(i)-48);
//System.out.println("now --->"+temp);
}
//System.out.println(temp);
System.out.println("zhengshu1");
if(temp>=2147483647)
return 2147483647;
if(temp<=-2147483648)
return 2147483647;
return (int)temp;
}
else if(str.charAt(start)=='-'){
if(start+1==end-1)
return -((int)str.charAt(start+1)-48);
if(end-start>=13)
return -2147483648;
for(int i=start+1;i<end;i++){
//System.out.println("temp--->"+temp);
//System.out.println((int)str.charAt(i)-48);
if(str.charAt(i)==' ')
continue;
temp = temp*10+((int)str.charAt(i)-48);
//System.out.println("now --->"+temp);
}
System.out.println(temp);
temp = -temp;
System.out.println("fushu");
if(temp>=2147483647)
return -2147483648;
if(temp<=-2147483648){
System.out.println("fushu min ");
return -2147483648;
}
return (int)temp;
}
else{
System.out.println(start-end);
if(end-start>=13)
return 2147483647;
System.out.println(str.charAt(start));
System.out.println(end);
if(!(48<=str.charAt(start)&&str.charAt(start)<=57))
return 0;
if(start==end-1)
return ((int)str.charAt(start)-48);
for(int i=start;i<end;i++){
//System.out.println("temp--->"+temp);
//System.out.println((int)str.charAt(i)-48);
if(str.charAt(i)==' ')
continue;
temp = temp*10+((int)str.charAt(i)-48);
//System.out.println("now --->"+temp);
}
System.out.println(temp);
System.out.println("zhengshu2");
if(temp>=2147483647)
return 2147483647;
if(temp<=-2147483648)
return 2147483647;
return (int)temp;
}
}
}
以下是综合了所有情况但是高度简洁的代码:
int myAtoi(string str) {
int length = str.size();
long long ret_64 = 0;
int op = 1; int p = 0;
while (str[p] == ' ') ++p;
if (str[p] == '+' || str[p] == '-')
{ if (str[p] == '-') op = -1; p++; }
for (int i = p; i < length; ++i)
if ('0' <= str[i] && str[i] <= '9')
{ ret_64 = ret_64 * 10 + (str[i] - '0');
if ((op == -1 && ret_64 > 2147483648LL))
return -2147483648;
if ((op == 1 && ret_64 > 2147483647LL))
return 2147483647; }
else
break;
return (int)ret_64 * op; }
总结一下:
没有技巧可言,就是常见的判断溢出(用long),一个个构建起一个数字(temp = temp*10+((int)str.charAt(i)-48);),字符转数字用ASCII码。
LeetCode题解 #8 String to Integer (atoi)的更多相关文章
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【一天一道LeetCode】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- 【LeetCode】008. String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【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 ...
- LeetCode(8) - String to Integer (atoi)
虽然是easy,却是比较繁琐的一道题,需要考虑各种边界条件.在WA了好几遍之后,才把各种边界条件给补全.需要考虑到的因素如下: 输入不合法字符,非"0-9",对于首位,合法字符还包 ...
- 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 ...
随机推荐
- grunt实现修改代码实时刷新浏览器
grunt例子:https://github.com/Aquarius1993/gruntDemo grunt 实时刷新1: 1.安装chrome浏览器插件:liveReload ...
- Makefile中怎么使用Shell if判断
/********************************************************************* * Makefile中怎么使用Shell if判断 * 说 ...
- vuex(一)mutations
前言:vuex的使用,想必大家也都知道,类似于状态库的东西,存储某种状态,共互不相干的两个组件之间数据的共享传递等.我会分开给大家讲解vuex的使用,了解并掌握vuex的核心(state,mutati ...
- Python中的collections模块
Python中内置了4种数据类型,包括:list,tuple,set,dict,这些数据类型都有其各自的特点,但是这些特点(比如dict无序)在一定程度上对数据类型的使用产生了约束,在某些使用场景下效 ...
- 【剑指offer】09-2跳台阶,C++实现
原创博文,转载请注明出处! # 本文是牛客网<剑指offer>刷题笔记 1.题目 # 一只青蛙一次可以跳1级台阶,也可以跳2级.求该青蛙跳n级的台阶总共有多少种跳法. 2.思路 # 跳0级 ...
- iOS UIWebView 中 js调用OC 打开相册 获取图片, OC调用js 将图片加载到html上
线上html <!DOCTYPE html> <html> <head> <title>HTML中用JS调用OC方法</title> < ...
- margin top 无效
常出现两种情况: (一)margin-top失效 两个层box1和box2,box1具有浮动属性,box2没有,这时候设置box2的上边距margin-top没有效果. 解决办法: 1.box2增加f ...
- php7+Redis+Windows7安装 (phpstudy)
1.首先去github网站上下载https://github.com/dmajkic/redis/downloads: 2.根据实际情况,将64bit的内容cp到自定义盘符目录,如D:\Redis; ...
- C#封装的一个JSON操作类
using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...
- nginx 支持ie 6 等低版本https 的配置
nginx 配置 https 支持ie6 等低版本(主要是加密套件的问题) server { listen 443 ssl; server_name itapiway.demo.com; ssl_ce ...