Leetcode 504. Base 7
Problem Explanation
This question asks to convert a given number into a base 7 number system.
A base 7 number system consists of digits from 0 to 6. In the base 7 number system's representation, each digit is multiplied with power of 7. For example, let's take a base 7 number "456". It is calculated as (4 * (7^2)) + (5 * (7^1)) + (6 * (7^0)) = 226.
So, if we are given the value in base 10, we need to convert it into base 7 equivalents.
Approach
The approach used here is a recursive approach. Firstly, we check if the number is smaller than zero. If it is, then we return "-" and the base 7 equivalent of the positive of the number. If the number is less than 7, we return its string equivalent directly. If it's greater or equal to 7, we divide the number by 7 recursively, and also take modulo of 7, and concatenate them.
Java Solution
1
2java
3class Solution {
4 public:
5 string convertToBase7(int num) {
6 if (num < 0)
7 return "-" + convertToBase7(-num);
8 if (num < 7)
9 return to_string(num);
10 return convertToBase7(num / 7) + to_string(num % 7);
11 }
12};
Python Solution
1 2python 3class Solution: 4 def convertToBase7(self, num: int) -> str: 5 if num < 0: 6 return "-" + self.convertToBase7(-num) 7 if num < 7: 8 return str(num) 9 return self.convertToBase7(num // 7) + str(num % 7)
JavaScript Solution
1
2javascript
3var convertToBase7 = function(num) {
4 if (num < 0)
5 return "-" + convertToBase7(-num);
6 if (num < 7)
7 return String(num);
8 return convertToBase7(Math.floor(num / 7)) + String(num % 7);
9};
C++ Solution
1
2c++
3class Solution {
4public:
5 string convertToBase7(int num) {
6 if (num < 0)
7 return "-" + convertToBase7(-num);
8 if (num < 7)
9 return to_string(num);
10 return convertToBase7(num / 7) + to_string(num % 7);
11 }
12};
C# Solution
1
2csharp
3public class Solution {
4 public string ConvertToBase7(int num) {
5 if (num < 0)
6 return "-" + ConvertToBase7(-num);
7 if (num < 7)
8 return num.ToString();
9 return ConvertToBase7(num / 7) + (num % 7).ToString();
10 }
11}
Remember to always refer to your language's specific syntax for string concatenation and number conversion when implementing the solution. Also, use the floor division (// in Python, / in C++/Java and Math.floor in JavaScript) for the division to get the integer quotient.## Go Solution
In Go, string formatting works differently than in the aforementioned languages. We use fmt.Sprintf to convert an integer to a string, which works similarly to printf/scanf in C.
1
2go
3package main
4import "fmt"
5
6func convertToBase7(num int) string {
7 if num < 0 {
8 return "-" + convertToBase7(-num)
9 }
10 if num < 7 {
11 return fmt.Sprintf("%d", num)
12 }
13 return convertToBase7(num / 7) + fmt.Sprintf("%d", num % 7)
14}
15
16func main() {
17 fmt.Println(convertToBase7(100)) // Prints 202
18 fmt.Println(convertToBase7(-7)) // Prints -10
19 fmt.Println(convertToBase7(-49)) // Prints -100
20}
The main function in the code, when run, prints the base 7 equivalents of the number.
Swift Solution
Swift has built-in features for the conversion from one base to another, and you can leverage this.
1
2swift
3func convertToBase7(_ num: Int) -> String {
4 if num < 0 {
5 return "-" + convertToBase7(-num)
6 }
7 if num < 7 {
8 return String(num)
9 }
10 return convertToBase7(num / 7) + String(num % 7)
11}
You can test this function out as follows:
1
2swift
3print(convertToBase7(100)) // Prints "202"
4print(convertToBase7(-7)) // Prints "-10"
5print(convertToBase7(-49)) // Prints "-100"
Ruby Solution
In Ruby, you directly use the to_s method to convert an integer to a string.
1
2ruby
3def convert_to_base7(num)
4 if num < 0
5 return "-" + convert_to_base7(-num).to_s
6 end
7 if num < 7
8 return num.to_s
9 end
10 return convert_to_base7(num / 7).to_s + (num % 7).to_s
11end
To test this function, you can:
1 2ruby 3puts convert_to_base7(100) # Prints "202" 4puts convert_to_base7(-7) # Prints "-10" 5puts convert_to_base7(-49) # Prints "-100"
These Go, Swift, and Ruby solutions use the same recursive approach, demonstrating its universality.
Got a question?ย Ask the Teaching Assistantย anything you don't understand.
Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.