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 ...
随机推荐
- OAF_EO系列1 - Definition定义(概念)
2014-06-14 Created By BaoXinjian
- python(12)给文件读写上锁
目的:当我们用脚本去爬取数据或者向文件中写数据的时候,有时候需要两个或者多个脚本同时向一个文件中读写 于是乎就会出现写乱的情况,于是乎我们就需要把正在写的文件先锁起来,只让当前的写,写完后再释放 代码 ...
- 手动编译Jsp文件
手动模拟Tomcat编译jsp文件 Tomcat编译jsp文件的配置路径是在%tomcat_home%/conf/web.xml中,有这样一段代码 <servlet> <servle ...
- memcpy
函数原型 void *memcpy(void*dest, const void *src, size_t n); 功能 由src指向地址为起始地址的连续n个字节的数据复制到以destin指向地址为起始 ...
- java网络编程之TCP实例
Dgram类 package Socket; import java.net.DatagramPacket; import java.net.InetAddress; public class Dgr ...
- iOS 审核加急通道使用--转载来源--有梦想的蜗牛
提交完成后进入加急审核页面. 链接:https://developer.apple.com/appstore/contact/appreviewteam/index.html 在i would lik ...
- mongodb 查询使用
> db.jd_58tc_raw.findOne() { "_id" : "2659e4e4caf0504ec4362478e2ed57ca", &quo ...
- 认识与学习BASH(中)
1.在设置变量中:单引号与双引号的最大不同:双引号能保有变量的内容,单引号仅能是一般字符 2.反单引号(`)作用:在一串指令中,在‘之内的指令将会被先执行,其结果将作为外部的输入信息. locate指 ...
- MSSQL中的随机函数
随机函数:rand()在查询分析器中执行:select rand(),可以看到结果会是类似于这样的随机小数:0.36361513486289558,像这样的小数在实际应用中用得不多,一般要取随机数都会 ...
- sencha touch 开发准备
这是本人第一次写博客教程,没什么经验,文笔也不是很好,写这教程一方面为了巩固自己这段时间的学习成果,一方面帮助大家解决问题,欢迎大家多提建议,指出问题.接下来我们就开始我们的sencha touch开 ...