`
jgsj
  • 浏览: 965250 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

String to Integer (atoi)

Implementatoito 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.

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.

好长的注意地方,因为要考虑的东西实在也挺多的。总结如下:

1 前面空格分隔符号的时候

2 第一个符号位处理+ -

3 遇到非数字字符退出

4 为正数的时候,大于INT_MAX上溢

5 为负数的时候, 小于INT_MIN下溢

6 为空字符串或者空指针的时候

考虑完了就好写程序了:

class Solution {
public:
	int atoi(const char *str) 
	{
		if (str == nullptr) return 0;
		//注意:判断空字符串数组
		if(*str == '\0') return 0;
		long long llnum = 0;
		int sign = 1;

		while (*str == ' ')
		{
			str++;
		}
		if (*str == '-')
		{
			sign = -1;
			str++;
		}
		//注意:不要溜了要判断+号的情况
		else if(*str == '+')
		{
			str++;
		}
		while (isdigit(*str))
		{
			int i = *str - '0';
			llnum = llnum*10 + i;
			if (llnum*sign > INT_MAX)
			{
				llnum = INT_MAX;
				break;
			}
			else if (llnum*sign < INT_MIN)
			{
				llnum = INT_MIN;
				break;
			}
			str++;
		}
		return int(sign*llnum);
	}
};


2014-1-24 update

简洁一点的程序,思路一样,这题没多少变化:

int atoi(const char *str) 
	{
		long long rs = 0;
		while (isspace(*str)) str++;
		
		int sign = 1;
		if (*str == '-')
		{
			sign = -1;
			str++;
		}
		else if (*str == '+') str++;
		
		for ( ; isdigit(*str); str++)
		{
			rs = rs*10 + (*str-'0');
			if (sign == 1 && rs >= INT_MAX) return INT_MAX;
			else if (rs*sign < INT_MIN) return INT_MIN;
		}
		return rs*sign;
	}




分享到:
评论

相关推荐

    leetcode-String-to-Integer-atoi

    leetcode字符串转换为整数

    leetcode跳跃-LeCode:乐科

    字符串转换整数 (atoi) 9. Palindrome Number 回文数 10. Regular Expression Matching 正则表达式匹配 11. Container With Most Water 盛最多水的容器 12. Integer to Roman 整数转罗马数字 13. Roman to Integer ...

    leetcode中文版-LeetCode:LeetcodeC++/Java

    字符串转整数 string 13 Roman to Integer 罗马数字转整数 number,string 14 Longest Common Prefix 最长公共前缀 string 16 3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted Lists 合并两个...

    leetcode小白刷题-String-to-Integer-atoi-:来自https://leetcode.com/problems/st

    leetcode小白刷题字符串到整数 (atoi) 示例 1: Input: " 42 " Output: 42 示例 2: Input: " -42 " Output: - 42 Explanation: The first non-whitespace character is ' - ' , which is the minus sign. Then take...

    javalruleetcode-lxxcode:Leetcode或Lintcode的代码

    3:无重复字符的最长子串(java、javascript) leetcode 4:两个排序数组的中位数(java,javascript) leetcode 5: 最长回文子串(java, javascript) leetcode 6: ZigZag 转换(java, javascript) leetcode 7: 反转...

    lrucacheleetcode-oh-my-leetcode:Leetcode题解

    lru缓存leetcode 哦,我的 ...将字符串转换为整数。 **第 0004 题:**反转位 反转给定 32 位无符号整数的位。 例如,给定输入 43261596(以二进制表示为 00000010100101000001111010011100),返回 96417

    leetcode答案-LeetCode:重做一遍LeetCode

    6之字形转换](字符串)(中)20180202眩 7.【倒整数】 20181030完成 8.[String to Integer(atoi)](中)这个问题懒得想了 9.【回文数】(易) 20181030完成 10.【正则表达式匹配】(难)不理解 11.【盛水的容器】(中...

    LeetCodeJSSolutions

    #8字符串到整数(atoi)解决方案: 运行时:76毫秒,快于将String转换为Integer(atoi)JavaScript在线提交的99.95%。 内存使用量:40 MB,少于JavaScript在线提交的String to Integer(atoi)的88.17%。 相关...

    leetcode算法题主函数如何写-kisure-notes:亲笔笔记

    string-to-integer-atoi.js 字符串转换整数 │ ├── three-number-sum.js 三数之和 │ ├── two-number-sum.js 两数之间的和 │ ├── 2020-5-27.js 大小为 K 且平均值大于等于阈值的子数组数目 │ ├── 2020...

    javalruleetcode-leetcode-solutions-java:leetcode-解决方案-java

    无重复字符最长子串.java) 4 [Java](/Java/004 两个有序数组的中位数.java) 5 [Java](/Java/005 最长回文子串.java) 6 [Java](/Java/006 ZigZag Conversion.java) 7 [Java](/Java/007 反向整数.java) 8 [Java](/Java...

Global site tag (gtag.js) - Google Analytics