leetcode:String to Integer (atoi)
Implement atoi to convert a string to an integer.
Hint: Carefully consider all 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.
注意atoi的要求:
1、它会扫描字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回(返回转换后的整型数)
2、字符串中可能包含许多对函数行为无效的参数
3、如果字符串中不含有效参数(如:全是空格等),返回0
4、如果字符串中保存的有效值超出了整型的范围,那么根据其正负,返回 INT_MAX (2147483647) or INT_MIN (-2147483648)
理解了上述要求,则很容易写出以下代码:
class Solution {
public:
int myAtoi(string str) {
long long res = 0;//注意这里要用long long,避免溢出
int i=0;
int sign = 1;
while(str[i]==' ') i++;//跳过空格
if(str[i] == '-' || str[i] == '+') //如遇到正负号,将其存储在sign中
{
sign = str[i++] == '-'?-1:1;
}
while(str[i]>='0' && str[i]<='9')//将字符串中0-9的字符转换为整型数(不带符号)
{
res = res*10 + str[i++]-'0';
if(res>INT_MAX) return sign>0?INT_MAX:INT_MIN;//如整型溢出,根据sign的符号,返回相应值
}
return res*sign;//将符号加上
}
};
看看其他解法:
1、
class Solution {
public:
int myAtoi(string str) {
size_t index = str.find_first_not_of(' ');
if(index == string::npos) return 0;
long result = 0;//其实这里也最好用long long的,不过long也通过了所有的测试用例。
bool negative = false;
if(str[index] == '-') {
negative = true;
index++;
} else if(str[index] == '+') {
index++;
}
for(int i=index; i<str.size(); i++) {
if(isdigit(str[i])) {
result = result * 10 + (str[i]-'0');
if(negative && -result <= INT_MIN) return INT_MIN;
if(!negative && result >= INT_MAX) return INT_MAX;
} else {
break;
}
}
if(negative) result = -result; return int(result);
}
};
注1:size_t和size_type的区别
为了使自己的程序有很好的移植性,c++程序员应该尽量使用size_t和size_type而不是int, unsigned
1. size_t是全局定义的类型;size_type是STL类中定义的类型属性,用以保存任意string和vector类对象的长度
2. string::size_type 制类型一般就是unsigned int, 但是不同机器环境长度可能不同 win32 和win64上长度差别;size_type一般也是unsigned int
3. 使用的时候可以参考:
string::size_type a =123;
vector<int>size_type b=234;
size_t b=456;
4. size_t 使用的时候头文件需要 <cstddef> ;size_type 使用的时候需要<string>或者<vector>
5. sizeof(string::size_type)
sizeof(vector<bool>::size_type)
sizeof(vector<char>::size_type)
sizeof(size_t)
上述长度均相等,长度为win32:4 win64:8
6. 二者联系:在用下标访问元素时,vector使用vector::size_type作为下标类型,而数组下标的正确类型则是size_t
注:2:函数find_first_not_of() (C++语言中string类对象的成员函数)功能如下:
注3:short、int和long类型都表示整型值,存储空间的大小不同。一般,short类型为半个机器字长(word)长,int类型为一个机器字长,而long类型为一个或两个机器字长(在32位机器中int类型和long类型通常字长是相同的)。
leetcode:String to Integer (atoi)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- 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. 字符 ...
- Q8:String to Integer (atoi)
8. String to Integer (atoi) 官方的链接:8. String to Integer (atoi) Description : Implement atoi to conver ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- LeetCode 8. String to Integer (atoi) (字符串到整数)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- No.008:String to Integer (atoi)
问题: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
随机推荐
- [百度空间] [原] Empty base class optimization
最近遇到了一个诡异的问题, 数组的数据不对, 最后发现是两个类型的大小不一样导致的. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- 你必须知道的ADO.NET
原文:http://www.cnblogs.com/liuhaorain/archive/2012/02/06/2340409.html 1. 什么是ADO.NET? 简单的讲,ADO.NET是一组允 ...
- ZOJ3550 Big Keng(三分)
题意:给定一个立体的图形,上面是圆柱,下面是圆台,圆柱的底面半径和圆台的上半径相等,然后体积的V时,问这个图形的表面积最小可以是多少.(不算上表面).一开始拿到题以为可以YY出一个结果,就认为它是圆锥 ...
- HDU1542 Atlantis(矩形面积并)
#pragma warning (disable:4996) #include<iostream> #include<cstring> #include<string&g ...
- POJ 3978 Primes(素数筛选法)
题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...
- POJ 2100
Graveyard Design Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 4443 Accepted: 946 ...
- iOS网络检测
使用之前请从Apple网站下载示例:点此下载 Reachability 中定义了3种网络状态: typedef enum : NSInteger { NotReachable = ,//无网络 Rea ...
- POJ 2017
#include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("t ...
- Unix安装BerkeleyDB
下载安装包Berkeley DB 5.3.21.tar.gz http://www.oracle.com/technetwork/products/berkeleydb/downloads/index ...
- hdu 3404 Switch lights 博弈论
主要是求NIM积!!! 代码如下: #include<iostream> #include<cstdio> #include<stack> #include< ...