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.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
这题,想想清楚就好了。可以看看Integer.valueOf()
public class Solution {
public int atoi(String str) {
int i = 0;
boolean negitive = false;
String s = str.trim();
if(s == null ||"".equals(s)){
return 0;
}
if(s.charAt(0)=='-'){
negitive = true;
}
if(s.charAt(0) == '-'||s.charAt(0)=='+'){
s = s.substring(1);
}
i = translate(s, negitive);
if(i>0){
i = negitive?-i:i;
}
return i;
}
public int translate(String str,boolean b){
char[] arrays = str.toCharArray();
int end = 0;
for(int i =0;i<arrays.length;i++){
if(arrays[i]-48 <0||arrays[i]-48>9){
break;
}
if(!b){
if(end > 214748364){
return Integer.MAX_VALUE;
}else if(end == 214748364 && arrays[i]-48>7){
return Integer.MAX_VALUE;
}
}else{
if(end > 214748364){
return Integer.MIN_VALUE;
}else if(end == 214748364 && arrays[i]-48>8){
return Integer.MIN_VALUE;
}
}
end = end *10+(arrays[i]-48);
}
return end;
}
}
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.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
这题,想想清楚就好了。可以看看Integer.valueOf()
public class Solution {
public int atoi(String str) {
int i = 0;
boolean negitive = false;
String s = str.trim();
if(s == null ||"".equals(s)){
return 0;
}
if(s.charAt(0)=='-'){
negitive = true;
}
if(s.charAt(0) == '-'||s.charAt(0)=='+'){
s = s.substring(1);
}
i = translate(s, negitive);
if(i>0){
i = negitive?-i:i;
}
return i;
}
public int translate(String str,boolean b){
char[] arrays = str.toCharArray();
int end = 0;
for(int i =0;i<arrays.length;i++){
if(arrays[i]-48 <0||arrays[i]-48>9){
break;
}
if(!b){
if(end > 214748364){
return Integer.MAX_VALUE;
}else if(end == 214748364 && arrays[i]-48>7){
return Integer.MAX_VALUE;
}
}else{
if(end > 214748364){
return Integer.MIN_VALUE;
}else if(end == 214748364 && arrays[i]-48>8){
return Integer.MIN_VALUE;
}
}
end = end *10+(arrays[i]-48);
}
return end;
}
}
发表评论
-
Merge k Sorted Lists
2015-03-12 19:55 326Merge k sorted linked lists and ... -
Generate Parentheses
2015-03-12 19:50 365Given n pairs of parentheses, w ... -
Generate Parentheses
2015-03-05 22:39 0Given n pairs of parentheses, w ... -
Valid Parentheses
2015-03-05 22:33 308Given a string containing just ... -
Remove Nth Node From End of List
2015-03-05 22:31 340Given a linked list, remove the ... -
Letter Combinations of a Phone Number
2015-03-05 22:30 331Letter Combinations of a Phone ... -
4Sum
2015-03-05 22:26 312Given an array S of n integers, ... -
3Sum Closest
2015-03-05 22:25 292Given an array S of n integers, ... -
3Sum
2015-03-03 22:34 318Given an array S of n integers, ... -
Longest Common Prefix
2015-03-03 22:21 328Write a function to find the lo ... -
Roman to Integer
2015-03-03 22:20 327Given a roman numeral, convert ... -
Integer to Roman
2015-03-01 23:35 289Given an integer, convert it to ... -
Container With Most Water
2015-03-01 22:55 322Given n non-negative integers a ... -
Regular Expression Matching
2015-03-01 20:19 367Implement regular expression ma ... -
Palindrome Number
2015-02-13 22:08 331Determine whether an integer is ... -
Reverse Integer
2015-02-12 23:39 227Reverse digits of an integer. ... -
ZigZag Conversion
2015-02-12 23:37 254The string "PAYPALISHIRING ... -
Longest Palindromic Substring
2015-02-12 22:50 334Given a string S, find the long ... -
Add Two Numbers
2015-02-12 22:12 300You are given two linked lists ... -
Longest Substring Without Repeating Characters
2015-02-11 21:14 425[size=24px;]Longest Substring W ...
相关推荐
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 ...
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字符串转换为整数
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
leetcode 答案 Leetcode题解 leetcode题库的答案及解决思路,随着解题的深入,题解会不断改进时间复杂度和空间复杂度,因此每个题包含多个算法。 目录 Algorithms(算法) ...String to Integer (atoi) Medium com.bc
String to Integer (atoi)][15]][16] [![9. Palindrome Number][17]][18] [![10. Regular Expression Matching][19]][20] [![11. Container With Most Water][21]][22] [![12. Integer to Roman][23]][24] [![13. ...
8.String to Integer (atoi) 9.Palindrome Number 11.Container With Most Water 14.Longest Common Prefix 15.3Sum 16.3Sum Closest 19.Remove Nth Node From End of List 20.Valid Parentheses 21.Merge Two ...
Integer(atoi) :star: :star: :star: 注意细节,溢出 ---- strlen :star: :star: :star: const char,size_t类型 ---- strcpy :star: :star: :star: 字符串复制,返回值,赋值语句 0028 strStr :star: :star: :star:...