700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > LeetCode实战:字符串转换整数 (atoi)

LeetCode实战:字符串转换整数 (atoi)

时间:2019-04-07 11:01:55

相关推荐

LeetCode实战:字符串转换整数 (atoi)

题目英文

Implement atoi which converts a string to an integer.

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.

Note:

Only the space character ’ ’ is considered as whitespace character.Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range:[−2^31, 2^31− 1]. If the numerical value is out of the range of representable values,INT_MAX (2^31 − 1)orINT_MIN (−2^31)is returned.

Example 1:

Input: "42"Output: 42

Example 2:

Input: " -42"Output: -42Explanation: The first non-whitespace character is '-', which is the minus sign.Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"Output: 4193Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: "words and 987"Output: 0Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: "-91283472332"Output: -2147483648Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.Thefore INT_MIN (−231) is returned.

题目中文

请你来实现一个atoi函数,使其能将字符串转换成整数。

首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。

当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。

该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。

注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。

在任何情况下,若函数不能进行有效的转换时,请返回 0。

说明:

假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为[−2^31, 2^31− 1]。如果数值超过这个范围,请返回INT_MAX (2^31− 1)INT_MIN (−2^31)

示例 1:

输入: "42"输出: 42

示例 2:

输入: " -42"输出: -42解释: 第一个非空白字符为 '-', 它是一个负号。我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。

示例 3:

输入: "4193 with words"输出: 4193解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。

示例 4:

输入: "words and 987"输出: 0解释: 第一个非空字符是 'w', 但它不是数字或正、负号。因此无法执行有效的转换。

示例 5:

输入: "-91283472332"输出: -2147483648解释: 数字 "-91283472332" 超过 32 位有符号整数范围。 因此返回 INT_MIN (−231) 。

示例 6:

输入: " 0000000000012345678"输出: 12345678

示例 7:

输入: "20000000000000000000"输出: 2147483647

算法实现

public class Solution {public int MyAtoi(string str) {str = str.Trim();if (string.IsNullOrEmpty(str))return 0;if (str[0] != '-' && str[0] != '+'){if (str[0] < '0' || str[0] > '9')return 0;}int negative = 1;long result = 0;Queue<int> q = new Queue<int>();for (int i = 0; i < str.Length; i++){if (str[i] == '-' && i == 0){negative = -1;continue;}if (str[i] == '+' && i == 0){continue;}if (str[i] < '0' || str[i] > '9'){break;}q.Enqueue(str[i] - '0');}while (q.Count != 0){int i = q.Dequeue();//去掉队列前端的零if (i == 0 && result == 0)continue;// 返回超过位数的数字if (negative == 1 && q.Count > 10){return int.MaxValue;}if (negative == -1 && q.Count > 10){return int.MinValue;}result += i * (long)Math.Pow(10, q.Count);if (negative == 1 && result > int.MaxValue){return int.MaxValue;}if (negative == -1 && result * -1 < int.MinValue){return int.MinValue;}}return (int)result * negative; }}

实验结果

状态:通过1079 / 1079 个通过测试用例执行用时: 104 ms, 在所有 C# 提交中击败了 98.32% 的用户内存消耗: 24.3 MB, 在所有 C# 提交中击败了 24.45% 的用户

相关图文

1. “数组”类算法

LeetCode实战:三数之和LeetCode实战:求众数LeetCode实战:缺失的第一个正数LeetCode实战:快乐数LeetCode实战:寻找两个有序数组的中位数

2. “链表”类算法

LeetCode实战:两数相加LeetCode实战:删除链表的倒数第N个节点LeetCode实战:合并两个有序链表LeetCode实战:合并K个排序链表LeetCode实战:两两交换链表中的节点LeetCode实战:旋转链表LeetCode实战:环形链表

3. “栈”类算法

LeetCode实战:有效的括号LeetCode实战:最长有效括号LeetCode实战:逆波兰表达式求值

4. “队列”类算法

LeetCode实战:设计循环双端队列LeetCode实战:滑动窗口最大值

5. “递归”类算法

LeetCode实战:爬楼梯

6. “字符串”类算法

LeetCode实战:反转字符串LeetCode实战:翻转字符串里的单词

7. “树”类算法

LeetCode实战:相同的树LeetCode实战:对称二叉树LeetCode实战:二叉树的最大深度LeetCode实战:将有序数组转换为二叉搜索树

8. “哈希”类算法

LeetCode实战:两数之和

9. “搜索”类算法

LeetCode实战:搜索二维矩阵

10. “动态规划”类算法

LeetCode实战:最长回文子串

11. “数值分析”类算法

LeetCode实战:x 的平方根

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。