8. String to Integer (atoi)

Total Accepted: 93917 Total Submissions: 699588 Difficulty: Easy

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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

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转换为int,本身并不难,但是边界条件很多:

1.允许开头*个空字符,允许开头有'-''+'

2.不允许数字中出现除'0'-'9'以外的字符

3.结果大于MAX小于MIN返回MAX个MIN

 class Solution {
public:
int myAtoi(string str) { int ret = , tmp;
int i = , flag = ;
int len = str.length(); // special case : ""
if (len == ) return ; // special case : " (+/-)123"
while (str[i] == ' ') i++;
if (str[i] == '-') {
i++;
flag = -;
}
else if (str[i] == '+')
i++; for (;i < len; i++) {
if (str[i] < '' || str[i] > '') break; tmp = ret * + str[i] - '';
if (tmp / != ret)
if (flag == ) return INT_MAX;
else return INT_MIN; ret = tmp;
} return ret * flag;
}
};

注意:1.可用ASCII码 2.line(25)判断是否越界写的很好

 tmp = ret *  + str[i] - '';
if (tmp / != ret)
if (flag == ) return INT_MAX;
else return INT_MIN;

leetcode-8-String to Integer (atoi) (已总结)的更多相关文章

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

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

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

  3. 【leetcode】String to Integer (atoi)

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

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

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

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

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

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

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

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

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

  8. LeetCode 8. String to Integer (atoi) (字符串到整数)

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

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

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  10. LeetCode 7 -- String to Integer (atoi)

    Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...

随机推荐

  1. 完全卸载vs2013、vs2015的方法

    Visual Studio安装过程会安装好多组件,如果想要卸载的话会出现一些因难,在控制面板不容易卸载干净,在Linux下的命令都有--help参数来显示命令的用法,今天突发奇想,在控制台下输入vs2 ...

  2. Winfrom 开源组件Control.FirefoxDialog使用

    1. 如果窗体是以模式窗体方式打开的,会出现点了应用,窗体就立马关闭.此时可能别的设置需要一块设置,这种就存在问题. var form1 = new Form1(); form1.ShowDialog ...

  3. CentOS6.5 安装codeblocks-13.12

    安装环境CentOS6.5 启动命令行 1.先安装gcc和gcc++,这个可以直接安装 # yum install gcc # yum install gcc-c++ 2.再安装gtk2,也是直接安装 ...

  4. [html]window.open 使用示例

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. phpcms模块开发中的小问题及解决方法

    1.模块菜单中文名出错 在编写安装模块时候可能需要更改extention.inc.php中定义中文名称,由于反复安装或者通过phpcms的扩展->菜单管理 修改菜单名会导致中文名失败.解决办法很 ...

  6. servlet笔记1

    Myeclipse关于Servlet项目文件的组织方面,如下: WEB-INF:这个目录名称和位置是固定的,放置在该目录下的文件或目录,对外界来说的封闭的,也就是客户端无法用http的任何方式访问到其 ...

  7. vb.net 与 c# 运算符区别

    vb.net vs c# 详细的Operators运算符区别 vb.net ===================== Comparison = < > <= >= <& ...

  8. vs读取ini文件

    读取string类型: DWORD GetPrivateProfileString(LPCTSTR lpAppName,LPCTSTR lpKeyName,LPCTSTR lpDefaut,LPSTR ...

  9. epoll用法【整理】

    l  epoll是什么? epoll是当前在Linux下开发大规模并发网络程序的热门人选,epoll 在Linux2.6内核中正式引入,和select相似,都是I/O多路复用(IO multiplex ...

  10. 18.4Sum (Map)

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...