题目:

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. 最短路之Dijkstra(单源)HDU 2544

    #include <iostream> using namespace std; ; ][]; ]; int middist; ]; void dijkstra(int n,int m) ...

  2. CF #546div2D

    题目本质:只有能做到一路过关斩将的勇者才能冒泡过来救出女主. 主要代码: ; int n, m, a[maxn], ans; vector<int> edge[maxn]; set< ...

  3. TopCoder9915(期望dp)

    1.还是逆向. 2.状态是还剩红i黑j张时的期望,这样从0,0往R,B推.注意因为是逆着的,所以到了某一步发现期望为负时直接f[i][j]归零,意义是这之后(在递推中算是这之前)的都不摸了,到这就停( ...

  4. 2017 Multi-University Training Contest - Team 1 Balala Power!

    Talented Mr.Tang has n strings consisting of only lower case characters. He wants to charge them wit ...

  5. javascript要点(上)

    立即执行函数 即Immediately Invoked Function Expression (IIFE),正如它的名字,就是创建函数的同时立即执行.它没有绑定任何事件,也无需等待任何异步操作: ( ...

  6. 渣渣菜鸡为什么要看 ElasticSearch 源码?

    前提 人工智能.大数据快速发展的今天,对于 TB 甚至 PB 级大数据的快速检索已然成为刚需,大型企业早已淹没在系统生成的浩瀚数据流当中.大数据技术业已集中在如何存储和处理这些海量的数据上.Elast ...

  7. 解决win64无法添加curl扩展的问题

    网上试了很多方法都无效,最后直接用phpstudy集成安装包中对应版本的php_curl.dll替换即可

  8. m_pConnection.CreateInstance( "ADODB.Connection ") 执行错误 结果总是为NULL

    今天下午搞了下项目 数据库操作模块,总是出现m_pConnection.CreateInstance( "ADODB.Connection ") 执行错误,即m_pConnecti ...

  9. 源文件名长度大于系统支持的长度,无法删除,java主方法执行方式删除

    import java.io.File; /** * @author 海盗船长 * 2017年2月14日11:24:26 */ public class DeleteFiles { public st ...

  10. 三行命令搞定查询Python安装目录

    想为Python添加一个库文件到默认目录,却忘记了Python安装目录. 其实,只要用下面三行命令,就可以轻松得到Python安装路径了. 进入Python >>>import sy ...