Problem
Given an integernum, return a string of its base 7 representation.
Examples
Constraints
- -10^7 <= num <= 10^7
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 504 with 25 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 504 # by problem number
lcpy gen -s base_7 # by problem name
num, return a string of its base 7 representation.
Input: num = 100
Output: "202"
Input: num = -7
Output: "-10"
class Solution:
# Time: O(log_7 |num|)
# Space: O(log_7 |num|)
def convert_to_base_7(self, num: int) -> str:
if num == 0:
return "0"
negative = num < 0
digits = ""
value = abs(num)
while value:
digits = str(value % 7) + digits
value //= 7
return f"-{digits}" if negative else digits
| Time | Space | ||||
|---|---|---|---|---|---|
| O(log_7 | num | ) | O(log_7 | num | ) |