【小熊刷题】valid number - 各种edge cases真是醉了

这题让我想起了当年学automata的时候,需要复习一下!

Question

Validate if a given string is numeric.

Some examples:
“0” => true
” 0.1 ” => true
“abc” => false
“1 a” => false
“2e10” => true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

*Difficulty: Hard(coz hard to get all the cases once), Frequency: Low

https://leetcode.com/problems/valid-number/

My Solution

/**
 * Created by hui on 8/10/15.
 * forgot sth like "." "e" "..2" "6+1"
 */
public class ValidNum {
    public boolean isNumber(String s) {
        s = s.toLowerCase().trim();
        if(s.isEmpty()) return false;
        boolean ifPosNeg = false;
        boolean dot = false;
        boolean exp = false;
        boolean hasDigit = false;
        for(int i = 0; i < s.length();i++){
            char ch = s.charAt(i);
            if(!Character.isDigit(ch)){
                if(ch == '-' || ch=='+'){
                    if((ifPosNeg && !(i>0 && s.charAt(i-1)=='e')) || i == s.length()-1) return false;
                    else if(i>0 && s.charAt(i-1)=='e') continue;
                    else if(hasDigit || dot) return false;
                    else ifPosNeg = true;
                }
                else if(ch == 'e'){
                    if(exp || i==s.length()-1) return false;
                    else if(!hasDigit) return false;
                    else{
                        exp = true;
                        dot = true; //later digit should not have dot
                    }
                }
                else if(ch=='.'){
                    if(dot || (!hasDigit && i==s.length()-1)) return false;//.5 should be good
                    else dot = true;
                }
                else return false;
            }else hasDigit = true;
        }
        return true;
    }

    public static void main(String[] args){
        ValidNum v = new ValidNum();
        String s = " 005047e+6";
        boolean r = v.isNumber(s);
        System.out.println("s:"+s+" r:"+r);
        s = " 1+6";
        r = v.isNumber(s);
        System.out.println("s:"+s+" r:"+r);
        s = " -.7e+0435";
        r = v.isNumber(s);
        System.out.println("s:"+s+" r:"+r);
    }
}

Standard Solution

public boolean isNumber(String s) {

 int i = 0, n = s.length();

 while (i < n && Character.isWhitespace(s.charAt(i))) i++;

 if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++;

 boolean isNumeric = false;

 while (i < n && Character.isDigit(s.charAt(i))) {

 i++;

 isNumeric = true;

 }

 if (i < n && s.charAt(i) == '.') {

 i++;

 while (i < n && Character.isDigit(s.charAt(i))) {

 i++;

 isNumeric = true;

 }

 }

 if (isNumeric && i < n && s.charAt(i) == 'e') {

 i++;

 isNumeric = false;

 if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) i++;

 while (i < n && Character.isDigit(s.charAt(i))) {

 i++;

 isNumeric = true;

 }

 }

 while (i < n && Character.isWhitespace(s.charAt(i))) i++;

 return isNumeric && i == n;

}

【先copy书上的答案过来,明天起来继续研究~】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值