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

Beautiful Arrangement II Problem

LeetCode 667. Given two integers n and k, construct a...

Range Addition II Problem

LeetCode 598. You are given an m x n matrix...

Perfect Number Problem

LeetCode 507. A perfect number is a positive integer that...

Optimal Division Problem

LeetCode 553. You are given an integer array nums. The...

Minimum Time Difference Problem

LeetCode 539. Given a list of 24-hour clock time points...

Minimum Moves To Equal Array Elements Problem

LeetCode 453. Given an integer array nums of size n,...