Accepted Clean Java Solution


  • 34
    C

    I don't understand why so many people tends to write "short" java solutions over "clear" java solution when performance stays the same.
    In order to be a good teammate, one should always write clean code instead of hacky code if performance stays the same.

      public String fractionToDecimal(int numerator, int denominator) {
            boolean isNegative = (numerator < 0 && denominator > 0 || numerator > 0 && denominator < 0) ? true : false;
            long numeratorL = Math.abs((long) numerator);
            long denominatorL = Math.abs((long) denominator);
            Map<Long, Integer> previousRemains = new HashMap<Long, Integer>();
            StringBuilder sb = new StringBuilder();
            long quotian = numeratorL / denominatorL;
            sb.append(quotian);
            
            numeratorL %= denominatorL;
        
            if (numeratorL != 0) {
                sb.append(".");
            }
            
            int quotianIndex = 0;
            while (numeratorL != 0) {
                numeratorL *= 10;
                quotian = Math.abs(numeratorL / denominatorL);
                if (!previousRemains.containsKey(numeratorL)) {
                    sb.append(quotian);
                    previousRemains.put(numeratorL, quotianIndex++);
                } else {
                    int firstIndex = 1 + previousRemains.get(numeratorL) + sb.indexOf(".");
                    sb.insert(firstIndex, '(');
                    sb.append(")");
                    break;
                }
                numeratorL %= denominatorL;
            }
            
            if (isNegative) {
                sb.insert(0, "-");
            }
            return sb.toString();
        }

  • 0
    S

    public String fractionToDecimal(int numerator, int denominator) {
    boolean isNegative = (numerator < 0 && denominator > 0 || numerator > 0 && denominator < 0) ? true : false;
    long numeratorL = Math.abs((long) numerator);
    long denominatorL = Math.abs((long) denominator);
    Map<Long, Integer> previousRemains = new HashMap<Long, Integer>();
    StringBuilder sb = new StringBuilder();
    long quotian = numeratorL / denominatorL;
    sb.append(quotian);

        numeratorL %= denominatorL;
    
        if (numeratorL != 0) {
            sb.append(".");
        }
    
        int quotianIndex = 0;
        while (numeratorL != 0) {
            numeratorL *= 10;
            quotian = Math.abs(numeratorL / denominatorL);
            if (!previousRemains.containsKey(numeratorL)) {
                sb.append(quotian);
                previousRemains.put(numeratorL, quotianIndex++);
            } else {
                int firstIndex = 1 + previousRemains.get(numeratorL) + sb.indexOf(".");
                sb.insert(firstIndex, '(');
                sb.append(")");
                break;
            }
            numeratorL %= denominatorL;
        }
    
        if (isNegative) {
            sb.insert(0, "-");
        }
        return sb.toString();
    }

  • 0
    D

    Up vote for your point over the "short" code.


  • 0

    For a dirty question like this, can not agree more for your point.


Log in to reply
 

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