【LeedCode】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.
列举需要注意的集中情况
1、去空格 trim()
2、首字母为“+”或者“-”,则要记录下来。设一个flag变量,用于存储符号位
3、数字前出现了非数字字符,则返回0
4、在数字中出现了非数字字符,则返回前面的一些数字字符组成的数,后面的直接舍弃
5、存在数字越界的情况,所以要对最后得到的数字进行判断
public static int atoi(String str){
if(str == null || str.length() == 0)
{
return 0;
}
str = str.trim();
int flag = 1;
int start = 0;
long res = 0;
if(str.charAt(0)=='+'){
flag = 1;
start++;
}else if(str.charAt(0)=='-'){
flag = -1;
start++;
}
for(int i = start;i<str.length();i++){
if(Character.isDigit(str.charAt(i))){
return (int)res*flag;
}
res = res*10 + str.charAt(i)-'0';
}
if (flag == 1 && res > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
if (flag == -1 && (-1) * res < Integer.MIN_VALUE)
return Integer.MIN_VALUE;
return (int)(flag * res);
}
【LeedCode】String to integer(atoi)的更多相关文章
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- LeetCode【8】. String to Integer (atoi) --java实现
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- 【Leetcode】【Easy】String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【Leetcode-easy】String to Integer(atoi)
题目要求:字符串->整型 * 1. 首先需要丢弃字符串前面的空格. * 2. 然后可能有正负号(注意只取一个,如果有多个正负号,那么说这个字符串是无法转换的,返回0.比如测试用例里就有个“+-2 ...
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- 【Leet Code】String to Integer (atoi) ——常考类型题
String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement ato ...
- 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)
Implement atoi which converts a string to an integer.The function first discards as many whitespace ...
- 【leetcode刷题笔记】String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
随机推荐
- 总结const
int b; const int *a=&b; int const * a=&b; int * const a =&b; const int *const a=&b; ...
- Jlink 软件断点和硬件断点
调试2440 RAM拷贝至SDRAM遇到的问题 汇编代码主要是初始化一些寄存器,关狗,初始化时钟,初始化存储管理器以便访问内存,然后将SoC上4k RAM数据拷贝至SDRAM,然后在SRAM里面运行, ...
- Python执行Linux系统命令的4种方法
http://www.jb51.net/article/56490.htm (1) os.system 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 复制代码代码如下: system( ...
- JDBC数据源的驱动问题
classes12.jar,ojdbc14.jar,ojdbc5.jar和ojdbc6.jar的区别,之间的差异 在使用Oracle JDBC驱动时,有些问题你是不是通过替换不同版本的Oracle ...
- Delphi GetCurrentDir 获取当前文件夹
//获取当前文件夹 GetCurrentDirvardir: string;begindir := GetCurrentDir;ShowMessage(dir); //C:\Documents and ...
- Eclipse的优化方案
General > Startup and Shutdown : 移除所有在启动时加载的插件. General > Editors > Text Editors > Spell ...
- 在C/C++函数中使用可变参数
原文链接地址:http://blog.csdn.net/djinglan/article/details/8425768 下面介绍在C/C++里面使用的可变参数函数. 先说明可变参数是什么,先回顾一下 ...
- 菜单 & 工具栏 & 状态栏
MFC中ON_UPDATE_COMMAND_UI和ON_COMMAND消息区别 CCmdUI 加载状态栏 加载工具栏
- 【BZOJ 1124】[POI2008] 枪战Maf Tarjan+树dp
#define int long long using namespace std; signed main(){ 这个题一看就是图论题,然后我们观察他的性质,因为一个图论题如果没有什么性质,就是真· ...
- java AWT repaint paint update 方法
paint(Graphic g): awt调用这个方法有2种形式.程序驱动方式和系统驱动方式. 在系统驱动的情况下(比如界面第一次显示组件),系统会判断组件的显示区域,然后向事件分发线程发出调用pai ...