题目:

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. python 基础(五) 迭代器与生成器

    迭代器和生成器 迭代器 iterator (1) 迭代对象: 可以直接作用于for循环的 称为可迭代对象(iterable)可以通过 isinstance 判断是否属于可迭代对象 可以直接作用于for ...

  2. HDU3853(期望)

    题目很水了,但是原地打转的点……虽然不难想到这个坑,但是从数学的角度来讲期望不应该算正无穷嘛……为什么算0啊? ; ; int R, C; struct gird { db ori, right, d ...

  3. Python enumerate() 函数----枚举

    描述 enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中. Python 2.3. 以上版本可用,2. ...

  4. mongodb的基本命令操作

    mongodb在已经存在管理员的情况下,需要创建一个库 使用管理员进入mongodb的命令行界面 mongo admin -u 管理员名 -p 管理员密码 创建库(进入库) use 库名 创建当前库的 ...

  5. Linux 增加虚拟内存

    1. 用 df -h 命令找一个比较大的磁盘空间 2.建立swap文件 大小为2G count= 3.启用虚拟内存 1. 将swap文件设置为swap分区文件 mkswap swapfile #(由于 ...

  6. Nodejs 文件修改自动重启扩展

    使用 supervisor: 安装: # 全局安装 npm -g install supervisor 启动: supervisor app.js

  7. js js弹出框、对话框、提示框、弹窗总结

    js弹出框.对话框.提示框.弹窗总结 一.JS的三种最常见的对话框 //====================== JS最常用三种弹出对话框 ======================== //弹 ...

  8. 【转】HashMap,ArrayMap,SparseArray源码分析及性能对比

    HashMap,ArrayMap,SparseArray源码分析及性能对比 jjlanbupt 关注 2016.06.03 20:19* 字数 2165 阅读 7967评论 13喜欢 43 Array ...

  9. 5、两个栈实现队列------------>剑指offer系列

    题目 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路 栈1: 用于入队列存储 栈2: 出队列时将栈1的数据依次出栈,并入栈到栈2中 栈2出栈即栈1的底部数据 ...

  10. [BZOJ2982]combination Lucas定理

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2982 $C(N,M)\% P = C(N\% P,M\% P) * C(N/P,M/P)\ ...