LeetCode 8. 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.
Update (2015-02-10):
The signature of the C++
function had been updated. If you still see your function signature accepts a const char *
argument, please click the reload button to reset your code definition.
题目标签:String
题目给了我们一个 str,让我们把它 转换为 int。
其中有很多违规的条件没有说明:
正负的符号只能有0个 或者 1个;
符号后面就应该是数字了,如果遇到不是数字的符号,返回目前为止合格的数字,不需要考虑后面的数字;
如果数字overflow,大于MAX的要返回MAX,小于MIN 的要返回MIN;
etc。
Java Solution:
Runtime beats 57.67%
完成日期:01/09/2017
关键词:String
关键点:考虑到所有违规情况
class Solution
{
public int myAtoi(String str)
{
long res = 0; // the res number to return. Note: res to return should be long and cast it to int when return it at the end.
int sign = 1; // the sign before the number. default is 1 (positive).
int index = 0; // index for num string to go through. // Step 0: if parameter str is null or "", then return 0.
if(str.length() == 0 || str == null)
return 0; // Step 1: trim the whitespace.
str = str.trim(); // Step 2: check first char is '+' or '-', move the index by 1 and also sign value.
if(str.charAt(0) == '+')
index++;
else if(str.charAt(0) == '-')
{
index++;
sign = -1; // change the sign to -1 (negative).
} // Step 3: go through the str string.
for(; index<str.length(); index++)
{
// if this char is not a number char, then break. No matter there are more numbers after.
if(str.charAt(index) > '9' || str.charAt(index) < '0')
break; // add this char value into res.
res = res * 10 + (str.charAt(index) - '0'); // char - '0' is the correct int value. // check the num exceed the max or not.
if(res > Integer.MAX_VALUE) // res should be long because here res might be over Integer.max value.
break;
} // Step 4: depending on the sign and max or min value, return res.
if(res * sign >= Integer.MAX_VALUE)
return Integer.MAX_VALUE;
else if(res * sign <= Integer.MIN_VALUE)
return Integer.MIN_VALUE; // goes here meaning the res number doesn't exceed max and min integer value.
return (int)res * sign; // here need to cast res to int.
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 8. String to Integer (atoi) (字符串到整数)的更多相关文章
- [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】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 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) 字符串转换整数
作者: 负雪明烛 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 8 String to Integer (atoi) 字符串处理
题意:将字符串转化成数字. 前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的. class Solution { pu ...
- LeetCode OJ String to Integer (atoi) 字符串转数字
#include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ...
- 008 String to Integer (atoi) 字符串转换为整数
详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...
随机推荐
- git Eclipse项目不显示当前分支
问题: 在Eclipse中,导入新的git项目,在项目上不再显示当前所处的分支,也不再显示修改了哪些文件 解决: 右键选中项目 --> Team --> Share Project ...
- text-shadow的用法详解
1.兼容性:text-shadow 和 box-shadow 这两个属性在主流现代浏览器上得到了很好的支持( > Chrome 4.0, > Firefox 3.5, > Safar ...
- 使ThinkPHP(3.2.3)的分页类支持Bootstrap风格
ThinkPHP 3.2.3自带的分页类位于:/ThinkPHP/Library/Think/Pages.class.php ,官方文档在这里:ThinkPHP3.2.3数据分页 Pages.clas ...
- node遍历给定目录下特定文件,内容合并到一个文件
遍历目录用了fs.readdir这个异步方法,得到当前目录下所有的文件和目录的一个数组.然后判断: if文件,并且后缀符合设定的规则(本文例子是符合后缀ts,js)直接用同步方法写入, if目录,继续 ...
- 17Oracle Database 维护
Oracle Database 维护 备份 还原
- oracle查询没有主键的表
select table_name from user_tables a where not exists (select * from user_constraints b where b.cons ...
- 【原】CentOS release 6.2 安装mysql
1. yum update升级以后的系统版本为 [root@yl-web yl]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (C ...
- canvas练手项目(三)——Canvas中的Text文本
Canvas中的Text文本也是一个知识点~,我们需要掌握一下几个基本的Text操作方法 首先是重要参数textAlign和textBaseline: textAlign left center ri ...
- UVA-1368 DNA Consensus String(思路)
题目: 链接 题意: 题目虽然比较长,但读完之后题目的思路还是比较容易想出来的. 给出m个长度为n的字符串(只包含‘A’.‘T’.‘G’.‘C’),我们的任务是得出一个字符串,要求这个字符串与给出的m ...
- C++关键字:explicit
#include "pch.h" #include <iostream> using namespace std; class BaseClass { public: ...