字符串转整型,更新之后的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. 梦开始的地方(Noip模拟3) 2021.5.24

    T1 景区路线规划(期望dp/记忆化搜索) 一看题目发现肯定是概率期望题,再仔细想想这三天做的题,就知道是个期望dp. 考试思路(错): 因为聪聪与可可的10分打法根深蒂固,导致在考试时想到了用深搜( ...

  2. 如何将声学的spectrogram(声谱图)重新反变换成时域语音信号

    最近在研究一些信号分析的事情,感兴趣如何将频谱信号反变换成时域信号.fft 与ifft可以顺畅的转变,但是这个是一帧信号,当时间较长的信号再一起是,通过反变换变成一帧一帧的时域信号,如何把他们拼接起来 ...

  3. Redis源码分析(adlist)

    源码版本:redis-4.0.1 源码位置: adlist.h : listNode.list数据结构定义. adlist.c:函数功能实现. 一.adlist简介 Redis中的链表叫adlist( ...

  4. 组件通信之全局事件总线 & 消息订阅发布

    全局事件总线 介绍 一种组件间通信的方式,适用于任意组件间通信. 在使用全局事件总线之前需要一些知识准备 所有组件实例的原型对象的原型对象就是 Vue 的原型对象,即VueComponent.prot ...

  5. 常见yaml写法-job

    apiVersion: batch/v1 kind: Job metadata: name: job-demo spec: template: metadata: name: job-demo spe ...

  6. Alpha冲刺.2李霆政

    一.基本情况 队名:不行就摆了吧 组长博客:https://www.cnblogs.com/Microsoft-hc/p/15534079.html 小组人数: 8 二.冲刺概况汇报 谢小龙 过去两天 ...

  7. Django笔记&教程 4-3 模型(models)主键外键

    Django 自学笔记兼学习教程第4章第3节--模型(models)主键外键 点击查看教程总目录 参考:https://docs.djangoproject.com/en/2.2/ref/models ...

  8. printf("%d\n",printf("%d",printf("%d",i)));

    #include <stdio.h> int printf( const char *format, ... );首先 得看printf的返回类型是 int 这个函数的返回值是 你输出的位 ...

  9. C#长程序(留着看)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. Redis篇:事务和lua脚本的使用

    现在多数秒杀,抽奖,抢红包等大并发高流量的功能一般都是基于 redis 实现,然而在选择 redis 的时候,我们也要了解 redis 如何保证服务正确运行的原理 前言 redis 如何实现高性能和高 ...