String to Integer(字符串转换成数字—leetcode)
2016-11-21 22:12
411 查看
题目:
Implement atoi to convert a string to an integer.
注意问题:
1. 判断字符串是否为空字符串;
2. long long 范围:[–9,223,372,036,854,775,808 - 9,223,372,036,854,775,807];
3. 注意:不要漏了要判断+号的情况 ;
4. isdigit--- 测试字符串 *str 是否具有数字特征;
5. 字符串的ASK码减去‘0’的ASK码即为该数字的大小;
6. 字符串从前到后排序得到数字(例如:“123”为:1,1*10+2,(1*10+2)*10+3)。
实现代码:
class Solution
{
public:
int myAtoi(string str)
{
if (str.empty())
{
return 0;
}
// if(*str == '\0') //注意:判断空字符串数组
// {
// return 0;
// }
long long llnum = 0; //long long 范围:[–9,223,372,036,854,775,808 - 9,223,372,036,854,775,807]
int sign = 1;
int index=0;
while (str[index] == ' ')
{
index++;
}
if (str[index] == '-')
{
sign = -1;
index++;
}
else if(str[index] == '+') //注意:不要漏了要判断+号的情况
{
index++;
}
while (isdigit(str[index])) //isdigit--- 测试字符串 *str 是否具有数字特征
{
int i = str[index] - '0'; //ASK码相减得到数字
llnum = llnum*10 + i; //字符串从前到后排序得到数字(例如:“123”为:1,1*10+2,(1*10+2)*10+3);
if (llnum*sign > INT_MAX)
{
llnum = INT_MAX;
break;
}
else if (llnum*sign < INT_MIN)
{
llnum = INT_MIN;
break;
}
index++;
}
return int(sign*llnum);
}
};
Implement atoi to convert a string to an integer.
注意问题:
1. 判断字符串是否为空字符串;
2. long long 范围:[–9,223,372,036,854,775,808 - 9,223,372,036,854,775,807];
3. 注意:不要漏了要判断+号的情况 ;
4. isdigit--- 测试字符串 *str 是否具有数字特征;
5. 字符串的ASK码减去‘0’的ASK码即为该数字的大小;
6. 字符串从前到后排序得到数字(例如:“123”为:1,1*10+2,(1*10+2)*10+3)。
实现代码:
class Solution
{
public:
int myAtoi(string str)
{
if (str.empty())
{
return 0;
}
// if(*str == '\0') //注意:判断空字符串数组
// {
// return 0;
// }
long long llnum = 0; //long long 范围:[–9,223,372,036,854,775,808 - 9,223,372,036,854,775,807]
int sign = 1;
int index=0;
while (str[index] == ' ')
{
index++;
}
if (str[index] == '-')
{
sign = -1;
index++;
}
else if(str[index] == '+') //注意:不要漏了要判断+号的情况
{
index++;
}
while (isdigit(str[index])) //isdigit--- 测试字符串 *str 是否具有数字特征
{
int i = str[index] - '0'; //ASK码相减得到数字
llnum = llnum*10 + i; //字符串从前到后排序得到数字(例如:“123”为:1,1*10+2,(1*10+2)*10+3);
if (llnum*sign > INT_MAX)
{
llnum = INT_MAX;
break;
}
else if (llnum*sign < INT_MIN)
{
llnum = INT_MIN;
break;
}
index++;
}
return int(sign*llnum);
}
};
相关文章推荐
- [LeetCode]—String to Integer (atoi) 字符串转换为数字
- leetcode - 字符串转换成数字(String to Integer)atoi
- LeetCode 8. String to Integer (atoi) C++ --字符串转为数字,包含正负号、空格、字母、数字等字符
- 【LeetCode】8. String to Integer (atoi) 字符串转换为整数
- LeetCode String to Integer (atoi) 字符串转换整数
- leetcode——String to Integer (atoi) 字符串转换为整型数(AC)
- LeetCode--String to Integer (atoi)字符串转数字
- Leetcode 08 String to Integer (atoi)(实现c++atoi即字符串转数字的功能)
- leetcode 8. String to Integer (atoi) 字符串转数字
- LeetCode OJ String to Integer (atoi) 字符串转数字
- LeetCode 8. String to Integer (atoi)(字符串转数字)
- [C++]LeetCode 12: Integer to Roman(将整数转换为罗马数字)
- LeetCode OJ 之 String to Integer (atoi字符串转数字)
- 为下面的函数原型编写函数定义: int ascii_to_integer(char *str); 这个字符串参数必须包含一个或者多个数字,函数应该把这些数字字符转换为整数并返回这个整数。如果字符串参数
- 【LeetCode-面试算法经典-Java实现】【008-String to Integer (atoi) (字符串转成整数)】
- LeetCode 8 String to Integer (atoi)(转换到整型)
- 【LeetCode 8_字符串_实现】String to Integer (atoi)
- 将字符串转换为整数 Convert the string to integer
- 【C语言】为下面的函数原型编写函数定义: int ascii_to_integer(char *str); 这个字符串参数必须包含一个或者多个数字,函数应该把这些数字字符转换为整数并返回这个整数。
- leetCode 8. String to Integer (atoi) (字符串转整形) 解题思路和方法