My ugly java approach ^_^


  • 0
    T
    
    public class Solution
    {
    
        public boolean isNumber(String s)
        {
            int numbers = 0;
            int letters = 0;
            int e = 0;
            int period = 0;
            int middle = 0;
            int period_on_left = 0;
            int period_on_right = 0;
            int space = 0;
            int space_middle = 0;
            int fist_is_space = 0;
    
            // preprocess string
            for (int i = 0; i < s.length(); i++)
            {
                int current = (int) s.charAt(i);
    
                // check if its a number
                if (current >= 48 && current <= 57)
                {
                    System.out.println("num++");
                    numbers++;
    
                }
    
                // check if its a space
                if (current == 32)
                {
                    //case:" 74" == ture
                    if (i == 0)
                    {
                        fist_is_space++;
                    }
                    space++;
                    if (i >= 1 && i < s.length()-1) //case: ". 1" == false
                    {
                        space_middle++;
                    }
                }
    
                // check if its a letter
                if ((current >= 65 && current <= 90) ||
                        (current >= 97 && current <= 122) )
                {
                    letters++;
    
                    // check if its a letter e
                    if (current == 101) e++;
                }
    
                // check if its a period
                if (current == 46)
                {
                    period++;
    
                    // check if period followed by number
                    if(i < s.length() - 1)
                    {
                        int next = (int) s.charAt(i+1);
                        // check if period followed by a number
                        if (next >= 48 && next <= 57)
                        {
                            period_on_left++;
                        }
                    }
                
                    // check if number followed by a period
                    if(i >= 1)
                    {
                        int previous = (int) s.charAt(i-1);
                        // check if period followed by a number
                        if (previous >= 48 && previous <= 57)
                        {
                            period_on_right++;
                        }
                    }
                }
    
                // check if its middle
                if (e == 1 || period == 1)
                {
                    if (i >= 1 && i < s.length()-1)
                    {
                        int previous = (int) s.charAt(i-1);
                        int next = (int) s.charAt(i+1);
    
                        // confirm that prevvious and next are numbers
                        if ((previous >= 48 && previous <= 57) &&
                                (next >= 48 && next <= 57) )
                        {
                            middle++;
                        }
                    }
                }
            }
    
            //case: "G76" == false
                if(letters >=1 && e<=0)
                {
                    return false;
                }
            // postprocess and deduce if string is a number
            if (numbers >= 2 && (period == 1 || e == 1) && middle == 1 && space <= 0)
            {
                //case:"1 4" == false
                return true;
            }
            else if (numbers >= 1 && period <= 1 && e < 1 && space_middle <= 0 && space <= 0)  
            {
                return true;
            }
            else if (numbers == 1 && letters <= 0 && period <= 0 && space_middle <=0)
            {
                return true;
            }
            else if (numbers >= 1 && period == 1 && (period_on_left == 1 || period_on_right == 1) && space_middle <= 0 && e < 1)
            {   //case: ". 1" == false
                //case:" 74" == ture
                return true;
            }
            else if (numbers >= 1 && period < 1 && e < 1 && space >= 2)
            {
                // case: "1  " == ture
                return true;
            }
            else if (letters >= 2 || period >= 2 || e >= 2)
            {
                return false;
            }
            else if (numbers == 0)
            {
                return false;
            }
            else if(fist_is_space == 1 && e <=0 || (numbers >=2 && space == 1 && space_middle <=0))
            {
                //case:" 74" == ture
                //case: " 9e" == false
                //case:  "00 " == true ?
                //case:  "1 4" == false ?
                
                return true;
            }
            else 
            {
                return false;
            }
        }
    }
    
    
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.