新博文地址:[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 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.
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.
这道题最大的问题就是问题描述不清晰:
希望德国一会儿献上更精彩的比赛。
首先将str开头的空格剪掉,遇到第一个非空格字符之后再处理。
1. 无论什么时候遇到非数字和非符号,处理结束
2. 遇到符号,判断之前有没有遇到数字,如果之前遇到过数字,则退出循环。如果没有遇到数字,再进行这样的判断:
2.1 如果之前还遇到过符号,这是第二次遇到符号,退出循环。
2.2 如果之前没遇到过符号,这是第一次遇到符号,记录该符号。
3. 遇到数字则判断是否会溢出,溢出则做相应处理,否则就记录该数字。
代码如下:
if(str == null || str.length() == 0 || str.trim().length() == 0){ return 0; } int begin = 0; for(begin = 0 ; begin < str.length(); begin++){ if(str.charAt(begin) != ' '){ break; } } str = str.substring(begin); Set<Character> letterSet = new HashSet<Character>(Arrays.asList('0','1','2','3','4','5','6','7','8','9')); Set<Character> signSet = new HashSet<Character>(Arrays.asList('+','-')); boolean hasSign = false; boolean hasNum = false; char[] charArray = str.toCharArray(); StringBuilder sb = new StringBuilder(); for(int i = 0; i < str.length(); i++){ if(!letterSet.contains(charArray[i]) && !signSet.contains(charArray[i])){ break; }else if(letterSet.contains(charArray[i])){ int rightNow = 0; if(hasNum){ rightNow = Integer.valueOf(sb.toString()); } if(rightNow > Integer.MAX_VALUE / 10 || (rightNow == Integer.MAX_VALUE / 10 && (charArray[i] - '0') >= Integer.MAX_VALUE % 10 )){ return Integer.MAX_VALUE; }else if(rightNow < Integer.MIN_VALUE / 10 || (rightNow == Integer.MIN_VALUE / 10 && (charArray[i] - '0') >= -1 * (Integer.MIN_VALUE % 10) )){ return Integer.MIN_VALUE; }else{ sb.append(charArray[i]); hasNum = true; } }else if(signSet.contains(charArray[i])){ if(hasSign) break; else{ if(hasNum) break; hasSign = true; sb.append(charArray[i]); } } } if(!hasNum){ return 0; }else{ if(signSet.contains(sb.charAt(0))){ return sb.charAt(0) == '+' ? Integer.valueOf(sb.substring(1)): -1 * Integer.valueOf(sb.substring(1)); }else{ return Integer.valueOf(sb.toString()); } }
相关推荐
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 ...
java入门 java_leetcode题解之008_String_to_Integer(atoi)
c语言入门 C语言_leetcode题解之08-string-to-integer-atoi.c
js js_leetcode题解之8-string-to-integer-(atoi).js
在LeetCode平台上,"String to Integer (atoi)"是一道经典的编程题目,主要考察开发者对于字符串处理和数值转换的理解。此题目的目标是实现一个函数,该函数能够将一个字符串转换成一个整数,遵循C语言的`atoi`函数...
* 字符串到整数(atoi)(String to Integer (atoi)):将字符串转换为整数。 2. 字符串操作: * 最长的回文子串(Longest Palindromic Substring):找到字符串中最长的回文子串。 * ZigZag转换(ZigZag ...
leetcode 2sum # Programming-Problems This will have many problems from all over the web, As of ...LeetCode: ...Integer ...String To Integer Atoi [Medium] LC9: Palindrome Number [Easy] LC11:
Integer(atoi) 字符串转整数 string 13 Roman to Integer 罗马数字转整数 number,string 14 Longest Common Prefix 最长公共前缀 string 16 3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted ...
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...
String to Integer (atoi) 中等 9 Palindrome Number 简单 11 Container With Most Water 中等 12 Integer to Roman 中等 13 Roman to Integer 简单 14 Longest Common Prefix 简单 15 3Sum 中等 16 3Sum Closest ...
leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...
LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....
String to Integer (atoi) 009 Palindrome Number 010 Regular Expression Matching 011 Container With Most Water 012 Integer to Roman 013 Roman to Integer 014 Longest Common Prefix 015 3Sum 016 3Sum ...
String to Integer (atoi) addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方图的最大面积,左右两次扫面+剪枝优化 Valid Parentheses 用栈判断括号匹配 Regular ...
leetcode 分类 Leetcode 总结 (updating) # Title Solution 1 Two ...用unordered_map,降至O(n) ...一个变量存储进位,当l1,l2,进位非均为空时,继续计算 ...注意要保证n1长度小于n2,因为...String to Integer (atoi) 9 Palind
* [String](https://github.com/kamyu104/LeetCode#string) * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue]...
String to Integer (atoi) 18.5% 中等 9 Palindrome Number 56.7% 简单 10 Regular Expression Matching 25.3% 困难 11 Container With Most Water 59.3% 中等 12 Integer to Roman 61.8% 中等 13 Roman to In
8. String to Integer (atoi):将字符串转换成整数,考虑溢出等问题。 9. Valid Number:判断一个字符串是否是有效的数字表示。 10. Longest Substring Without Repeating Characters:找出不含有重复字符的最长子串...
String to Integer (atoi) Easy -> Medium 19 Remove Nth Node From End of List Easy -> Medium 33 Search in Rotated Sorted Array Hard -> Medium 35 Search Insert Position Medium -> Easy 36
* String to Integer (atoi):该题目要求将字符串转换成整数,实现方法使用了状态机算法。 * Merge Sorted Array:该题目要求合并两个排序数组,实现方法使用了迭代算法。 * Valid Parentheses:该题目要求检查括号...