LeeCode String to Integer (atoi)
2017-08-29 17:24
274 查看
class Solution { public int myAtoi(String str) { str = str.trim(); if(str == null || str.length() == 0) return 0; int length = str.length(); int isNegative =1; int i = 0; long result = 0; if(str.charAt(0) == '+') i++; else if(str.charAt(0) == '-'){ isNegative = -1; i++; } for(; i<length ; i++){ if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){ result = result * 10 + str.charAt(i) - '0'; if(result > Integer.MAX_VALUE){ return isNegative < 0 ? Integer.MIN_VALUE : Integer.MAX_VALUE; } }else{ break; } } return (int)result * isNegative; } }
相关文章推荐
- Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]
- (LEEDCODE)String to Integer (atoi) 字符串转整型
- 【Leet Code】String to Integer (atoi) ——常考类型题
- [leedcode oj 8]String to Integer (atoi)
- LeedCode String to Integer (atoi)
- CODE 122: String to Integer (atoi)
- [LeedCode OJ]#8 String to Integer (atoi)
- Leetccode String to Integer (atoi) 字符串转数字
- Leet Code 8 String to Integer (atoi)
- leetocde-8. String to Integer (atoi)
- 【Leet Code】String to Integer (atoi) ——常考类型题
- Leecode 8: String to Integer (atio)
- (letcode)String to Integer (atoi)
- LeedCode OJ -- String to Integer (atoi)
- Leetcode: String to Integer (atoi)
- [LeetCode]--8. String to Integer (atoi)
- LeetCode OJ String to Integer (atoi)
- Leetcode 8. String to Integer (atoi) The Solution of Python
- leetcode python - String to Integer (atoi)
- LeetCode String to Integer (atoi)