Problem:
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 (C++):
string convertToBase7(int num) {
if (num == 0) return "0";
string res = "";
int x = abs(num);
while (x) {
res = to_string(x%7) + res;
x /= 7;
}
return (num > 0 ? "" : "-") + res;
}
性能:
Runtime: 0 ms Memory Usage: 6 MB
思路:
Solution (C++):
int fib(int N) {
if (N == 0) return 0;
vector<int> dp(N+1, 0);
dp[1] = 1;
for (int i = 2; i <= N; ++i) {
dp[i] = dp[i-1] + dp[i-2];
}
return dp[N];
}
性能:
Runtime: 0 ms Memory Usage: 6.2 MB