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. C语言sizeof

    一.关于sizeof 1.它是C的关键字.是一个运算符,不是函数: 2.一般用法为sizeof 变量或sizeof(数据类型):后边这种写法会让人误认为是函数,但这种写法是为了防止和C中类型修饰符(s ...

  2. 命令行登录mysql报Segmentation fault错误是怎么回事

    ==========解决方法============在源码包里,编辑文件 cmd-line-utils/libedit/terminal.c把terminal_set方法中的 char buf[TC_ ...

  3. whois配置

    $itemRules = array ( 'default' => array ( 'registry_domain_id' => 'Registry Domain ID:(.*?)', ...

  4. Chrome每次打開都要打開123.sogou.com

    剛開始還以為中毒了,又是殺毒又是掃描的,最後發覺,原來就是chrome的一個設置被改了. Chrome->設置->啟動時 : 選打开特定网页或一组网页->設置網頁 , 將其中的123 ...

  5. SmtpClient 类

    1, SmtpClient 类 http://www.cnblogs.com/panlijiao/archive/2012/11/14/2773836.html

  6. SyntaxError: Non-UTF-8 code starting with '\xba' in file 错误的解决方法!!

    第一次在Eclipse建立python工程,添加了自己新建的文件,写了一点代码,随后执行时候出现了错误,和昨天我在Visual Studio 2015里面一样,错误: SyntaxError: Non ...

  7. iPhone开发之UIScrollView初步

    来源:http://blog.csdn.net/htttw/article/details/7891396 iPhone开发之UIScrollView初步 今天我们初步介绍以一下iPhone开发中的U ...

  8. C++学习14 继承的概念及语法

    继承是类与类之间的关系,是一个很简单很直观的概念,与现实世界中的继承(例如儿子继承父亲财产)类似. 继承(Inheritance)可以理解为一个类从另一个类获取成员变量和成员函数的过程.例如类B继承于 ...

  9. Intellisense in Visual Studio for Microsoft Dynamics CRM 2016

    Intellisense in Visual Studio for Microsoft Dynamics CRM 2016 posted by dynamicsnick on may 18, 2016 ...

  10. svn: E175002: can not read HTTP status line

    问题:eclipse连接svn:https://bdsvn-pc/svn/Project,报错svn: E175002: can not read HTTP status line 解决办法:将域名改 ...