string类型转换为int类型,需要考虑不同的转换情况。

“   04”  转换结果   4;

“   4   43”  转换结果  4;

“a@12 ”   转换结果    0;

“12a”    转换结果    12;

“ +12”   转换结果    12;

“ +  12”  转换结果   0;

“ -12”   转换结果     -12;

“  -  12”  转换结果    0;

“ +-12”   转换结果   0;

其次就是对边界的考虑,若转换之后的数越上界,则返回上界;若转换之后的数越下界,则返回下界。

代码如下:

class Solution {
public:
int myAtoi(string str) {
int result = ;
bool sign = true;
int tag = ;
for(int i = ; i < str.length(); ++i)
{
if(str[i] == ' ' && tag == )
{
continue;
}
if(str[i] == '+' && tag == )
{
tag = ;
continue;
}
if(str[i] == '-' && tag == )
{
tag = ;
sign = false;
continue;
}
while(i < str.length())
{
if((str[i] - '') < || (str[i] - '') > )
{
return sign? result : -result;
}
if(result > INT_MAX / )
{
return sign? INT_MAX : INT_MIN;
}
result *= ;
if ((str[i] - '') > (INT_MAX - result))
return sign? INT_MAX : INT_MIN;
result = result + str[i] - '';
i ++;
}
}
return sign? result : -result;
}
};

leetcode 8的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. CentOS 防火墙打开和关闭端口(转载)

    From:http://jianzhong5137.blog.163.com/blog/static/982904920126202313376/ http://soft.chinabyte.com/ ...

  2. Java多线程之捕获异常

    1.主线程不能捕获到子线程的异常 package Thread.Exection; import java.util.concurrent.ExecutorService; import java.u ...

  3. NAND FLASH均衡算法笔记(转)

    转来一篇关于NAND FLASH均衡算法的文章,加上一点思考和笔记,认为这种思考有助于更深刻的理解,更好的记忆,所以也算半原创了吧,最起码笔记是原创的.有意思的是,帖子提起这个算法并不是因为嵌入式开发 ...

  4. Cordova是做什么的

    Cordova提供了一组设备相关的API,通过这组API,移动应用能够以JavaScript访问原生的设备功能,如摄像头.麦克风等. Cordova还提供了一组统一的JavaScript类库,以及为这 ...

  5. aria-expanded,aria-hidden到底做什么用?

    aria-expanded表示展开状态.默认为undefined, 表示当前展开状态未知.其它可选值:true表示元素是展开的:false表示元素不是展开的. aria-hidden字符串.可选值为t ...

  6. datetime时区转换

    http://www.dannysite.com/blog/122/ Python标准库中的datetime模块提供了各种对日期和时间的处理方法.从本文的主题着手,首先利用datetime中提供的ut ...

  7. nyoj 97 兄弟郊游问题

    点击打开链接 兄弟郊游问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 兄弟俩骑车郊游,弟弟先出发,每分钟X米,M分钟后,哥哥带一条狗出发.以每分钟Y米的速度去追弟弟 ...

  8. 菜鸟-手把手教你把Acegi应用到实际项目中(11)-切换用户

    在某些应用场合中,我们可能需要用到切换用户的功能,从而以另一用户的身份进行相关操作.这一点类似于在Linux系统中,用su命令切换到另一用户进行相关操作.      既然实际应用中有这种场合,那么我们 ...

  9. HTML控件-Select

    从今天开始,编写对于html控件的特性的探索文章,会广泛的引用网络的资源,所以本文的版权属于广大人民群众,欢迎转载,也同样禁止商业应用. [高手勿喷,标签页点击红色叉叉] select控件有一个特性: ...

  10. Android——android:gravity 和 android:layout_Gravity

    LinearLayout有两个非常相似的属性: android:gravity与android:layout_gravity. 他们的区别在于: android:gravity 属性是对该view中内 ...