504. Base 7
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
相关链接如下:
知乎:littledy
GitHub主页:https://github.com/littledy
github.io:https://littledy.github.io/
欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可
作者:littledy
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。