Skip to content

Latest commit

 

History

History
30 lines (25 loc) · 630 Bytes

README.md

File metadata and controls

30 lines (25 loc) · 630 Bytes

504. Base 7

Given an integer, return its base 7 string representation.

Example 1:

Input: 100
Output: "202"

Example 2:

Input: -7
Output: "-10"

Note: The input will be in range of [-1e7, 1e7].

Solutions (Python)

1. Solution

class Solution:
    def convertToBase7(self, num: int) -> str:
        if num >= 7:
            return self.convertToBase7(num // 7) + str(num % 7)
        elif num < 0:
            return '-' + self.convertToBase7(-num)
        elif num < 7:
            return str(num)