관리 메뉴

솜씨좋은장씨

[leetCode] 504. Base 7 (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 504. Base 7 (Python)

사용자 솜씨좋은장씨 2020. 10. 12. 22:06

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].

 

Solution

class Solution:
    def convertToBase7(self, num: int) -> str:
        if num == 0:
            return str(num)
        
        sign = 1 
        if num < 0:
            sign = -1
            
        num = abs(num)
        answer = ''
        
        while num:
            answer = str(num % 7) + answer
            num //= 7
            
        return ('-' if sign < 0 else '') + answer

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

0 Comments
댓글쓰기 폼