Base 7 Problem


Description

LeetCode Problem 504.

Given an integer num, return a string of its base 7 representation.

Example 1:

1
2
Input: num = 100
Output: "202"

Example 2:

1
2
Input: num = -7
Output: "-10"

Constraints:

  • -10^7 <= num <= 10^7


Sample C++ Code

1
2
3
4
5
6
7
8
class Solution {
public:
    string convertToBase7(int n) {
        if (n < 0) return "-" + convertToBase7(-n);
        if (n < 7) return to_string(n);
        return convertToBase7(n / 7) + to_string(n % 7);
    }
};




Related Posts

Three Equal Parts Problem

LeetCode 927. You are given an array arr which consists...

Surface Area Of 3D Shapes Problem

LeetCode 892. You are given an n x n grid...

Super Palindromes Problem

LeetCode 906. Let’s say a positive integer is a super-palindrome...

Smallest Range I Problem

LeetCode 908. You are given an integer array nums and...

Projection Area Of 3D Shapes Problem

LeetCode 883. You are given an n x n grid...

Prime Palindrome Problem

LeetCode 866. Given an integer n, return the smallest prime...