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.

spoilers alert... click to show requirements for atoi.

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.

提示: 使用string的特性来判断是否越界。 strcmp

1.去掉字符串之前多余的空格

2. 判断正负号

3. 记录数字字符串。

4. 判断是否越界。

    利用好:strcmp

 class Solution {
public:
int atoi(const char *str) {
const char* Max = "";
const char* Min = "";
char b[];
int number=;
int below=,i=,j=;
while(str[i]==' ')i++;
if(i==strlen(str)) return ; if(str[i]=='+'||str[i]=='-')
{
if(str[i]=='-') below=;
i++;
} for(;i<strlen(str);i++)
{
if(str[i]>='' && str[i]<='')
b[j++]=str[i];
else
break;
}
b[j]='\0';
int lb = strlen(b);
if(lb>)
{
if(below) return INT_MIN;
else return INT_MAX;
}
else if(lb==)
{
if(below)
{
if(strcmp(b,Min)>=) return INT_MIN;
else
{
for(j=;j<lb;j++)
number= number* + b[j]-'';
}
}
else
{
if(strcmp(b,Max)>=) return INT_MAX;
else
{
for(j=;j<lb;j++)
number= number* + b[j]-'';
}
}
}
else
{
for(j=;j<lb;j++)
number= number* + b[j]-'';
}
return below>?-number:number;
} };

分支结构有点多,需要仔细分析分析。

转载请注明出处: http://www.cnblogs.com/double-win/谢谢

[LeetCode 题解]: String to Interger (atoi)的更多相关文章

  1. LeetCode题解——String to Integer(atoi)

    题目: 字符串转换为数字. 解法: 这道题的意思是要考虑到,如果有前置的空字符,则跳过:如果超出数字范围,则返回最大/最小整数:如果碰到第一个不能转换的字符,则返回. 代码: class Soluti ...

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

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

  3. [LeetCode 题解]: Roman to Interger

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a ro ...

  4. [LeetCode][Python]String to Integer (atoi)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...

  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. 【leetcode】String to Integer (atoi)

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

  7. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

  8. Leetcode 8. String to Integer (atoi)(模拟题,水)

    8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...

  9. LeetCode——8. String to Integer (atoi)

    一.题目链接:https://leetcode.com/problems/string-to-integer-atoi/ 二.题目大意: 实现一个和C语言里atoi具有相同功能的函数,即能够把字符串转 ...

随机推荐

  1. C# 设计模式-单例模式(Singleton)

    所谓单例模式即所谓的一个类只能有一个实例,说白了,也就是类只能在内部实例一次,然后提供这一实例,外部无法对此类实例化. 单例模式的特点: 1.只能有一个实例: 2.只能自己创建自己的唯一实例: 3.必 ...

  2. 「小程序JAVA实战」小程序和后台api通信(28)

    转自:https://idig8.com/2018/08/19/xiaochengxujavashizhanxiaochengxuhehoutaiapitongxin28/ 开发最重要的就是实操! 小 ...

  3. Weak References

    http://docwiki.embarcadero.com/RADStudio/Seattle/en/Automatic_Reference_Counting_in_Delphi_Mobile_Co ...

  4. Scanner和BufferReader之区别

    在Java SE6中我们可知道一个非常方便的输入数据的类Scanner,位于java.util包中,这个Scanner的具体用法为Scanner in = new Scanner(System.in) ...

  5. ubuntu10.10手工安装jdk1.6

    声明:以下操作是在root用户下操作. 一.下载JDK首先,在Oracle的官网上下载JDK.http://www.oracle.com/technetwork/java/javase/downloa ...

  6. lucene3.0范围查找

    在lucene3.0以上版本中,范围查询也有很大的变化,RangeQuery已经不推荐使用,使用TermRangeQuery和NumericRangeQuery两个替代.TermRangeQuery: ...

  7. ADB Not Responding - Android Studio

    问题描述: 最近安装了Android Studio v1.0,运行的时候老是这个错误 解决方案: 网上有人说是已经有adb的进程在运行,可是打开任务管理器,找不到对应的adb 进程. 无奈之下,想到a ...

  8. Mac 通过gem安装CocoaPods及Pod的使用

    注:根据http://www.jianshu.com/p/6e5c0f78200a的文章做了部分修改 一.什么是CocoaPods CocoaPods是iOS项目的依赖管理工具,该项目源码在Githu ...

  9. www请求用到的Unescape

    //发送请求          WWW w = new WWW(url); //web服务器返回          yield return w;        if (!string.IsNullO ...

  10. NavigationController.viewControllers

      NSMutableArray *viewControllersArray = [NSMutableArray new];    // 获取当前控制器数组    for (CardLoanBaseT ...