leetcode 8

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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- linux命令(10)使用kill杀死含有指定关键字的进程
命令:ps -ef|grep keyword|grep -v grep|cut -c 9-15|xargs kill -9 批量杀死包含关键字“keyword”的进程. "ps -ef&qu ...
- TFS使用中的问题
http://msdn.microsoft.com/zh-cn/library/vstudio/fda2bad5.aspx 项目映射步骤(项目上传): a.新建一个空的文件夹Team Server,用 ...
- PHP $_SERVER['HTTP_REFERER'] 获取前一页面的 URL 地址
PHP $_SERVER['HTTP_REFERER'] 使用 $_SERVER['HTTP_REFERER'] 将很容易得到链接到当前页面的前一页面的地址.一个例子如下: index.php(实际地 ...
- 怎样查看oracle当前的连接数
SQL> select count(*) from v$session #当前的连接数SQL> Select count(*) from v$session where status='A ...
- linux 可用内存查看
用free命令查看. 下面是一个例子(单位是MB): [root@linuxzgf ~]# free -m total used free shared buffers cachedMem: 7982 ...
- SQL在指定列后添加新的列
ALTER TABLE `MR_CustomerShopFuture` ADD COLUMN `ProcessID` INT(11) DEFAULT '0' COMMENT '审核流程ID';
- NoSuchMethodError: resolveTypeArguments
NoSuchMethodError: resolveTypeArguments——因为spring版本冲突导致,观察解压war包后lib中有几个spring.在pom中通过exclusion解决 Ht ...
- VC 三点 划 曲线
y = ax2+bx+c 条件,三点成一曲线 pointone(x1,y1)//(y1在X柱上,必须为零,如果不为零这个公式要重新求值) pointtwo(x2,y2)// 可以为任意值 pointt ...
- mysql的一些心得
1.unsigned修饰整型 ,既为非负数,用此类型可以增加数据长度! 类型 大小 范围(有符号) ...
- .net重启iis线程池和iis站点程序代码【转】
转:http://www.jb51.net/article/44162.htm 重启站点: 复制代码代码如下: /// <summary> /// 根据名字重启站点.(没重 ...