leetcode第八题--String to Integer (atoi)
Problem:
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.
spoilers alert... click to show requirements for atoi.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
哈哈,这题也没有参考别人的代码,我是看完了以上题目,修改了七次终于通过了。
我根据requirements for atoi写,
先写了个子函数,用来返回正确的数字字符串
在这当中首先是读过前面的空格,如果第一个非空格字符不是 + - 或者数字,那么返回空字符串。如果是+号或者-号,那么后边必须紧跟数字,否则也返回空字符。如果以上都没有返回,那么就是遇到数字了,直接读到非数字部分,然后将正负号与数字一同返回字符串。
再在atoi中判断返回的字符串
我定义的是long long 型来存,通过string转换为数字long long 型(通过包含sstream头文件,利用stringstream转的),然后判断long long 型在正负情况下和INT_MAX 或 INT_MIN 的比值,特别要注意是符号的时候,如果边界不对那么很多case通过不了,我一开始用long型发现不行,才转用long long 型的。
还有就是一开始我不知道原来一定要规定开头才算。如果数字在中间,前后其他字符就不算了。例如“abc123”虽然有123,但是因为开头的不是符合要求所以就不能返回数字,直接返回0即可。
class Solution {
public:
int atoi(const char *str)
{
string s = substringInteger(str);
if (s.size() == )
return ;
int positive = ;
if (s[] == '+')
s = s.substr(,s.size() - );
else if(s[] == '-')
{
s = s.substr(,s.size() - );
positive = ;
}
std::stringstream ss;
ss<<s;
long long result;
ss>>result;
if (positive == )
{
long long maxval = ; // 这里直接用数字是因为我用 INT_MAX + 1 的时候显示的是 INT_MIN的值, -1的case会出错
if (result < maxval)
return -*result;
else
return INT_MIN;
}
else
{
if (result < INT_MAX)
return result;
else
return INT_MAX;
}
}
private:
//leetcode7 return the sub string include interger
string substringInteger(string s)
{
if (s.size() == )
return "";
string substring;
int pos = -, len = ;
int index = ;
int positive = ;
while(s[index] == ' ' && index < s.size())
{
index++;
}
if (index == s.size())
return "";
if(s[index]!='+' && s[index]!='-' && !isdigit(s[index]))
return "";
if(s[index] == '+'&&index+<s.size())
if(!isdigit(s[index+]))
return "";
if(s[index] == '-'&&index+<s.size())
{
if(!isdigit(s[index+]))
return "";
else
positive = ;
}
// detect till digit if exist
while(!isdigit(s[index])&&index<s.size())
{
index++;
}
if(index == s.size())
return "";
// find the length of the digit
pos = index;
while(isdigit(s[index])&&index<s.size())
{
index++;
}
len = index - pos;
substring = s.substr(pos,len);
// if negative return '-' in front
if (positive == )
substring = "-" + substring;
return substring;
}
};
这次coding中回顾并学习了 substr,sstream 中 stringstream 将string和任意型转换 INT_MAX 和 INT_MIN 特别边界处理时注意,因为有个尾数是8,还有就是代码中用中文注释的地方也要注意。
leetcode第八题--String to Integer (atoi)的更多相关文章
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- [leetcode]经典算法题- String to Integer (atoi)
题目描述: 把字符串转化为整数值 原文描述: Implement atoi to convert a string to an integer. Hint: Carefully consider al ...
- LeetCode【8】. String to Integer (atoi) --java实现
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- LeetCode(8)String to Integer (atoi)
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【leetcode❤python】 8. String to Integer (atoi)
#-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- 乘风破浪:LeetCode真题_008_String to Integer (atoi)
乘风破浪:LeetCode真题_008_String to Integer (atoi) 一.前言 将整型转换成字符串,或者将字符串转换成整型,是经常出现的,也是必要的,因此我们需要熟练的掌握,当然也 ...
随机推荐
- Vertica对于所计算的时间SQL声明大全
词:强.大.所有,强烈推荐 SQL语句 查询结果 select (timestamp '2005-01-17 10:00' - timestamp '2005-01-01'); 16 10:10 se ...
- POJ 2431 Expedition (贪心+优先队列)
题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...
- latex如何输入正确的 双引号
latex当输入双引号,假设直接用双引号键在键盘上.玩过顺-handed. 引述左输入法是正确的:按两次"Tab在之上,数字1左边的键".至于后面行情,该方法是一样的老,这是两次单 ...
- 十依据一个有用的算法来找到最小(最大)的k的数量-线性搜索算法
例如:进入1.2.3,4,5,6.7.8此8数字,最小的4图的1,2,3,4. 思路1:最easy想到的方法:先对这个序列从小到大排序.然后输出前面的最小的k个数就可以.假设选择高速排序法来进行排序, ...
- Java多线程总结之由synchronized说开去(转)
这几天不断添加新内容,给个大概的提纲吧,方面朋友们阅读,各部分是用分割线隔开了的: synchronized与wait()/notify() JMM与synchronized ThreadLocal与 ...
- C++ Primer 学习笔记_43_STL实践与分析(17)--再谈迭代器【中】
STL实践与分析 --再谈迭代器[中] 二.iostream迭代[续] 3.ostream_iterator对象和ostream_iterator对象的使用 能够使用ostream_iterator对 ...
- 如何生成可变表头的excel(转)
1.实现功能: 传入一个表头和数据,将数据导入到excel中. 为了便于项目的扩展,数据传入通过泛型集合传入,获取数据时,通过反射的方式获取,这样无论你的表头是多少项,我都能很方便的生成.另外为了便于 ...
- UIMenuController,UIPasteboard:复制,粘贴详细解释
首先,这四个地图.我想实现的功能.[一张照片讲一个复制到另一UIimageView上] 实现代码例如以下: -(IBAction)panGestureTop:(UILongPressGestureRe ...
- Ajax 实现无刷新页面
注意:如本文所用,在前面的文章库的数目可以在源代码中找到,我将指示在文本,其中链路,为了缩短制品的长度,阅读由此带来的不便.乞求被原谅. 评论文章 Ajax 实现无刷新页面.其原理.代码库.代码. 这 ...
- NSOJ 飞船汇合(经典)
一支分散的飞船舰队,需要汇合到主舰,但是这种飞船在太空中飞行的耗油与质量没有关系,只与发动机打开的时间有关系,为了节省油量,指挥官通知,汇合途中,多台飞船可以串成串飞行,这样只需启动一台发动机,由于安 ...