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

  • LeetCode 8 String to Integer atoi字符串

    • 问题描述
    • 解题思路
    • 参考代码
  • By Scarb
  • Scarb’s Blog

Tags:
- Math
- String

问题描述

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.

解题思路

这题要实现atoi,将字符串转换成数字。
有很多细节需要注意。
包括: 空格、正负号、正负号重复出现、溢出。

参考代码

#include <iostream>
#include <string>
#include <cctype>
using namespace std;class Solution {public:int myAtoi(string str) {int result = 0;int sign = 1;const int n = str.length();int i = 0;while (str[i] == ' ' && i < n) ++i;     // 处理空格if (str[i] == '+')           // 处理正负号++i;else if (str[i] == '-'){sign = -1;++i;}for (; i < n; ++i){if (str[i] < '0' || str[i] > '9')       // 处理非法字符break;if (result > INT_MAX / 10|| (result == INT_MAX / 10 && (str[i] - '0') > INT_MAX % 10))      // 处理溢出return sign == -1 ? INT_MIN : INT_MAX;result = result * 10 + str[i] - '0';}return sign * result;}
};int main()
{string s1("2147483648");Solution sl = Solution();cout << sl.myAtoi(s1) << endl;system("pause");return 0;
}

LeetCode 8. String to Integer (atoi)(字符串)相关推荐

  1. 【细节实现题】LeetCode 8. String to Integer (atoi)

    LeetCode 8. String to Integer (atoi) Solution1:我的答案 参考链接:http://www.cnblogs.com/grandyang/p/4125537. ...

  2. 8. String to Integer (atoi) 字符串转成整数

    [抄题]: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: T ...

  3. leetcode 8. String to Integer (atoi)

    也许是我没有理解清楚题意,为什么输入+-2的时候要输出0,而不是输出2呢. public class Solution {public int myAtoi(String str) {if(str = ...

  4. LeetCode算法入门- String to Integer (atoi)-day7

    LeetCode算法入门- String to Integer (atoi)-day7 String to Integer (atoi): Implement atoi which converts ...

  5. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 1 clas ...

  6. [LeetCode] NO. 8 String to Integer (atoi)

    [题目] Implement atoi to convert a string to an integer. [题目解析] 该题目比较常见,从LeetCode上看代码通过率却只有13.7%,于是编码提 ...

  7. 008 String to Integer (atoi) [Leetcode]

    题目内容: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  8. String to Integer (atoi) leetcode java

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

  9. 8. String to Integer (atoi)

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

最新文章

  1. Linux 文件与目录管理+用户管理命令(详解+练习)
  2. 企业集成模式_企业集成模式简介
  3. bootstrap上传图片可实现查看上一张图片和下一张图片_如何实现像人民日报微信推文一样的的点亮效果?...
  4. Bootstrap表格内容垂直水平居中
  5. Django:应用程序的两种架构:C/S架构,B/S架构,(TCP, URL)HTTP,HTTP request, HTTP response
  6. Eclipse自动生成作者、日期注释功能设置
  7. 光盘放进电脑读不出来_U盘插入电脑读不出来?学会这3招,轻松解决USB无法读取的问题...
  8. Solidworks:Solidworks2016软件程序破解详细攻略说明(也适合长时间没使用SW2016再次破解)
  9. TestNG基础教程 - IntelliJ IDEA中配置TestNG.xml, 查看TestNG Report
  10. windows NT到底是什么
  11. 1466:Girls and Boys:优美的拆散早恋学生?
  12. IntelliJ IDEA CE 2019.2 发布
  13. 计算机地图综合制图实验报告,地图制图实验报告.doc
  14. 9月15日相约苏州丨共同探讨数智化时代,国产数据库的技术创新与实践
  15. Portapack应用开发教程 自定义开机图片
  16. 一网统管是什么?管什么,怎么管?通俗易懂
  17. 《深渊古纪》古剑奇谭衍生小说 阅读笔记
  18. java 控制台输出时间_Java获取时间打印到控制台代码实例
  19. intellidea 应用问题
  20. 《花千骨》为何被批“脑残”还能创造收视神话?

热门文章

  1. oracle判断是否为空为空赋值,sql数据库中ISNULL函数用法 sql语句判断值是否为空...
  2. 使用Python脚本实现web页面自动登录
  3. java统计输入出现次数并打印_Java键盘输入一个字符串,并且统计其中各种字符出现的次数...
  4. oracle查询时间交集,ORACLE SQL日期范围交集
  5. sqlite3 取整函数
  6. 你真的了解结构体和共用体吗?
  7. word中的修订的使用
  8. java反编译微信小程序_反编译微信小程序(最新)
  9. php打印分页的水印,php 打印水印函数示例
  10. pybullet入门