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.
public class Solution { public int myAtoi(String str) { if (str == null || str.equals("")) { return 0; } str = str.trim(); int i = 0; boolean posi = true; if (str.charAt(0) == '-') { posi = false; i++; } else if (str.charAt(0) == '+') { posi = true; i++; } int res = 0; int cur = 0; for (; i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9'; i++) { cur = str.charAt(i) - '0'; if(Integer.MAX_VALUE/10 < res || (Integer.MAX_VALUE/10 == res && Integer.MAX_VALUE%10 < cur)) { return posi ? Integer.MAX_VALUE : Integer.MIN_VALUE; } res = res * 10 + cur; } return posi ? res : -res; } }
相关推荐
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 ...
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 2sum # Programming-Problems This will have many problems from all over the web, As of writing this ...Integer ...String To Integer Atoi [Medium] LC9: Palindrome Number [Easy] LC11:
在LeetCode平台上,"String to Integer (atoi)"是一道经典的编程题目,主要考察开发者对于字符串处理和数值转换的理解。此题目的目标是实现一个函数,该函数能够将一个字符串转换成一个整数,遵循C语言的`atoi`函数...
`atoi`(ASCII to Integer)函数的功能是从一个字符串中提取整数值。它会读取字符串中的字符直到遇到非数字字符为止,并将这些数字字符组成的序列转换成对应的整数值返回。如果字符串的第一个字符不是数字或者字符串...
19 String to Integer (atoi) 59 20 Merge Sorted Array 61 ... ... 231 Counting Bits 561 232 Maximum Product of Word Lengths 563 233 Gray Code 565 234 Permutations 567 235 Permutations II 571 236 ...
(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 as many numerical digits as ...
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) 中等 字串 麻烦 Palindrome Number 简单 字串 Container With Most Water 中等 动态规划 重要 Integer to Roman 中等 重要 Roman to Integer 简单 重要 Longest Common Prefix 简单 字串 ...
Integer(atoi) 字符串转整数 string 13 Roman to Integer 罗马数字转整数 number,string 14 Longest Common Prefix 最长公共前缀 string 16 3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted ...
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 ...
8.String to Integer (atoi) 9.Palindrome Number 10.Regular Expression Matching 11.Container With Most Water 12.Integer to Roman 13.Roman to Integer 14.Longest Common Prefix (Trie树待完成) 15.3Sum 16.3...
String to Integer (atoi) 字符串转换整数 (atoi) 9. Palindrome Number 回文数 10. Regular Expression Matching 正则表达式匹配 11. Container With Most Water 盛最多水的容器 12. Integer to Roman 整数转罗马...
String to Integer (atoi) addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方图的最大面积,左右两次扫面+剪枝优化 Valid Parentheses 用栈判断括号匹配 Regular ...
内存使用量:40 MB,少于JavaScript在线提交的String to Integer(atoi)的88.17%。 相关材料: #1710卡车解决方案上的最大单位: 运行时间:124毫秒,快于JavaScript在线提交的卡车上最大单位数的30.46%。 ...
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
leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 ...String to Integer (atoi) 15.5% Medium 0009 Palindrome Number 49.4% Easy