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.

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

Solution 1:

class Solution
{
public:
int myAtoi(string str)
{
int num = ;
int sign = ;
const int n = str.length(); int i = ;
while(str[i] == ' ' && i < n) i++; if(str[i] == '+') i++;
else if(str[i] == '-') { sign = -; i++; } for(; i < n; i++)
{
int temp = str[i] - '';
if(temp < || temp > )
break; if(sign == -)
{
if(num < (INT_MIN + temp) / )
return INT_MIN;
num = num * - temp; }
else
{
if(num > (INT_MAX - temp) / )
return INT_MAX;
num = num * + temp;
}
}
return num;
} };

Solution 2:

class Solution
{
public:
int myAtoi(string str)
{
int num = ;
int sign = ;
const int n = str.length(); int i = ;
while(str[i] == ' ' && i < n) i++; if(str[i] == '+') i++;
else if(str[i] == '-') { sign = -; i++; } for(; i < n; i++)
{
int temp = str[i] - '';
if(temp < || temp > )
break; temp = (sign == ) ? temp : -temp; // Attention if(sign == -)
{
if(num < (INT_MIN - temp) / )
return INT_MIN;
}
else
{
if(num > (INT_MAX - temp) / )
return INT_MAX;
}
num = num * + temp;
}
return num;
} };

String to Integer (atoi) ---- LeetCode 008的更多相关文章

  1. String to Integer (atoi) leetcode

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  2. String to Integer (atoi) leetcode java

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  3. 8. String to Integer (atoi) ---Leetcode

    Implement atoi to convert a string to an integer. 题目分析: 题目本身很简单就是将一个字符串转化成一个整数,但是由于字符串的千差万别,导致在实现的时候 ...

  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. Kotlin实现LeetCode算法题之String to Integer (atoi)

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

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

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

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

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

  8. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  9. LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

随机推荐

  1. SyntaxError: Non-ASCII character '\xe7' in file解决方法

    SyntaxError: Non-ASCII character '\xe7' in file 出现这种错误的原因是程序中的编码出问题了,只要在程序的最前面加上 最前面的意思是在最前面,包括在注释的前 ...

  2. Qt之坐标系统

    简述 坐标系统是由QPainter类控制的,再加上QPaintDevice和QPaintEngine类,就形成了Qt的绘图体系. QPainter:用于执行绘图操作. QPaintDevice:二维空 ...

  3. c++表达式的一些小小的注意事项

    3+12>>1 = 7; 12>>1+3 =0; 3+(12>>1)=9;

  4. Linux中Matlab保存多个数据到同一个文件当中

    % load pyrim % NumTrain = 50; % load machine %NumTrain = 150; % load housing % NumTrain = 300; % loa ...

  5. tsne降维可视化

    Python代码:准备训练样本的数据和标签:train_X4000.txt.train_y4000.txt 放于tsne.py当前目录.(具体t-SNE – Laurens van der Maate ...

  6. Android基础之项目结构分析

    创建了第一个Android项目,用工具开发Android项目,我们有必要熟悉项目的目录结构,清楚各个项目下面放置的是什么东西.展开整个项目,其根目录结构(选用不同版本的SDK文件目录结构会有一些不同, ...

  7. 用eclipse开发javaweb项目

    准备工作:安装并配置jdk jdk环境变量:1.C:\Program Files\Java\jdk1.6.0_31\bin   path的配置2.C:\Program Files\Java\jdk1. ...

  8. js——页面回到顶部

    很久都没有去慕课网学习学习了,刚恰好就看见了一个用的比较多的小例子——页面回到顶部,记得之前自己也是在初学web时,被这个坑了一回,因此今天特地拿来分享分享…… <!DOCTYPE html&g ...

  9. Matrix-Tree定理

    感觉又学到了一个利器! 感谢Vfleaking神犇,传送门 http://vfleaking.blog.163.com/blog/static/1748076342013112523651955/   ...

  10. cordova 5.0 白名单

    最新的cordova 5.0 更新了白名单机制,增强了安全性,但是也给我们在开发中带来了很多问题: 当你引入谷歌.百度地图时,会出现 Failed to load resource -- 解决办法: ...