题目:

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.

分析:

题目理解就废了一番功夫,看了几遍也没有抓住精髓。
该题目是说将string类型的字符串转换成整型数据,类似于C++库里的atoi函数,解决该题目的关键在于两个方面:
(1)字符串格式的合法判断
(2)转换结果的溢出判断
首先,对于字符串格式,空格不计入计算,应从第一个非空字符开始判断,首字母只能是符号(+、-)与数字的一种;从计算开始遍历字符串,到最后一位数字为止;
其次,对于转换结果,我们知道整型数据的范围是INT_MIN(-2147482648)到INT_MAX(2147483647),超出范围则返回最大与最小值。所以我们可以开始用long long类型的变量存储结果;

AC代码:

class Solution {
public:
int myAtoi(string str) { if(str.length() == 0)
return 0;
//用于存储结果
long long result = 0 ;
int sign = 1 , i=0; while(str[i] == ' ')
{
if (str[i] == ' ')
i++;
} if(str[i] == '+')
i++;
else if(str[i] == '-')
{
sign = -1;
i++;
} for(int j=i ; j<str.length() ; j++)
{
if(str[j]>='0' && str[j]<='9')
{
result = result * 10 + (str[j]-'0');
if(result > INT_MAX)
return sign<0 ? INT_MIN : INT_MAX;
}
else
break;
}
result *= sign;
return (int)result;
}
};

LeetCode(8)String to Integer (atoi)的更多相关文章

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

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

  2. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  3. LeetCode【8】. String to Integer (atoi) --java实现

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

  4. leetcode第八题--String to Integer (atoi)

    Problem: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible inp ...

  5. [leetcode]经典算法题- String to Integer (atoi)

    题目描述: 把字符串转化为整数值 原文描述: Implement atoi to convert a string to an integer. Hint: Carefully consider al ...

  6. 【leetcode❤python】 8. String to Integer (atoi)

    #-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...

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

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

  8. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

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

随机推荐

  1. [软件工程基础]2017.11.02 第六次 Scrum 会议

    具体事项 燃尽图 每人工作内容 成员 已完成的工作 计划完成的工作 工作中遇到的困难 游心 #10 搭建可用的开发测试环境:#9 阅读分析 PhyLab 后端代码与文档:#8 掌握 Laravel 框 ...

  2. oracle dual是个什么表

    这几天一直在研究oracle,常常会用到dual这个系统表,dual表到底是一个什么表?带着疑问查了百度了一下,现在总结一下:DUAL是Oracle与数据字典一起自动创建的一个表,它只有一列:DUMM ...

  3. centOS+uwsgi+nginx 部署flask项目,问题记录

    用flask做的项目想要部署到centOS系统上,填了一些坑,终于成功了,记录一下遇到的问题: 此次部署主要是按照这个博客进行的 https://www.cnblogs.com/Ray-liang/p ...

  4. EDAS提交论文字体未嵌入

    一.深夜更一波,刚刚在EDAS提交论文,提示格式不通过,说我有字体未嵌入.但是之前一直都没有问题,这次只是在LaTeX中嵌图的时候把eps换成PDF了.所以问题应该是出在我的PDF图里,里面有字体未被 ...

  5. PHP知识点总结3

    PHP 函数 PHP 的真正威力源自于它的函数. 在 PHP 中,提供了超过 1000 个内建的函数. <html> <body> <?php function writ ...

  6. arcgis jsapi接口入门系列(1):地图

    地图相关 //地图相关demo mapFun: function () { //获取地图中心点 let center = this.mapView.center; //地图中心点坐标(同地图坐标系) ...

  7. Python相关机器学习

    Python机器学习库 Python的机器学习库汇总与梳理 机器学习之开源库大总结

  8. root.sh脚本支持checkpoints文件实现重复运行

    安装集群GRID/GI一般包括三个过程:首先,运行OUI/RunInstaller输入集群配置信息,其次,拷贝/编译集群文件,最后,以root用户运行root.sh脚本配置集群/启动集群,其中运行ro ...

  9. UVA 1220 Party at Hali-Bula (树形DP)

    求一棵数的最大独立集结点个数并判断方案是否唯一. dp[i][j]表示以i为根的子树的最大独立集,j的取值为选和不选. 决策: 当选择i时,就不能选择它的子结点. 当不选i时,它的子结点可选可不选. ...

  10. 怎么在webstorm中设置代码模板

    大家都知道webstorm对程序员来说是一个很好用的IDE.我们输入几个关键字,webstorm就会给出提示,大大提高了我们的开发效率,可有时候webstorm的默认设置不能满足我们的个性化代码模板的 ...