504. Base 7

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) {
        if(num < 0) return "-" + convertToBase7(-num);
        if(num < 7) return "" + num;
        return(convertToBase7(num / 7) + (num % 7) + "");
    }
}

由此可演变成base N

posted @ 2019-11-29 09:23  Schwifty  阅读(111)  评论(0编辑  收藏  举报