0%

base7

Base 7 – LeetCode 504

Problem

Description

Given an integer, return its base 7 string representation.

Answer

Original

Code

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
string convertToBase7(int num) {
string s;
int tmp = abs(num);
do {
s = (static_cast<char>('0' + tmp % 7)) + s;
tmp /= 7;
} while(tmp > 0);
return (num < 0 ? '-' + s : s);
}
};

思路

最简单的常规进制转换。时间复杂度$O(log_{7}n)$,空间复杂度$O(1)$。
耗时$7$ ms,排名$20.73\%$

Better

思路

还没看到更好的思路。