Pass in Eclipse but RunTime Error, Is there anyone help me please???


  • 0
    M
     public static String fractionToDecimal(int numerator, int denominator) {
            String res="";
            int temp=numerator/denominator;
            int num=numerator%denominator;
            if(num==0){
            res=temp+"";
            return res;
            }
            StringBuffer sb=new StringBuffer();
            sb.append(temp);
            sb.append('.');
            boolean[] countTable=new boolean[256];
            countTable[num]=true;
            
            while(num>0){
                num=num*10;
                int a=num/denominator;
                int b=num%denominator;
                if(b==0){
                    sb.append(a);
                    res=sb.toString();
                    return res;
                }
                if(countTable[b]==true){
                    sb.append('(');
                    sb.append(a);
                    sb.append(')');
                    res=sb.toString();
                    return res;
                }else{
                    num=b;
                    sb.append(a);
                }
            }
            
            res=sb.toString();
            return res;
            
        }

  • 0
    D

    Your code has many errors.
    Let me explain why there is Run Time error first. In your while loop, there is no way to escape the while loop in many cases, for example 1/6.
    Considering this, u will see that 1/6 = 0.1(6). Hence your code kips adding to StringBuilder and never stops, which causes run time error.

    So u can fix the above bug. But your code also has many other bugs:

    • negative cases
    • num > 256, num < 0 (256 is too small)
    • You never handle overflow on n * 10.
    • overflow on Integer.MIN_VALUE.

    You can reference to the code below:

    public String fractionToDecimal(int numerator1, int denominator1) {
            
            //ignore case where denominator == 0;
            if(numerator1 == 0) return "0";
            StringBuilder builder = new StringBuilder();
            boolean neg = (numerator1 >=0 ) ^ (denominator1 >= 0);
            long numerator = (numerator1 >=0 ? numerator1: -((long)numerator1));
            long denominator = (denominator1>=0? denominator1: -((long)denominator1));
            HashMap<Long, Integer> map = new HashMap<Long, Integer>();
            
            long remainder = numerator % denominator;
            int count = 0;
            
            long temp;
            while(remainder != 0){
                if(!map.containsKey(remainder)){
                    map.put(remainder, count);
                    count ++;
                }else {
                    break;
                }
                temp = remainder * 10;
                builder.append(temp / denominator);
                remainder = temp % denominator;
            }
            
            String result = neg?"-":"";
            result += numerator / denominator;
            if(map.size() > 0) result += ".";
            String s = builder.toString();
            if(remainder == 0) {
                return result + s;
            }
            if(map.get(remainder) > 0) result += s.substring(0, map.get(remainder));
            result += "(" + s.substring(map.get(remainder), count) + ")";
            return result;
        }
    

  • 0
    M

    You are right! Thank you so much!


Log in to reply
 

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