题目

字符串转换为数字。

解法

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

代码

 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. netty是什么?

    Netty是什么? 相对于Tomcat这种Web Server(顾名思义主要是提供Web协议相关的服务的),Netty是一个Network Server,是处于Web Server更下层的网络框架,也 ...

  2. IT讲师韩顺平:我为什么辞去百万年薪,自己创业?

    先自我介绍一下,我叫韩顺平,是一名IT讲师.国内很多自学PHP和Java的朋友都看过我的视频课程,算是有些知名度. 15年8月从传智辞职后,很多朋友非常关心我的去向,网上也流传各种说法,有的说我和某某 ...

  3. python 下划线的使用(转载:安生犹梦 新浪博客)

    Python 用下划线作为变量前缀和后缀指定特殊变量. _xxx      不能用'from module import *'导入 __xxx__ 系统定义名字 __xxx    类中的私有变量名 核 ...

  4. 修改 eclipse 文件编码格式

    如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,Eclipse工作空间(workspace)的缺省字符编码是操作系统缺省的编码,简 ...

  5. highcharts 结合phantomjs纯后台生成图片系列二之php2

    上篇文章中介绍了phantomjs的使用场景,方法. 本篇文章详细介绍使用php,highcharts 结合phantomjs纯后台生成图片.包含一步步详细的php代码 一.highcharts 结合 ...

  6. makefile生成静态库和动态库

    库是一种软件组件技术,库里面封装了数据和函数. 库的使用可以使程序模块化. Windows系统包括静态链接库(.lib文件)和动态链接库(.dll文件). Linux通常把库文件存放在/usr/lib ...

  7. 解决eclipse maven 项目重新下载包这个问题

    问题:eclipse项目使用maven下载依赖包,但是有时候断网什么来着就不会自动下载了,挺蛋疼了. 所以,需要我们重新更新项目下载呢. 首先是要在maven的conf文件下setting.xml配置 ...

  8. 如何在Ubuntu上安装最新版本的Node.js

    apt-get update apt-get install -y python-software-properties software-properties-common add-apt-repo ...

  9. Awesome Javascript(中文翻译版)

    [导读]:GitHub 上有一个 Awesome – XXX 系列的资源整理.awesome-javascript 是 sorrycc 发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架 ...

  10. JAVA使用apache http组件发送POST请求

    在上一篇文章中,使用了JDK中原始的HttpURLConnection向指定URL发送POST请求 可以看到编码量有些大,并且使用了输入输出流 传递的参数还是用“name=XXX”这种硬编的字符串进行 ...