008 String to Integer (atoi) 字符串转换为整数
详见:https://leetcode.com/problems/string-to-integer-atoi/description/
实现语言:Java
class Solution {
public int myAtoi(String str) {
int index =0;
Long total = new Long(0);
int sign = 1;
while(index < str.length() && str.charAt(index) == ' '){
++index;
}
if(index == str.length()){
return (int)(total*sign);
}
if(str.charAt(index) == '-' || str.charAt(index) == '+'){
sign = str.charAt(index) == '-'?-1 : 1;
index++;
}
while(index < str.length() && str.charAt(index) >= '0' && str.charAt(index) <= '9'){
total = total *10 + str.charAt(index) - '0';
++index;
if(total > Integer.MAX_VALUE){
return sign==1?Integer.MAX_VALUE:Integer.MIN_VALUE;
}
}
return (int)(total*sign);
}
}
实现语言:C++
class Solution {
public:
int myAtoi(string str) {
int n=str.size();
if(n==0||str.empty())
{
return 0;
}
int sign=1,base=0,i=0;
while(i<n&&str[i]==' ')
{
++i;
}
if(str[i]=='+'||str[i]=='-')
{
sign=str[i++]=='+'?1:-1;
}
while(i<n&&str[i]>='0'&&str[i]<='9')
{
if(base>INT_MAX/10||(base==INT_MAX/10&&str[i]-'0'>7))
return sign==1?INT_MAX:INT_MIN;
base=base*10+str[i++]-'0';
}
return sign*base;
}
};
参考:http://www.cnblogs.com/grandyang/p/4125537.html
008 String to Integer (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) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- Leetcode8.String to Integer (atoi)字符串转整数(atoi)
实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
随机推荐
- SQL中replace函数
string sql1 = "select price from dbo.eazy_farm where REPLACE(title,' ','')='" + cainame + ...
- linux下 vi 命令大全
引用:http://www.cnblogs.com/88999660/articles/1581524.html 进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi ...
- 低调的css3属性font-size-adjust
在我们日常的项目中经常会用到不同的字体来达到我们想要的效果,可是某些情况下不同字体的大小在相同的px下显示的大小是不同的 <div id="div1">Text 1&l ...
- K Sum(2 Sum,3 Sum,4 Sum,3-Sum Closest)
算是经典算法问题了.这里主要针对只存在一个解或者只需要求一个解的情况描述一下解题思路.若需要找到所有可能解,方法需要略作调整.如有问题,欢迎指正. 2 sum: 如果已排序,可直接用夹逼法,即两指针从 ...
- Spring boot 学习二:入门
1: 需要的环境: JDK:至少JDK7才支持Spring boot maven:至少3.2 spring-boot:1.2.5.RELEASE(在pom.xml中指定) 2: 创建一个maven工程 ...
- 相关符号标点的英文(IOS学习)
尖括号: angle bracket 方括号: square bracket 花括号: curly brace 圆括号: parentheses 逗号: comma 冒号: colon 逗号: sem ...
- Java Learning 000 搭建开发环境
Java Learning 000 搭建开发环境 你需要两个软件: * JDK (Java Develop Kit :Java开发工具包) * eclipse (eclipse 集成开发环境软件) 安 ...
- 20、BLAST比对及结果介绍
1.formatdb -i /share/nas1/huangt/project/IsoSeq/BMK170104-E545-03-a/Analysis_T01/MoveRebundant/T01/c ...
- 7.XXEinjector:一款功能强大的自动化XXE注射工具
今天给大家介绍的是一款名叫XXEinjector的漏洞利用工具,XXEinjector是一款基于Ruby的XXE注入工具, 它可以使用多种直接或间接带外方法来检索文件.其中,目录枚举功能只对Java应 ...
- 蚂蚁金服HR电话面
1.先自我介绍一下. 2.问到项目问题.(项目介绍,用到哪些技术,如何测试的.) 3.一些基础知识. a) list.map.set是继承了collection的吗? List:1.可以允许重复的对 ...