Skip to content

Commit

Permalink
0012 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Sep 17, 2018
1 parent 0c118cb commit 0737b0d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 0012-Integer-to-Roman/cpp-0012/CMakeLists.txt
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})
48 changes: 48 additions & 0 deletions 0012-Integer-to-Roman/cpp-0012/main.cpp
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;
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/) | | |
Expand Down

0 comments on commit 0737b0d

Please sign in to comment.