String to Integer (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.

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.

atoi的原理:

1.如果字符串前面有空格,则去除所有空格

2.允许有正负号,需要读取出正负号,连续两个正负号则是错误的字符串

3.读取过正负号后,开始读取数字,直到读到不是数字的位置或者字符串结束时停止,解析出里面的数字

注意会溢出,溢出时返回INT_MAX或者INT_MIN

 class Solution {
public:
int atoi(const char *str) {
if(str==NULL) return ;
int result=;
int flag=;
while(*str==' ')
{
str++;
}
if(*str=='+')
{
flag=;
str++;
}
else if(*str=='-')
{
flag=-;
str++;
}
while(*str>=''&&*str<='')
{ int digit=*str-'';
if(result<=INT_MAX/)
{
result*=;
}
else
{
if(flag==) return INT_MAX;
else return INT_MIN;
} if(result<=INT_MAX-digit)
{
result+=digit;
}
else
{
if(flag==) return INT_MAX;
else return INT_MIN;
} str++;
} return flag*result;
}
};

【leetcode】String to Integer (atoi)的更多相关文章

  1. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  2. 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  3. LeetCode【8】. String to Integer (atoi) --java实现

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

  4. 【Leetcode】【Easy】String to Integer (atoi)

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

  5. 【LeedCode】String to integer(atoi)

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

  6. 【Leetcode-easy】String to Integer(atoi)

    题目要求:字符串->整型 * 1. 首先需要丢弃字符串前面的空格. * 2. 然后可能有正负号(注意只取一个,如果有多个正负号,那么说这个字符串是无法转换的,返回0.比如测试用例里就有个“+-2 ...

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

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

  8. 【Leet Code】String to Integer (atoi) ——常考类型题

    String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement ato ...

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

随机推荐

  1. nginx location的配置

    文章转自:http://www.ttlsa.com/nginx/nginx-location-configure/ location的语法配置规则: 语法规则: location [=|~|~*|^~ ...

  2. js库中$冲突的解决方法

    http://www.w3school.com.cn/jquery/core_noconflict.asp

  3. load and initialize

    NSObject是一切OC类的基类,所以我们必须对NSObject所有的方法有一个清楚的认识. + (void)load; 当类或者分类被加入到runtime时,load方法会被调用,也就是说在mai ...

  4. opencv笔记3:trackbar简单使用

    time:2015年 10月 03日 星期六 13:54:17 CST # opencv笔记3:trackbar简单使用 当需要测试某变量的一系列取值取值会产生什么结果时,适合用trackbar.看起 ...

  5. java中获取本地文件的编码

    import java.util.*; public class ScannerDemo { public static void main(String[] args) { System.out.p ...

  6. 循序渐进Linux 3:Linux下软件安装与管理

    一.源码安装 ./configuremakemake install 二.RPM包 1. 安装软件包 rpm -i [辅助选项] file1.rpm file2.rpm主选项 -i: install, ...

  7. Git Pull 避免用户名和密码方法

    在开发中使用的版本控制器时git , 每次使用命令"git pull"从服务器获得最新代码时,都需要输入用户名和密码,这样浪费了大量的时间和热情,在此背景下,本文在网上找到解决版本 ...

  8. MyEclipse------快速读取特定目录下的文件的内容(字节输入流)

    other.jsp <%@ page language="java" import="java.util.*" pageEncoding="UT ...

  9. apt-get常用命令

    apt-get常用命令 一,什么的是apt-get 高级包装工具(英语:Advanced Packaging Tools,简称:APT)是Debian及其衍生发行版(如:ubuntu)的软件包管理器. ...

  10. ios 正则邮箱

    - (BOOL) isEmail { NSString *emailRegEx = @"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0- ...