Leetcode Base 7 problem solution

In this Leetcode Base 7 problem solution Given an integer num, return a string of its base 7 representation.

Leetcode Base 7 problem solution

Problem solution in Python.

class Solution:
    def convertToBase7(self, num: int) -> str:
        if num==0:
            return "0"
        s='';ms=0
        if(num<0):
            num=-num
            ms+=1
        while(num>0):
            s+=str(num%7)
            num//=7
        s=s[::-1]
        return s if ms==0 else '-'+s

Problem solution in Java.

class Solution {
    public String convertToBase7(int num) {
        if (num == 0) {
            return "0";
        }
        String res = "";
        boolean isNegative = num < 0;
        long abs = Math.abs(num);
        while (abs > 0) {
            long rem = abs % 7;
            res = rem + res;
            abs /= 7;
        }
        if (isNegative) {
            res = "-" + res;
        }
        return res;
    }
}

Problem solution in C++.

class Solution {
public:
    string convertToBase7(int num) {
        string result ="";
        if(num==0) return to_string(num);
        string sign = "";
        if(num<0){
            sign = "-";
            num = -num;
        }
        while(num){
            result= to_string(num%7) + result;
            num /=7;
        }
        return sign + result;
    }
};