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. Tomcat的8009端口AJP的利用

    Tomcat在安装的时候会有下面的界面,我们通常部署war,用的最多的是默认的8080端口. 可是当8080端口被防火墙封闭的时候,是否还有办法利用呢? 答案是可以的,可以通过AJP的8009端口,下 ...

  2. Opencv 2.4.9在Ubuntu下的配置与安装

    [原]Opencv 2.4.9在Ubuntu下的配置安装  Opencv 2.4.9在Ubuntu下的配置与安装 surgewong@gmail.com http://blog.csdn.net/su ...

  3. centos7安装mysql

    centos7安装mysql 1 查找系统是否安装了myql rpm -q mysql mysql-server1.1如果安装了.就删除 sudo yum -y remove mysql mysql- ...

  4. Jmeter监控服务器性能

    JMeter是一款压力测试工具,我们也可以用它来监控服务器资源使用情况.JMeter正常自带可以通过Tomcat的/manager/status来监控服务资源使用情况.这种情况只能监控Tomcat支持 ...

  5. Mongo 备份 还原

    表还原 mongorestore --collection Inquiry0315 -d Estate --drop --dir D:/backup/20150731/Estate/Inquiry.b ...

  6. ios下,对于position:fixed支持不完美的额解决方案

    ios下,当有文本框时,会调用输入法,而这个时候,定位(fixed)在底部的东西,就会被弹上例,离底部有段距离,这算是个坑了. 我的解决方案是这样的. 除了定位在底部的元素外,用一个大div把其他元素 ...

  7. [kuangbin带你飞]专题十二 基础DP1

            ID Origin Title   167 / 465 Problem A HDU 1024 Max Sum Plus Plus   234 / 372 Problem B HDU 1 ...

  8. SQL SERVER 2005快捷键+visual studio 2008 快捷键

    一.SQL SERVER 2005快捷键  快捷键                                       功能           CTRL       +       SHIF ...

  9. (easy)LeetCode 225.Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  10. @Required @Autowired @Resource注解详解

    一.@Required注解用于检查特定的属性是否设置 1.RequiredAnnotationBeanPostProcessor 为该注解的处理器,即bean后置处理器,检查所有带有该解的bean属性 ...