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.
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.
#define INT_MAX (2147483647) #define INT_MIN (-2147483648) class Solution { public: int atoi(const char *str) { char c; int len = strlen(str); int num = 0; bool neg = false; bool start = true; int old; for (int i =0 ; i < len; i++) { c = str[i]; if (start && isspace(c)) continue; if (!start && isspace(c)) break; if (start) { if (c == '-') { neg = true; start =false; continue; } else if (c == '+') { neg = false; start = false; continue; } start = false; } if (c < '0' || c > '9') break; old = num; num = 10 * num + (c - '0'); if ((num - (c - '0'))/ 10 != old) { if (neg) return INT_MIN; else return INT_MAX; } } if (num < 0) { if (neg) return INT_MIN; else return INT_MAX; } if (neg) { return -num; } else return num; } };
相关推荐
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 ...
c c语言_leetcode 0008_atoi.zip
在LeetCode上的"Reverse Integer"题目中,目标是反转一个32位有符号整数的数字。本题考察了基本的数学操作、字符串处理以及边界条件的处理。下面将详细解释这个问题及其解决方案。 首先,我们需要了解32位有符号...
java入门 java_leetcode题解之008_String_to_Integer(atoi)
c语言入门 C语言_leetcode题解之08-string-to-integer-atoi.c
LeetCode Roman to Integer解决方案
js js_leetcode题解之8-string-to-integer-(atoi).js
java java_leetcode题解之Flip String to Monotone Increasing.java
* 字符串到整数(atoi)(String to Integer (atoi)):将字符串转换为整数。 2. 字符串操作: * 最长的回文子串(Longest Palindromic Substring):找到字符串中最长的回文子串。 * ZigZag转换(ZigZag ...
LeetCode_To_GitHub Python 工具,用于从 LeetCode () 中搜索您的解决方案并自动将它们传输到您的 GitHub 帐户。 支持的操作 到目前为止,该工具仅支持一种登录类型,即 Google 登录。 此外,您只能自动下载“算法”...
* [String](https://github.com/kamyu104/LeetCode#string) * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue]...
Reverse words in a string-leetcode
在LeetCode平台上,字符串(String)专题是编程爱好者和求职者经常练习的重要部分。这个专题涵盖了字符串处理的各种算法问题,旨在提升编程者对字符串操作的熟练度和理解。在这个压缩包中,"LeetCode-String-master"很...
java java_leetcode题解之Magical String.java
python python_leetcode题解之Binary String With Substrings Representing
c语言入门 c语言_leetcode题解perform-string-shifts.c
java java_leetcode题解之Construct String from Binary Tree.java
* String to Integer (atoi):该题目要求将字符串转换成整数,实现方法使用了状态机算法。 * Merge Sorted Array:该题目要求合并两个排序数组,实现方法使用了迭代算法。 * Valid Parentheses:该题目要求检查括号...
vs code LeetCode 插件
Integer(atoi) 字符串转整数 string 13 Roman to Integer 罗马数字转整数 number,string 14 Longest Common Prefix 最长公共前缀 string 16 3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted ...