题目

字符串转换为数字。

解法

这道题的意思是要考虑到,如果有前置的空字符,则跳过;如果超出数字范围,则返回最大/最小整数;如果碰到第一个不能转换的字符,则返回。

代码

 class Solution {
public:
int atoi(const char *str) {
int sign = , result = ;
int len = strlen(str);
int i = ; for( ; i < len && str[i] == ' '; ++i)  //跳过前置的空字符
; if(str[i] == '+')    //如果有符号且为正
++i;
else if(str[i] == '-') //负数
{
sign = -;
++i;
} for( ; i < len; ++i)
{
if(str[i] < '' || str[i] > '')  //不能转换的字符
break;
if(result > INT_MAX/ || (result == INT_MAX/ && str[i] > INT_MAX% + '')) //即将超出int范围
return sign == - ? INT_MIN : INT_MAX;
result *= ;
result += str[i] - '';
} return result * sign;  //记得加上符号位
}
};

LeetCode题解——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——8. String to Integer (atoi)

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

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

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

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

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

随机推荐

  1. GIT权威手册及常用命令用法

    http://git-scm.com/book/zh Git Stash用法 http://www.cppblog.com/deercoder/archive/2011/11/13/160007.ht ...

  2. TCL语言笔记:TCL中的控制结构命令

    一.引言 控制结构允许程序根据不同的状态.条件和参数来选择不同的处理和执行路径,从而使代码具有更强的灵活性.健壮性和可读性. Tcl 提供了 if.if/else.if/elseif.foreach. ...

  3. 本人arcgis api for javascript中常见错误总结

    1. 2.对象不支持"replace"属性或方法 解决办法:一般在ie中执行js会报这样的错误,基本问题就是你引用了某个对象中不存在的方法,可能是这个方法本来存在而你写错了,或者调 ...

  4. Csharp日常笔记

    1. 1.退出程序                   this.Close();             //方法退关闭当前窗口. Application.Exit();       //方法退出整 ...

  5. JS代码片段:日期格式化

    Date.prototype.format = function(format) { var date = { "M+": this.getMonth() + 1, "d ...

  6. muParser公式库使用简介( 转)

    muParser是一个跨平台的公式解析库,它可以自定义多参数函数,自定义常量.变量及一元前缀.后缀操作符,二元操作符等,它将公式编译成字节码,所以计算起来非常快. 当前版本V1.28,官方网址http ...

  7. .net 生成拼音码与五笔码

    首先加入配置文件: <?xml version="1.0" encoding="utf-8" ?> <CodeConfig> <S ...

  8. [HIHO1051]补提交卡(枚举,贪心)

    题目链接:http://hihocoder.com/problemset/problem/1051 思路:先排序,然后枚举连续的长度为m的子段,用这个段之后的第一个天数减去这个段之前的第一个天数再-1 ...

  9. gulp.watch监听文件

    Gulp.watch()会返回我们熟知的watcher.我们可以利用watcher来监听额外的事件或者向watch中添加文件. 例如,在执行一系列任务和调用一个函数时,你就可以在返回的watcher中 ...

  10. Android中LayoutInflater的使用

    Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater 的作用类似于 findViewById(),不同点是LayoutInflater是用来找layou ...