feat(0504_base-7): add py3 soln
This commit is contained in:
parent
2348097dd3
commit
74770ca9b1
17
0504_base-7/README.md
Normal file
17
0504_base-7/README.md
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
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 <= 107`
|
11
0504_base-7/python3/solution.py
Normal file
11
0504_base-7/python3/solution.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
class Solution:
|
||||||
|
def convertToBase7(self, num: int) -> str:
|
||||||
|
sign = '-' if num < 0 else ''
|
||||||
|
num = abs(num)
|
||||||
|
b7 = ''
|
||||||
|
|
||||||
|
while num > 0:
|
||||||
|
b7 = str(num % 7) + b7
|
||||||
|
num //= 7
|
||||||
|
|
||||||
|
return '0' if b7 == '' else sign + b7
|
Loading…
Reference in New Issue
Block a user