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. win7 安装 node-sass报错

    由于国内网络问题,所以会导致下载node-sass二进制包失败 只需要在 ~/.npmrc(当前用户家目录下)添加下面一行: sass_binary_site=https://npm.taobao.o ...

  2. EF-CodeFirst系列100

    .学习资料地址: CodeFirst:https://msdn.microsoft.com/zh-cn/data/jj193542 一.CodeFirst迁移(Migration) .工具--> ...

  3. jquery.raty.js 评星插件的使用

    需要实现一个五星好评的功能,所以找到了这个JQ插件,使用起来还算简单,在这里记录下使用的方式. 第一步:导入这个插件和压缩包中的img文件夹 <script type="text/ja ...

  4. PDF预览之PDFObject.js总结

    get from:PDF预览之PDFObject.js总结   PDFObject.js - 将PDF嵌入到一个div内,而不是占据整个页面(要求浏览器支持显示PDF,不支持,可配置PDF.js来实现 ...

  5. Androdi Gradle build project info 很慢

    Androdi Gradle build project info 很慢 http://blog.csdn.net/stupid56862/article/details/78345584   原创  ...

  6. uva-11111-栈

    注意输入和输出的结果 -9 -7 -2 2 -3 -2 -1 1 2 3 7 9 -9 -7 -2 2 -3 -1 -2 2 1 3 7 9-9 -7 -2 2 -3 -1 -2 3 2 1 7 9- ...

  7. Windows下测量cpu利用率的类

    方法参考网上的一段代码,把它改成了类以方便调用,很简单,但是可以一用,哈哈. cpu_usage.h #include <windows.h> class CpuUsage { publi ...

  8. UNITY Destroy()和DestroyImadiate()的区别

    using System.Collections; using System.Collections.Generic; using System.Timers; using UnityEngine; ...

  9. arcgis mpk 打包地图 (数据管理)

    摘要 来自:http://help.arcgis.com/zh-cn/arcgisdesktop/10.0/help/index.html#/na/0017000000q5000000/ 对地图文档以 ...

  10. 迷你MVVM框架 avalonjs 1.3.1发布

    avalon1.3.1发布. interpolate支持注释节点做定界符,avalon.config({interpolate:["<!--","-->&qu ...