字符串转整型,更新之后的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)的更多相关文章

  1. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  2. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  3. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  4. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  5. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

  6. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  7. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  8. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

  9. LeetCode: String to Integer (atoi) 解题报告

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  10. 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

随机推荐

  1. Python课程笔记 (五)

    今天主要学习图形用户界面,更多的还是要我们自己去实际操作,课仿佛上了一半就完了,分享一下课程(这里在SixthClass)的源码: https://gitee.com/wang_ming_er/pyt ...

  2. 『学了就忘』Linux基础 — 8、虚拟机网络模式说明

    目录 1.虚拟机网卡 2.网络连接模式对应工作的网卡 3.桥接模式说明 4.补充说明 这篇主要总结一下虚拟机网络配置中桥接模式.NAT模式和仅主机模式的区别. 打开VMware,选中虚拟机,点击网络适 ...

  3. 树的子结构 牛客网 剑指Offer

    树的子结构 牛客网 剑指Offer 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) # class TreeNode: # def __init_ ...

  4. C++常见STL介绍

    栈 :FILO 栈(stack)又名堆栈,它是一种线性表,是一个后进先出的数据结构. 使用时须加上头文件:#include<stack> 允许进行插入和删除操作的一端称为栈顶(top),另 ...

  5. (二)lamp环境搭建之编译安装mysql

    mysql 编译安装1,在网站上下载: wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.15.tar.gz 2,安装cmake ...

  6. ansible安装和批量执行命令

    yum install -y ansible 编辑 /etc/ansible/hosts 文件 # This is the default ansible 'hosts' file.## It sho ...

  7. Mysql基础教程:(七)MySQL基础练习

    MySQL基础练习 一.创建student和score表 CREATE TABLE student (id INT(10) NOT NULL PRIMARY KEY ,name VARCHAR(20) ...

  8. 论文翻译:Fullsubnet: A Full-Band And Sub-Band Fusion Model For Real-Time Single-Channel Speech Enhancement

    论文作者:Xiang Hao, Xiangdong Su, Radu Horaud, and Xiaofei Li 翻译作者:凌逆战 论文地址:Fullsubnet:实时单通道语音增强的全频带和子频带 ...

  9. Part 28 AngularJS default route

    At the moment the problem is that, if you try to navigate to a route that is not configured, you wil ...

  10. [源码解析] PyTorch分布式(5) ------ DistributedDataParallel 总述&如何使用

    [源码解析] PyTorch 分布式(5) ------ DistributedDataParallel 总述&如何使用 目录 [源码解析] PyTorch 分布式(5) ------ Dis ...