【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. ...
随机推荐
- 【iOS开发】UIViewController的基本概念与生命周期
http://www.cnblogs.com/wayne23/p/3868535.html UIViewController是iOS顶层视图的载体及控制器,用户与程序界面的交互都是由UIViewCon ...
- java的命名空间
这个package me.gacl.websocket相当于.net中的namespace命名空间. import 相当于.net中的using,引用命名空间:
- Python + OpenCV 实现LBP特征提取
背景 看了些许的纹理特征提取的paper,想自己实现其中部分算法,看看特征提取之后的效果是怎样 运行环境 Mac OS Python3.0 Anaconda3(集成了很多包,浏览器界面编程,清爽) 步 ...
- JSON使用(4)
把JSON文本转换为JavaScript对象 JSON最常见的用法之一,是从web服务器上读取JSON数据(作为文件或作为HttpRequest),将JSON数据转换为JavaScript对象,然后在 ...
- [CF1041E]Tree Reconstruction
题目大意:有一棵树,现在给你每条树边被去掉时,形成的两个联通块中点的最大的编号分别是多少,问满足条件的树存不存在,存在输出方案 题解:一条边的两个编号中较大的一个一定是$n$,否则无解. 开始构造这棵 ...
- CentOS 7, Attempting to create directory /root/perl5
By francis_hao Apr 10,2017 在使用CentOS 7的时候,首次登陆会出现新建一个perl5文件夹的提示,删除该文件后,之后登陆还是会出现该提示并新建了perl5文件夹. ...
- mysql5.7.22以上版本忘记密码时这样修改
1.关闭mysql服务 net stop mysql 2.找到mysql安装路径找到 my.ini 打开在 [mysqld] 下添加 skip-grant-tables 跳过密码校验 3.登陆mysq ...
- HDU 4417 划分树写法
Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...
- 类名.class 类名.this 详解
我们知道在java中,一个类在被加载的时候虚拟机就会自动的生成一个这个类的一个Class类型的“类对象”,每个类都对应着一个这样的类对象,通过这个Class类型的类对象,我们就能够使用“内省与反射”机 ...
- Java 处理 XML 的三种主流技术及介绍
Java 处理 XML 的三种主流技术及介绍 原文地址:https://www.ibm.com/developerworks/cn/xml/dm-1208gub/ XML (eXtensible Ma ...