[504] Base 7
https://leetcode.com/problems/base-7/description/
- algorithms
- Easy (44.10%)
- Source Code: 504.base-7.py
- Total Accepted: 40K
- Total Submissions: 89.4K
- Testcase Example: '100'
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].
python
class Solution(object):
def convertToBase7(self, num):
"""
:type num: int
:rtype: str
"""
positive = True
if num < 0:
positive = False
num = -1 * num
if num == 0: return '0'
result = ''
while num >= 7:
a, b = num / 7, num % 7
result += str(b)
num = a
if num: result += str(num)
if not positive:
return '-' + result[::-1]
return result[::-1]
#
#
# s = Solution()
# print s.convertToBase7(100)
#