Leetcode8--->String to Integer(实现字符串到整数的转换)
题目: 实现字符串到整数的转换
解题思路:
下面给出这道题应该注意的一些细节:
1. 字符串“ 123 ” = 123;
2. 字符串“+123” = 123;
3. 字符串“-123” = -123;
4. 字符串“-” = 0;“+” = 0;
5. 字符串“123-” = 123;
6. 字符串“21474836478” = 2147483647(Integer.MAX_VALUE)
7. 其余情况都是非法输入,一律返回0;
代码如下:
public class Solution {
public int myAtoi(String str) {
if(str == null || str.length() < 1)
return 0;
boolean negative = false;
int i = 0;
double res = 0.0;
String s = str.trim();
int length = s.length();
if(s.charAt(i) < '0'){
if(s.charAt(i) == '-'){
negative = true;
}
else if(s.charAt(i) != '+'){
return 0;
}
if(length == 1)
return 0;
i++;
}
while(i < length){
int n = (s.charAt(i) - '0') ;
if(n >= 0 && n <= 9){
res *= 10;
res += n;
i ++;
}
else
break;
}
if(negative){
res *= -1;
}
res = res >= Integer.MAX_VALUE ? Integer.MAX_VALUE : res;
res = res <= Integer.MIN_VALUE ? Integer.MIN_VALUE : res;
return (int)res;
}
}
Leetcode8--->String to Integer(实现字符串到整数的转换)的更多相关文章
- Leetcode8.String to Integer (atoi)字符串转整数(atoi)
实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...
- [LeetCode] 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 which converts a string to an integer. The function first discards as many whitespace ...
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 8. String to Integer[M]字符串转整数
题目 Inplement atoi which converts a string to an integer. The function first discards as many whitesp ...
- 008 String to Integer (atoi) 字符串转换为整数
详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...
- 编程练习------C/C++分别实现字符串与整数的转换
C/C++分别实现字符串与整数的转换 前提:不使用 itoa 和 atoi. 方法一.C和C++通用的一种转换手段是: 1.整数转化为字符串:采用加'0',再逆序的办法,整数加'0'就会隐性转化成ch ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
随机推荐
- 变更gcc版本
当前的GCC版本为GCC-4.2,需要切换到GCC-3.4.首先,你需要去你的usr/bin/下去看看有没有gcc-3.4这样文件,如果没有的话,就安装一下吧: apt-get install gcc ...
- 排序算法C语言实现
大学有一门课程叫做数据结构,严蔚敏的课本,其中详细介绍了集中经典的排序算法,学习复习反复几次,但是直到现在仍然只记得名字了,所以想记录下来,随时复习直至牢记于心.经常面试的朋友知道,排序算法在面试中出 ...
- JavaScript命名——name不能做变量名
使用name作为变量名(var name = ‘’),在IE中未引起bug,在Chrome中引起bug但未明确指出命名错误,而是会报其他错误,故不便于发现. 现象原因: javascript中name ...
- 学习Unity 4.6新GUI系统
(搬运自我在SegmentFault的博客) 最近在学习Unity的过程中,自己做一款小游戏自娱自乐.自然需要用到GUI.但4.5中的GUI很难用,一个选择是传说中的NGUI插件.但对于4.6中的新G ...
- 【UML】用例图Use Case diagram(转)
http://blog.csdn.net/sds15732622190/article/details/48858219 前言 总结完UML概述,就该说道UML中的九种图了,这九种图中,最先要说的,就 ...
- 使用EventLog组件保存Windows系统日志
实现效果: 知识运用: EventLog类的CreateEventSource方法 //用于建立一个应用程序 使用指定的Sourc作为向本机上的日志中写入日志项的有效事件源 CreateEventS ...
- SC || Chapter 8
栈:方法调用和局部变量的存储位置,保存基本类型 堆:在一块内存里分为多个小块,每块包含 一个对象,或者未被占用
- Python——字典dict()详解
一.字典 字典是Python提供的一种数据类型,用于存放有映射关系的数据,字典相当于两组数据,其中一组是key,是关键数据(程序对字典的操作都是基于key),另一组数据是value,可以通过key来进 ...
- jfinal的配置文件详解
1.去官网下载最新的jar包(我这是JFinal-lib-2.2) tomcat+mysql 所需要的jar 2.配置web.xml <filter> <filter-name> ...
- IDEA项目显示树形结构