public class Solution {
    public String convertToBase7(int num) {
        if (num == 0) {
            return "0";
        }
        StringBuffer ans = new StringBuffer();
        String pre = "";
        if (num < 0) {
            pre = "-";
        }
        ans.append(pre);
        String res = mod(Math.abs(num), 7);
        for (int i = res.length() - 1; i >= 0; i--) {
            ans.append(res.charAt(i));
        }
        return ans.toString();
    }

    public String mod(int num, int base) {
        StringBuffer sb = new StringBuffer();
        while (num != 0) {
            int remainder = num % base;
            sb.append(String.valueOf(remainder));
            num = num / base;

        }
        return sb.toString();
    }


    public static void main(String[] args) {
        Solution504 solution = new Solution504();
        System.out.println(solution.convertToBase7(-7));
        System.out.println(solution.convertToBase7(100));
    }
}

@@ -123,55 +123,8 @@
   %7D%0A
-        StringBuffer ans = new StringBuffer();%0A
     
@@ -144,16 +144,16 @@
 e = %22%22;%0A
+
         
@@ -204,33 +204,8 @@
   %7D%0A
-        ans.append(pre);%0A
     
@@ -256,140 +256,41 @@
     
-for (int i = res.length() - 1; i %3E= 0; i--) %7B%0A            ans.append(res.charAt(i));%0A        %7D%0A        return ans.toString()
+%0A        return pre + res
 ;%0A    %7D%0A
 %0A   
@@ -285,17 +285,21 @@
 ;%0A    %7D%0A
+    
 %0A
-
     publ
@@ -555,16 +555,26 @@
 turn sb.
+reverse().
 toString
@@ -584,17 +584,16 @@
 %0A    %7D%0A%0A
-%0A
     publ

update problem 504 solution