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 ...
随机推荐
- 微信小程序 scroll-view 完成上拉加载更多
我们经常在软件客户端上看到这么一个功能,当我们阅读信息浏览到文章的末尾时,通常会加载出更多的信息.比如,我们在简书客户端上浏览推荐文章时,浏览到屏幕的末尾,此时又加载出了另一页的推荐文章,即实现了上拉 ...
- Kafka 消费迟滞监控工具 Burrow
Kafka 官方对于自身的 LAG 监控并没有太好的方法,虽然Kafka broker 自带有 kafka-topic.sh, kafka-consumer-groups.sh, kafka-cons ...
- matlab 图像保存时去除白边
很是讨厌MATLAB输出图像时自带的白边,尤其是当导出.eps格式时,很难通过编辑图片来去掉白边.网上有很多代码但是没有注释,有很多坑要填.这里提供一个去除白边的代码,自己在别人的基础上修改了而且加了 ...
- grafan源码编译
下载grafana源码:https://github.com/grafana/grafana 前端: 安装node.js,安装完自带npmnpm install -g yarnyarn install ...
- 2021 祥云杯 wp
52 web ezyii https://pan.baidu.com/s/1j7IJm9xiea5FvBhPMkPNoQ 提取码GAME <?php include("closure/ ...
- SpringBoot 整合 Mybatis-Plus + Mysql
mybatis-plus是mybatis的一款插件,它的主要作用是快速开发,省略mybatis的配置,具体的功能请参照官网. 开发环境: springboot,maven,mybatis-plus,m ...
- RocketMQ架构原理解析(二):消息存储
一.概述 由前文可知,RocketMQ有几个非常重要的概念: broker 服务端,负责存储.收发消息 producer 客户端1,负责产生消息 consumer 客服端2,负责消费消息 既然是消息队 ...
- .NET Protobuf包装器库
Wodsoft Protobuf Wrapper 内容 关于 需求 安装 用法 序列化 反序列化 字段定义 字段排序 非空构造函数对象 获取Protobuf包装器 高级 支持的属性类型与Protobu ...
- Python 爬取 房天下
... import requests from requests import ConnectionError from bs4 import BeautifulSoup import pymong ...
- 菜鸡的Java笔记 第二十七 - java 链表基本概念
链表基本概念 1.链表的基本形式 2.单向链表的完整实现 认识链表 链表= 可变长的对象数组,属于动态对象数组的范畴 链表 ...