c# solution


  • 0
    M

    public string ConvertToBase7(int num) {
    if (num == 0) return "0";

            string result = string.Empty;
            bool isNegative = num < 0;
            num = Math.Abs(num);            
    
            while (num > 0)
            {
                int mod = num % 7;
                result = mod.ToString() + result;
                num /= 7;
            }
    
            return isNegative ? "-" + result : result;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.