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 ...
随机推荐
- python2 使用matplotlib
背景 由於pyh在python3上沒法兒用,所以只能在python2上用pyh2 相應地也要在python2上使用matplotlib 下載 有兩種方法,pip & dnf pip爲: pip ...
- java之接口
背景 为了防止[多重继承]:(在面向对象的编程语言(例如java)中,指一个类可以同时继承多个父类的行为和特征功能)所引发的"致命方块",出现了接口. 使用 定义 public ...
- 【机器学习】集成学习之sklearn中的xgboost基本用法
原创博文,转载请注明出处!本文代码的github地址 博客索引地址 1.数据集 数据集使用sklearn自带的手写数字识别数据集mnist,通过函数datasets导入.mnist共1797个样 ...
- 【javascript 】组合式继承
开发人员采用的js的继承结构 function inheritPrototype(subType, superType) { var prototype = object(superType.prot ...
- mysql 时间转换 用EXCEL实现MySQL时间戳格式和日期格互转
今天项目表中需要导入好几w条数据 ,但日期由两个一个是标准时间一个为时间戳,程序中搜索是根据时间戳来搜索的,所以在网上翻箱倒柜的终于找到解决之道了,利用excel转换时间戳 时间戳转成正常日期的公式: ...
- HDU - 6437:Videos (裸的费用流)
...懒得说什么了 #include<bits/stdc++.h> using namespace std; ; <<;int To[maxn],Laxt[maxn],Next ...
- BZOJ4145 [AMPPZ2014]The Prices
题意 你要购买m种物品各一件,一共有n家商店,你到第i家商店的路费为d[i],在第i家商店购买第j种物品的费用为c[i][j],求最小总费用. \(n \leq 100,m \leq 16\) 分析 ...
- 读懂IL代码就这么简单 ---- IL系列文章
读懂IL代码就这么简单 (一) 读懂IL代码就这么简单(二) 读懂IL代码就这么简单(三)完结篇 出处:http://www.cnblogs.com/zery/tag/IL%20%E7%B3%BB%E ...
- Java列表分页查询结果导出到CSV文件,导入CSV文件并解析
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- 空间的搜索与R树
在现实地图应用中,有个比较常见的问题,比如,你到了一个地方,想查查附近1km内有什么饭店. 这时地图应用就可以马上查询出周围有什么饭店,如果让你设计,你会怎么设计.假设局限在中国的地图上,共有1000 ...