Hard 65题 Valid Number

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.

Solution:

琢磨了调试了半天。。。。。。。。

public class Solution {
	public static boolean isNumber(String s) {
        String t=s.trim();
        if(t==null||t.length()==0)
        	return false;
        if(t.length()==1)
        {
        	if(t.charAt(0)<='9'&&t.charAt(0)>='0')
        		return true;
        	else
        		return false;
        }
        if(isIntegerPos(t)||isIntegerNegOrPos(t)||isDecimal(t)||isHex(t))
            return true;
        return false;
    }
    public static boolean isNull(String t)
    {
        if(t==null||t.length()==0)
    		return true;
    	return false;
    }
    public static boolean isIntegerPos(String t)
    {
        if(t==null||t.length()==0)
    		return false;
    		
        for(int i=0; i<=t.length()-1;i++)
        {
            if(t.charAt(i)>'9'||t.charAt(i)<'0')
                return false;
        }
        return true;
        
    }
    public static boolean isIntegerNegOrPos(String t)
    {
        if(t==null||t.length()==0)
    		return false;
    	if((t.charAt(0)=='-')||(t.charAt(0)=='+'))
    	{
            for(int i=1; i<=t.length()-1;i++)
            {
                if(t.charAt(i)>'9'||t.charAt(i)<'0')
                    return false;
            }
            return true;
    	}
    	return false;
        
    }
    public static boolean isDecimal(String t)
    {
        if(t==null||t.length()==0)
    		return false;
    	if(t.charAt(0)=='.')
    	{
    		return isIntegerPos(t.substring(1));
    	}
    	if(isIntegerPos(t.substring(0,t.length()-1))||isIntegerNegOrPos(t.substring(0,t.length()-1)))
    	{
    	    if(t.substring(0,t.length()-1).length()==1&&(t.substring(0,t.length()-1).equals("+")||t.substring(0,t.length()-1).equals("-")))//+.
    			return false;
    		return t.charAt(t.length()-1)=='.';
    	}
    	
        String[] split=t.split("\\.");
        if(split.length!=2){
            return false;
        }
        if(t.substring(t.length()-1,t.length()).equals("."))//75.0.
        	return false;
        if(split[1].length()==1&&(split[1].charAt(0)=='+'||split[1].charAt(0)=='-'))//58.-
            return false;   
        if((isIntegerPos(split[0])||isIntegerNegOrPos(split[0])||isNull(split[0]))&&(isIntegerPos(split[1])||isNull(split[1])))
            return true;
        return false;

    }
    public static boolean isHex(String t)
    {
        if(t==null||t.length()==0)
    		return false;
        String[] split=t.split("e");

        if(split.length!=2)
            return false;
        if(split[0].length()==1&&(split[0].charAt(0)=='+'||split[0].charAt(0)=='-'))//-e58
            return false;
        if(split[1].length()==1&&(split[1].charAt(0)=='+'||split[1].charAt(0)=='-'))//58e-
            return false;   
        if(t.substring(t.length()-1,t.length()).equals("e")) //"7e69e"
        	return false;
        if((split[1].length()==1&&(split[1].charAt(0)>'9'||split[1].charAt(0)<'0'))) //4e+
        	return false;
        if((isIntegerPos(split[0])||isIntegerNegOrPos(split[0])||isDecimal(split[0]))&&(isIntegerPos(split[1])||isIntegerNegOrPos(split[1])||isNull(split[1])))
            return true;
        return false;  
    }
}


可以用自动机做。。。

你可能感兴趣的:(leetcode,hard)