[LeetCode 题解]: String to Interger (atoi)
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.
spoilers alert... click to show requirements for atoi.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
提示: 使用string的特性来判断是否越界。 strcmp
1.去掉字符串之前多余的空格
2. 判断正负号
3. 记录数字字符串。
4. 判断是否越界。
利用好:strcmp
class Solution {
public:
int atoi(const char *str) {
const char* Max = "";
const char* Min = "";
char b[];
int number=;
int below=,i=,j=;
while(str[i]==' ')i++;
if(i==strlen(str)) return ;
if(str[i]=='+'||str[i]=='-')
{
if(str[i]=='-') below=;
i++;
}
for(;i<strlen(str);i++)
{
if(str[i]>='' && str[i]<='')
b[j++]=str[i];
else
break;
}
b[j]='\0';
int lb = strlen(b);
if(lb>)
{
if(below) return INT_MIN;
else return INT_MAX;
}
else if(lb==)
{
if(below)
{
if(strcmp(b,Min)>=) return INT_MIN;
else
{
for(j=;j<lb;j++)
number= number* + b[j]-'';
}
}
else
{
if(strcmp(b,Max)>=) return INT_MAX;
else
{
for(j=;j<lb;j++)
number= number* + b[j]-'';
}
}
}
else
{
for(j=;j<lb;j++)
number= number* + b[j]-'';
}
return below>?-number:number;
}
};
分支结构有点多,需要仔细分析分析。
转载请注明出处: http://www.cnblogs.com/double-win/谢谢
[LeetCode 题解]: String to Interger (atoi)的更多相关文章
- LeetCode题解——String to Integer(atoi)
题目: 字符串转换为数字. 解法: 这道题的意思是要考虑到,如果有前置的空字符,则跳过:如果超出数字范围,则返回最大/最小整数:如果碰到第一个不能转换的字符,则返回. 代码: class Soluti ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- [LeetCode 题解]: Roman to Interger
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a ro ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- leetcode day6 -- String to Integer (atoi) && 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 ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- LeetCode——8. String to Integer (atoi)
一.题目链接:https://leetcode.com/problems/string-to-integer-atoi/ 二.题目大意: 实现一个和C语言里atoi具有相同功能的函数,即能够把字符串转 ...
随机推荐
- sublime text 怎么浏览包
点击到设置里 里面有个包浏览.
- [z]单次遍历带权随机选取
http://www.gocalf.com/blog/weighted-random-selection.html 没事可以看看,这个博客里面很多文章不错
- Netty的ChannelHandler,ChannelHandlerContext,ChannelPipeline
本小节一起学习一下ChannelHandler,ChannelHandlerContext,ChannelPipeline这三个Netty常用的组件,不探究它们的底层源码,我们就简单的分析一下用法 首 ...
- Arduino UNO 键盘记录器中时钟接到2口或3口,其它接口不行。马上就要放弃了。要修改例子中时钟的引脚。
- 直接用SQL语句把DBF导入SQLServer
直接用SQL语句把DBF导入SQLServer 在SQLServer中执行 SELECT * into bmk FROM OpenDataSource( ’Microsoft.Jet.OLEDB. ...
- PHP - 请求阻塞,Session写阻塞
之前写某些代码的时候,发现用户莫名奇妙地阻塞了,而且这种阻塞的情况还比较难以形容: 使用session过程中,在开启session后,同一浏览器,执行同一程序,不同页面会被锁.不同浏览器不会出现这种情 ...
- proxmox 安装ROS 备忘
虚拟机设置:使用qemu64 CPU和vrtio网卡在家里测试性能最好.
- git命令用法
git svn 说明 git pull svn update git add 要提交的文件名 svn add git rm svn rm,del git commit -m '备注一下提 ...
- 使用JavaScript调用aspx后台代码
方法1:js同步调用 触发: onclick="javascript:share('<%# Eval("id_File") %>')" 页面函数: ...
- Unity iOS 项目的一种性能评测方法
[Unity iOS 项目的一种性能评测方法]