leetCode 8. String to Integer (atoi) 字符串

8. String to Integer (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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

题目大意:该题目是说将string类型的字符串转换成整型数据,类似于C++库里的atoi函数,解决该题目的关键在于两个方面:

(1)字符串格式的合法判断

(2)转换结果的溢出判断

首先,对于字符串格式,空格不计入计算,应从第一个非空字符开始判断,首字母只能是符号(+、-)与数字的一种;从计算开始遍历字符串,到最后一位数字为止;

其次,对于转换结果,我们知道整型数据的范围是INT_MIN(-2147482648)到INT_MAX(2147483647),超出范围则返回最大与最小值。所以我们可以开始用long long类型的变量存储结果;

代码如下:

class Solution {
public:
    int myAtoi(string str) {
    	if (str.empty())
    		return 0;
    	int flag = 1;//flag 1正 -1负
    	long long result = 0;
    
    	int i = 0;
    	while (str[i] == ‘ ‘)
    	{
    		i++;
    	}
    
    	if (str[i] == ‘-‘)
    	{
    		i++;
    		flag = -1;
    
    	}
    	else if (str[i] == ‘+‘)
    	{
    		i++;
    	}
    
    	for (int j = i; j < str.length(); j++)
    	{
    		if (str[j] >= ‘0‘ && str[j] <= ‘9‘)
    		{
    			result = result * 10 + (str.at(j) - ‘0‘);
    			if (result > 2147483647)
    			{
    				if (flag == 1)
    					result = INT_MAX;
    				else
    				{
    					result = INT_MIN;
    					flag = 1;
    				}
    				break;
    			}
    		}
    		else
    			break;
    	}
    	return flag * result;
    }
};

2016-08-09 00:18:49

时间: 2024-10-12 18:09:49

leetCode 8. String to Integer (atoi) 字符串的相关文章

[LeetCode]41. String to Integer(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 spe

LeetCode OJ String to Integer (atoi) 字符串转数字

1 #include <iostream> 2 #include <assert.h> 3 using namespace std; 4 int ato(const char *str) { 5 int i=0,e=0,s=0; 6 int max=2147483647,min=-2147483648; 7 int f=1; 8 int tem[10]={0}; 9 unsigned int pan=0; 10 while(*str==' '){ //过滤掉连续空格 11 str+

LeetCode中String to Integer (atoi) 字符串转换成整数

int atoi(const char *str) { long long result = 0; while (*str==' ')str++; int flag=0; if (*str == '-' || *str == '+') { flag = (*str == '-') ? -1 : 1; str++; } while (*str!='\0') { int temp = *str - '0'; if (temp >= 0 && temp <= 9) { result

leetcode——String to Integer (atoi) 字符串转换为整型数(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

LeetCode 008 String to Integer (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 b

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,

【leetcode】String to Integer (atoi)

String to Integer (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 f

8. String to Integer (atoi) 字符串转为int类型的所有可能情况

8. String to Integer (atoi) 问题: 输入一个字符串,将字符串转为int类型,处理所有可能的输入情况. 可能的输入情况: 1.字符串为空.即"". 2.首先是假设输入的字符串都是数字型的,可含正负号,例如12345,+12548,-15568. 3.字符串中含有非数字的其他字符类型,例如a,-sd,-1286dgg558,000822fg55. 4.字符串首尾含有空格的,例如 -558dg12, 12dc12 . 5.针对转换后的数字越界,有两种情况,一个是超