送分题花了快20分钟。
感觉自己太菜了。数学计算加减乘除。
java版本:
class Solution {
public String convertToBase7(int num) {
int count=1,visited=0,temp=0;
if(num==0){
return "0";
}
if(num<0){
visited=1;
num=-num;
}
while(num>=count){
count=7*count;
}
count=count/7;
StringBuffer str=new StringBuffer();
while(num>=count || count>=1){
temp=num/count;
str.append(String.valueOf(temp));
num=num-temp*count;
count=count/7;
if(count==0){
break;
}
}
if(visited==1){
str.insert(0,"-");
}
return str.toString();
}
}