11-String to Integer (atoi)
字符串转整型,更新之后的leetcode题,需考虑各种情况,
测试过标准库的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.
#include<iostream>
#include<limits>
using namespace std;
#define IMAX numeric_limits<int>::max()
#define IMIN numeric_limits<int>::min()
/*********************************
test cases:
"1"=>"1"
"+"=>"0" "-"=>"0"
" 0101"=>"101"
" -1010023630o4"=>"-1010023630"
"+12"=>"12"
" fsad101 "=>"0"
"2147483648"=>"2147483647"
"-2147483648"=>"-2147483648"
**********************************/
int myAtoi(string str)
{
if(str=="")return 0;
if(str.size()==1)
{
if(str[0]>'9'||str[0]<'0')return 0;
}
int beg=0;
while(str[beg]==' ')beg++;//越过前面的空字符
if((str[beg]>'9'||str[beg]<'0')&&str[beg]!='-'&&str[beg]!='+')return 0;//第一个非空字符非法
int sign=(str[beg]=='-')?-1:1; //判断符号
int j=(str[beg]=='+'||str[beg]=='-')?beg+1:beg;//判定何时开始计算
int res=0;
int count=0;
for(int i=j;i<str.size();++i)
{
if(str[i]>'9'||str[i]<'0')break;//遇到非数字即不看后面的内容
if(count>9)return (sign==1)?IMAX:IMIN;
res = res*10;
if(count==8){
if(sign==1&&res>IMAX/10) return IMAX;
if(sign==-1&&res*sign<IMIN/10) return IMIN;
}
if(count==9){
//cout<<str[i]-'0'<<endl<<res<<" "<<IMIN;
if(sign==1&&(IMAX-res<=str[i]-'0'))return IMAX;
if(sign==-1&&(res*sign-IMIN<=str[i]-'0')) return IMIN;
}
res +=str[i]-'0';
count++;
}
return res*sign;
}
int main()
{
cout<<myAtoi(" -1010023630o4");
return 0;
}
11-String to Integer (atoi)的更多相关文章
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- LeetCode--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)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 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-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 ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- RocketMQ源码详解 | Producer篇 · 其二:消息组成、发送链路
概述 在上一节 RocketMQ源码详解 | Producer篇 · 其一:Start,然后 Send 一条消息 中,我们了解了 Producer 在发送消息的流程.这次我们再来具体下看消息的构成与其 ...
- PCB板HDI板几阶是什么意思
http://blog.sina.com.cn/s/blog_55ff6d5d0102xxvx.html
- STM32中按键中断分析
在按键学习中,我们有用到查询的方法来判断按键事件是否发生,这种查询按键事件适用于程序工作量较少的情况下,一旦程序中工作量较大较多,则势必影响程序运行的效率,为了简化程序中控制的功能模块的执行时间,引入 ...
- 在c中使用正则表达式
今天学习编译原理的时候,用c写一个简易的文法识别器实验遇到了一个问题:要用正则表达式去识别正则文法里面的A->ω,A->Bω, 其中ω属于T的正闭包,也就是说我们对正则文法的产生式进行抽象 ...
- hdu 5101 Select (二分+单调)
题意: 多多有一个智商值K. 有n个班级,第i个班级有mi个人.智商分别是v1,v2,.....vm. 多多要从这些人中选出两人.要求两人智商和大于K,并且两人不同班.问总共有多少种方案. 数据范围: ...
- 学会python永不加班系列之操作excel
python作为一种解释性语言,简单高效的模式逐渐火爆.同时存在多种扩展性. 永不加班系列 python正确操作excel 实验环境: 系统:win10 语言:python3.8 承载软件:pycha ...
- NOIP模拟88(多校21)
前言 对于这套题的总体感觉就是难,然后就是自己很菜... 对于 T1 考试时只会一个最垃圾的背包,考完之后对于思路这一块也不是很顺利,大概这就是薄弱的地方吧. 然后 T2 是比较简单的一道题了,但是考 ...
- maven+springmvc+cxf 实现简单webservice小例子
1.首先你需要创建一个maven项目[当然是web项目] 2.pom.xml添加以下 <properties> <cxf.version>2.2.3</cxf.versi ...
- 2016西邮Linux兴趣小组大事记
2016年还有半个小时就结束了,前面把自己9月做的规划拿出来完善了下,觉得真的是不容易的一年,所有的事情只有自己经历过才会有不一样的感受,世上无难事,只怕有心人. 这是我九月份制定的计划: 下面是20 ...
- building sasl.wrapper extention
yum install gcc-c++ python-devel.x86_64 cyrus-sasl-devel.x86_64 pip install pyhs2 ref: https://www.o ...