Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public int myAtoi(String str) {
- if ("".equals(str)) {return 0;}
- int i = 0, sign = 1, length = str.length();
- double base = 0;
- while (str.charAt(i) == ' ') {i++;}
- if (str.charAt(i) == '-' || str.charAt(i) == '+') {
- sign = str.charAt(i++) == '-' ? -1 : 1;
- }
- while (i < length && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
- if ((base = base * 10 + (str.charAt(i++) - '0')) > Integer.MAX_VALUE) {
- return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
- }
- }
- return sign * (int)base;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement