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 ...
随机推荐
- 开发高峰时的CPU使用率
- BEvent_客制化BusinessEvent通过Workflow Event接受消息传递(案例)
2014-08-03 Created By BaoXinjian
- Shell_Oracle Erp和其他系统Interface资料传输通过Shell进行控制(案例)
2014-06-26 Created By BaoXinjian
- UVA 11137 Ingenuous Cubrency(dp)
Ingenuous Cubrency 又是dp问题,我又想了2 30分钟,一点思路也没有,最后又是看的题解,哎,为什么我做dp的题这么烂啊! [题目链接]Ingenuous Cubrency [题目类 ...
- Markdown 编辑模板
Hello,我是s1124yy. 名字的由来呢,是因为我QQ前4位是1124,但有的账号不能数字开头,所以就随手打了几个字母,最后就这么叫了.其实我很菜,但是我会努力的~~ 由于看到qsc的博客,所以 ...
- Studio--代理设置(SDK下载代理设置)
为啥Android Studio有代理一说呢.比如我们要下载某个插件,但是这个插件又被tc墙了,所以这个时候需要FQ才能安装.FQ其中的一种方式就是使用VPN,配置如下图: 输入VPN的IP和PORT ...
- Python标准库04 文件管理 (部分os包,shutil包)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在操作系统下,用户可以通过操作系统的命令来管理文件,参考linux文件管理相关命令 ...
- 辩护技巧总结——律师在刑事辩护中应注意的几个问题 z
律师在刑事辩护中的角色就像医院中的医生对病人一样,对嫌疑人至关重要.律师的百分之一的失误,对嫌疑人来讲就是百分之百的不幸.因此我在刑事辩护中更加谨慎认真,并归纳了一些注意点供朋友参考. 一.仔细 ...
- 配置webstorm使用supervisor时出现 /usr/bin/env: node: 没有那个文件或目录 解决方法
配置好supervisor路径后 出现了 启动时出现了 /usr/bin/env: node: 没有那个文件或目录 需要讲.nvm下的node链接到, /usr/bin/目录下 sudo ln -s ...
- [Flex] IFrame系列 —— IFrame嵌入html点击其他组件后页面消失的问题
在flex建的web项目中,打开index.template.html,将param.wmode = "transparent";添加到以下位置 <script type=& ...