Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
integer-to-roman.cpp 643 Bytes
Copy Edit Raw Blame History
kamyu authored 2016-07-09 22:46 +08:00 . Create integer-to-roman.cpp
// Time: O(n)
// Space: O(1)
class Solution {
public:
string intToRoman(int num) {
const vector<int> nums{1000, 900, 500, 400, 100, 90,
50, 40, 10, 9, 5, 4, 1};
const vector<string> romans{"M", "CM", "D", "CD", "C", "XC", "L",
"XL", "X", "IX", "V", "IV", "I"};
string result;
int i = 0;
while (num > 0) {
int times = num / nums[i];
while (times--) {
num -= nums[i];
result.append(romans[i]);
}
++i;
}
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search