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.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

思路:INT最大是2147483647,最小是-2147483648。所以要尤其注意-2147483648取相反数是得不到2147483648的。

class Solution {
public:
int myAtoi(string str) {
int ret = ;
int i = ;
bool pos = true; while(str[i] == ' ') i++; //neglect the spaces at the start
if(str[i] == '+') i++;
else if(str[i] == '-') {
pos = false;
i++;
} while(str[i] != '\0'){
if(str[i] > '' || str[i] <'') break; //if invalid digit, stop scanning and return the current result
if(ret > || (ret == && str[i] - '' > )){ //if overflow, return the extreme value
if(pos) return INT_MAX;
else return INT_MIN;
}
ret = ret * + str[i] - '';
i++;
}
if(pos) return ret;
else return (-ret);
}
};

8.String to Integer (atoi) (INT; Overflow)的更多相关文章

  1. 【leetcode】String to Integer (atoi)

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

  2. 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) (java)

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

  4. 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 ...

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

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

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

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

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

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

  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 ...

随机推荐

  1. EasyMall 项目记录-环境搭建

    一.搭建项目运行的环境 (1)修改hosts文件 更改:C:\Windows\System32\drivers\etc目录下hosts文件 添加:127.0.0.1        www.easyma ...

  2. MapReduce C++ Library

    MapReduce C++ Library for single-machine, multicore applications Distributed and scalable computing ...

  3. RowToColumn

    SELECT S.NAME, sum(decode(S.COURSE,'语文',S.SCORE,0))"语文", sum(decode(S.COURSE,'数学',S.SCORE, ...

  4. 了解ES6

    内容: 1.ES6介绍及基础 2.模块.类和继承 3.ES6高级特性 4.Generator和Iterator 5.异步编程 6.函数相关 内容参考:<ES6 标准入门> ES6标准阅读链 ...

  5. Python之函数——基础篇

    函数 函数,在BASIC中,叫subroutine(子过程或子程序),在Pascal中叫做procedure(过程)和function,在C中只有function,在Java中叫method. 定义: ...

  6. selenium+python自动化84-chrome手机wap模式(登录淘宝页面)

    前言 chrome手机wap模式登录淘宝页面,点击验证码无效问题解决. 切换到wap模式,使用TouchActions模块用tap方法触摸 我的环境 chrome 62 chromedriver 2. ...

  7. uva140-暴力枚举

    题意:任意一个点都至少有一个点与其相连接,所有的点可以进行任意排列,总排列数为n!. 一个点带宽定义与它相连的点的最远距离,一个排列的带宽定义为,点中最大的带宽,找出带宽最小的那个排列,有多组,输出字 ...

  8. python学习笔记一和PHP的一些对比

    python和php一样是 解释性语言 php和php一样 定义变量不需要指定类型,C语言中的print函数 在python中也是适用的 python编码 适用缩进  4个空格,通常存储为UTF-8模 ...

  9. smyfony2-curd-数据库创建

    1创建类

  10. 数据库中的html在页面上显示

    在ASP页面中,动态显示html格式的数据的方法如下: .aspx文件: <div id="divBox" runat="server"></ ...