取余,然后翻转。
class Solution {
public String convertToBase7(int num) {
if(num==0){
return "0";
}
String str = "";
if(num<0){
str += "-";
num = -num;
}
StringBuilder sb = new StringBuilder();
while(num>0){
sb.append(num%7);
num /= 7;
}
return str+sb.reverse().toString();
}
}