考察對(duì)N進(jìn)制的理解鳍征,沒啥好說的。
Given an integer, return its base 7 string representation.
Example 1:
Input: 100
Output: "202"
Example 2:
Input: -7
Output: "-10"
class Solution {
public String convertToBase7(int num) {
StringBuilder sb = new StringBuilder();
int index = 0;
if(num==0)
return "0";
if(num<0)
{
sb.append('-');
index = num<0? 1:0;
num=-num;
}
while(num!=0)
{
sb.insert(index,num%7);
num=num/7;
}
return sb.toString();
}
}