Online judge pass -- java version


  • 6
    R
     public class Solution {
            public String fractionToDecimal(int numerator, int denominator) {
                if (denominator == 0) {
                    throw new IllegalArgumentException(" denominator can not be zero");
                }
                
                // avoid overflow for minimum vlaue of interger
                long newNum = (long) numerator;
                long newDeNom = (long) denominator;
        		
        		StringBuilder sb = new StringBuilder();
                
                // detect sign
                if ((newNum > 0 && newDeNom < 0) || (newNum <0 && newDeNom > 0)) {
        		    sb.append("-");
        		}
        		// make sure it is postive value
        		newNum = Math.abs(newNum);
        		newDeNom = Math.abs(newDeNom);      
                       
                sb.append(newNum / newDeNom);
                
                long reminder = newNum % newDeNom;
                Map<Long, Integer> reminderMap = new HashMap<>();
                
                if (reminder != 0) {
                    sb.append(".");
                }
                
                while (reminder != 0 && !reminderMap.containsKey(reminder)) {
                    reminderMap.put(reminder, sb.length());
                    reminder *= 10;
                    sb.append(reminder / newDeNom);
                    reminder = reminder % newDeNom; 
                }
                
                if (reminderMap.containsKey(reminder)) {
                    sb.insert(reminderMap.get(reminder), "(");
                    sb.append(")");
                }
                return sb.toString();
            }
        }

  • 0
    L

    Mine is similar, I used a HashMap instead of HashSet to record the position where we should insert the parentheses.

    public class Solution {
        public String fractionToDecimal(int numerator, int denominator)
        {
            if (0 == denominator)
            {
                return "";
            }
            else if (1 == denominator)
            {
                return String.valueOf(numerator);
            }
            else if (-1 == denominator)
            {
                return String.valueOf(-(long)numerator);
            }
            else if (0 == numerator)
            {
                return "0";
            }
            else
            {
                final StringBuilder sb = new StringBuilder();
                if ((numerator > 0 && denominator < 0) || (numerator < 0 && denominator > 0))
                {
                    sb.append("-");
                }
                
                final long num = Math.abs((long)numerator);
                final long den = Math.abs((long)denominator);
                
                
                sb.append(num / den);
                
                if (num % den != 0)
                {
                    sb.append(".");
                    final Map<Long, Integer> map = new HashMap<>();
                    long remain;
                    for (remain = num % den; remain != 0; )
                    {
                        if (map.containsKey(remain))
                        {
                            sb.insert(map.get(remain), "(");
                            sb.append(")");
                            break;
                        }
                        else
                        {
                            map.put(remain, sb.length());
                        }
                        
                        remain *= 10;
                        sb.append(remain / den);
                        remain %= den;
                    }
                }
                
                return sb.toString();
            }
        }
    }

  • 0
    R

    I think using map is better than set. thanks. but no need to handle 1 , -1 and 0 cases separately


Log in to reply
 

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