forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c118cb
commit 0737b0d
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(cpp_0012) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
set(SOURCE_FILES main.cpp) | ||
add_executable(cpp_0012 ${SOURCE_FILES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/// Source : https://leetcode.com/problems/integer-to-roman/description/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-09-16 | ||
|
||
#include <iostream> | ||
#include <cmath> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Ad-Hoc | ||
/// Time Complexity: O(1) | ||
/// Space Complexity: O(1) | ||
class Solution { | ||
public: | ||
string intToRoman(int num) { | ||
|
||
string res = ""; | ||
string one[] = {"I", "X", "C", "M", ""}, five[] = {"V", "L", "D", ""}; | ||
for(int k = 3; k >= 0; k --){ | ||
int b = (int)pow(10, k); | ||
if(num >= b){ | ||
int x = num / b; | ||
res += rome(x, one[k], five[k], one[k + 1]); | ||
num %= b; | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
private: | ||
string rome(int x, string one, string five, string ten){ | ||
if(x <= 3) | ||
return string(x, one[0]); | ||
else if(x == 4) | ||
return one + five; | ||
else if(x <= 8) | ||
return five + string(x - 5, one[0]); | ||
// x == 9 | ||
return one + ten; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ email: [[email protected]](mailto:[email protected]) | |
| 007 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/description/) | [solution](https://leetcode.com/problems/reverse-integer/solution/) | [C++](0007-Reverse-Integer/cpp-0007/) | | | | ||
| | | | | | | | ||
| 011 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/description/) | [solution](https://leetcode.com/problems/container-with-most-water/solution/) | [C++](0011-Container-With-Most-Water/cpp-0011/) | | | | ||
| 012 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman/description/) | [无] | [C++](0012-Integer-to-Roman/cpp-0012/) | | | | ||
| | | | | | | | ||
| 014 | [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/description/) | [无] | [C++](0014-Longest-Common-Prefix/cpp-0014/) | | | | ||
| 015 | [3Sum](https://leetcode.com/problems/3sum/description/) | [无] | [C++](0015-3Sum/cpp-0015/) | | | | ||
|