tinycodinglm / cp /cp-10013.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
1.29 kB
opencode trace
System You are an expert LeetCode problem solver. You solve problems efficiently with clean code.

You are an expert LeetCode problem solver. You solve problems efficiently with clean code.

User

Problem: Base 7 (Difficulty: Easy)
Tags: Math, String

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

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

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

Constraints:

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

Time: O(1)

Space: O(1)

class Solution(object):
def convertToBase7(self, num):
if num < 0:
return '-' + self.convertToBase7(-num)
result = ''
while num:
result = str(num % 7) + result
num //= 7
return result if result else '0'

class Solution2(object):
def convertToBase7(self, num):
"""
:type num: int
:rtype: str
"""
if num < 0:
return '-' + self.convertToBase7(-num)
if num < 7:
return str(num)
return self.convertToBase7(num // 7) + str(num % 7)