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. iOS开发数据库SQLite的使用

    iOS系统自带Core Data来进行持久化处理,而且Core Data可以使用图形化界面来创建对象,但是Core Data不是关系型数据库,对于Core Data来说比较擅长管理在设备上创建的数据持 ...

  2. eclipse hibernate 插件测试1

    今天先测试了hibernate tools 安装 在eclipse marketplace里面搜索 hibernate tools 就能找到 网上很多文章所说的使用 install new softw ...

  3. SQL Sever 2008 安装

    http://jingyan.baidu.com/article/4b07be3c1daf1248b380f33b.html 大致出错信息如下:RebootRequiredCheck 检查是否需要挂起 ...

  4. spring配置带参数的视图解析器:ParameterMethodNameResolver

    1.配置处理器 <!-- 处理器 --> <bean id="myController" class="cn.cnsdhzzl.controller.M ...

  5. JAVA读取EXCEL文件异常Unable to recognize OLE stream

    异常: jxl.read.biff.BiffException: Unable to recognize OLE stream at jxl.read.biff.CompoundFile.<in ...

  6. [工作需求]linux常用命令以及vim常用命令

    一.             Linux 常用命令 mkdir dirname新建文件夹 cd ~ 进入自己的家目录 cd dirname 进入名字为dirname的目录: l 显示当前文件夹下的文件 ...

  7. pycharm 导包

    如果需要requests包,python没有自带.可以执行命令:pip install requests,自动安装导入.

  8. oracle常见权限分配

    1.GRANT 赋于权限 常用的系统权限集合有以下三个: CONNECT(基本的连接), RESOURCE(程序开发), DBA(数据库管理) 常用的数据对象权限有以下五个: ALL ON 数据对象名 ...

  9. input 中的enabled与disabled属性

    <style type="text/css"> *{ padding:; margin:; list-style-type: none; box-sizing:bord ...

  10. C# 多线程详解 Part.03 (定时器)

    Timer 类:     设置一个定时器,定时执行用户指定的函数.定时器启动后,系统将自动建立一个新的线程,执行用户指定的函数. using System; using System.Threadin ...