Valid Number
Analysis:
Leetcode上面90个赞,785个踩,和String to integer是难兄难弟。考到的几率很小。
Solution:
class Solution { public boolean isNumber(String s) { int i = 0, n = s.length(); boolean isNumber = false; // start space while (i < n && s.charAt(i) == ' ') { i++; } if (i < n && (s.charAt(i) == '-' || s.charAt(i) == '+')) { i++; } while (i < n && Character.isDigit(s.charAt(i))) { i++; isNumber = true; } if (i < n && s.charAt(i) == '.') { i++; while (i < n && Character.isDigit(s.charAt(i))) { i++; isNumber = true; } } if (isNumber && i< n && s.charAt(i) == 'e') { i++; isNumber = false; if( i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++; while (i < n && Character.isDigit(s.charAt(i))) { i++; isNumber = true; } } while (i < n && s.charAt(i) == ' ') { i++; } return isNumber && i == n; } }
Leetcode上面90个赞,785个踩,和String to integer是难兄难弟。考到的几率很小。
Solution:
class Solution { public boolean isNumber(String s) { int i = 0, n = s.length(); boolean isNumber = false; // start space while (i < n && s.charAt(i) == ' ') { i++; } if (i < n && (s.charAt(i) == '-' || s.charAt(i) == '+')) { i++; } while (i < n && Character.isDigit(s.charAt(i))) { i++; isNumber = true; } if (i < n && s.charAt(i) == '.') { i++; while (i < n && Character.isDigit(s.charAt(i))) { i++; isNumber = true; } } if (isNumber && i< n && s.charAt(i) == 'e') { i++; isNumber = false; if( i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++; while (i < n && Character.isDigit(s.charAt(i))) { i++; isNumber = true; } } while (i < n && s.charAt(i) == ' ') { i++; } return isNumber && i == n; } }
评论
发表评论