Leetcode008. String to Integer (atoi)
/* improvement on dealing with overflow accroding to this:
* https://discuss.leetcode.com/topic/57452/c-solution-beats-100
*
* be careful about the INT_MAX and INT_MIN
* this atoi is not thinking about special char like dot and so on.
*/ class Solution {
public:
int myAtoi(string str) {
int i,nega_sign=;
long long int re=; //long long re to avoid overflow
for(i=;i<str.size();i++) //skip to all the blank in the front of the string
{
if(str[i]!=' ')break;
}
if(str[i]=='+')i++; // deal with signature
else if(str[i]=='-'){ nega_sign=-;i++;}
while(i<str.size()&&isdigit(str[i]))
/*when the string is over or the current char is not a valid integral number,no more conversion will be performed.
*/
{
re=re*+str[i]-'';
if(nega_sign==-&& -*re<=INT_MIN)return INT_MIN; //overflow
else if (nega_sign==&&re>=INT_MAX)return INT_MAX;
i++;
}
return re*nega_sign;
}
};
Leetcode008. 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 ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- 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 ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
随机推荐
- 1.html5 学习要求,Html 5发展历程
以下是我在学习html5,项目中用到的关于html5的总结和心得. 1.学习要求 Html4.01,xhtml Css2 Javascript 耐心,动手,毅力. 2.Html 发展历程 Html1. ...
- 在编译oc中protocol时出现的错误
Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang ...
- IREP_SOA Integration SOAP概述(概念)
20150827 Created By BaoXinjian
- Codeforces Round #358 (Div. 2)B. Alyona and Mex
B. Alyona and Mex time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Python 结巴分词(1)分词
利用结巴分词来进行词频的统计,并输出到文件中. 结巴分词github地址:结巴分词 结巴分词的特点: 支持三种分词模式: 精确模式,试图将句子最精确地切开,适合文本分析: 全模式,把句子中所有的可以成 ...
- 在tortoiseSVN上将trunk的代码merge到branch上去
1.进入branch项目的目录 2.右键选择merge 3.下一步 4.选择trunk
- laravel5.2 移植到新服务器上除了“/”路由 ,其它路由对应的页面显示报404错误(Object not found!)———新装的LAMP没有加载Rewrite模块
Laravel 框架通过 public/.htaccess 文件来让网址不需要 index.php.如果你的服务器是使用 Apache,请确认是否有开启 mod_rewrite 模块.如果 Larav ...
- 区分DPI、分辨率(PPI)、图像的物理大小、像素宽度
分辨率都知道,越高越清晰. 一.描述分辨率的单位有: dpi(点每英寸).lpi(线每英寸)和ppi(像素每英寸).但只有lpi是描述光学分辨率的尺度的.虽然dpi和ppi也属于分辨率范畴内的单 ...
- 20145305《JAVA程序设计》课程总结
每周读书笔记链接汇总 问卷调查 第0周学习心得 第1周学习总结 第2周学习总结 第3周学习总结 第4周学习总结 第5周学习总结 第6周学习总结 第7周学习总结 第8周学习总结 第9周学习总结 第10周 ...
- 20145305 《Java程序设计》实验四
Android开发基础 安装过程没有截图 给出安装参考链接 http://ask.android-studio.org/?/article/9 实验结果截图: 完成HelloWorld项目,显示自己的 ...