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 ...
随机推荐
- hystrix的dashboard和turbine监控
当我们的应用程序使用了hystrix后,每个具体的hystrixCommand命令执行后都会产生一堆的监控数据,比如:成功数,失败数,超时数以及与之关联的线程池信息等.既然有了这些监控数据数据,那么我 ...
- RAW RGB格式
RAW RGB格式 10bit Raw RGB, 就是说用10bit去表示一个R, G, 或者B, 通常的都是用8bit的. 所以你后面处理时要把它转换为8bit的, 比较简单的方法就是将低两位去掉, ...
- 计算机网络传输层之TCP协议(tcp协议特点、tcp报文段首部格式、tcp连接建立---三次握手、tcp连接释放---四次握手)
文章转自:https://blog.csdn.net/weixin_43914604/article/details/105516090 学习课程:<2019王道考研计算机网络> 学习目的 ...
- Linux 文本三剑客之 sed
Linux 系统中一切皆文件. 文件是个文本.可以读.可以写,如果是二进制文件,还能执行. 在使用Linux的时候,大都是要和各式各样文件打交道.熟悉文本的读取.编辑.筛选就是linux系统管理员的必 ...
- Spark记录(一):Spark全景概述
一.Spark是什么 Spark是一个开源的大数据处理引擎. 二.Spark的主要组件如下图所示: 三.Spark运行时架构 Spark共有三种运行模式:本地模式.集群模式.客户端模式. 生产环境基 ...
- 使用Netty和动态代理实现一个简单的RPC
RPC(remote procedure call)远程过程调用 RPC是为了在分布式应用中,两台主机的Java进程进行通信,当A主机调用B主机的方法时,过程简洁,就像是调用自己进程里的方法一样.RP ...
- Excel 读写
一.环境准备:pom.xml 导入依赖 poi-ooxml <dependencies> <dependency> <groupId>org.apache.poi& ...
- sqlalchemy create single table
User.__table__.drop(engine) User.__table__.create(engine) https://stackoverflow.com/a/45287771/80250 ...
- 博主日常工作中使用的shell脚本分享
前言: 今天给大家分享一篇在我工作中常用的一个shell脚本,里面有一些我们常用到的shell操作.该脚本用于本地电脑和服务器交互上,实现以下功能: 自动拉取自己个人电脑上的源码到服务器上yocto包 ...
- SpringCloud微服务实战——搭建企业级开发框架(十六):集成Sentinel高可用流量管理框架【自定义返回消息】
Sentinel限流之后,默认的响应消息为Blocked by Sentinel (flow limiting),对于系统整体功能提示来说并不统一,参考我们前面设置的统一响应及异常处理方式,返回相同的 ...