[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具有相同功能的函数,即能够把字符串转 ...
随机推荐
- C++获取本机的ip地址程序
#include <WinSock2.h> #pragma comment(lib,"ws2_32") //链接到ws2_32动态链接库 class CInitSock ...
- Oracle 创建表空间借鉴 保留,占版权留言告知
/*分为四步 */ /*第1步:创建临时表空间 */ create temporarytablespace user_temp tempfile 'D:\oracle\oradata\Oracle9i ...
- 针对android&ios yuv旋转、镜像、格式转换、裁剪 算法实现<转>
http://blog.csdn.net/dangxw_/article/details/50903693
- Linux大牛分享的7道经典面试题和秒收 offer 的技巧
笔者其实没有想到去面试,只是在智联上更新了一下简历,就陆陆续续接到很多猎头的邮件和电话,闲话少说,下面就分享给大家Linuxer的面试经历: 首先,猎头或者公司人资会把公司的介绍及岗位要求发到你邮箱( ...
- Redis常用类型数据操作
sortedset: 添加: zadd key score1 member1 score2 member2... zad mysort 90 laosong 100 zhangsan 获得:zsco ...
- <转>Linux环境进程间通信(五): 共享内存(下)
http://www.ibm.com/developerworks/cn/linux/l-ipc/part5/index2.html 系统调用mmap()通过映射一个普通文件实现共享内存.系统V则是通 ...
- SData:优雅的数据交换方案
SData的网址是https://github.com/knat/SData. 数据交换方案可以分为两类:有纲要(schema)的和无纲要的.有纲要的数据交换方案有Google的Protocol Bu ...
- 互联网大规模数据分析技术(自主模式)第五章 大数据平台与技术 第10讲 大数据处理平台Hadoop
大规模的数据计算对于数据挖掘领域当中的作用.两大主要挑战:第一.如何实现分布式的计算 第二.分布式并行编程.Hadoop平台以及Map-reduce的编程方式解决了上面的几个问题.这是谷歌的一个最基本 ...
- 如何清除保存的FTP用户名和密码
很多人习惯登陆FTP时选择保存密码,这样下次只需打开地址就可以进入FTP的页面了.这样确实方便,但如果遇到更换别的FTP用户名登陆,该怎么办?相信不少人还真答不出.重装浏览器,或者重装系统?呵呵, ...
- 226. Invert Binary Tree 翻转二叉树
[抄题]: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 [暴力解法]: 时间分析: 空间分 ...