Skip to content

Commit

Permalink
013 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Sep 17, 2018
1 parent 0737b0d commit 96e616d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
7 changes: 7 additions & 0 deletions 0013-Roman-to-Integer/cpp-0013/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.5)
project(cpp_0013)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(cpp_0013 ${SOURCE_FILES})
38 changes: 38 additions & 0 deletions 0013-Roman-to-Integer/cpp-0013/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/// Source : https://leetcode.com/problems/roman-to-integer/description/
/// Author : liuyubobobo
/// Time : 2018-09-17

#include <iostream>
#include <unordered_map>

using namespace std;


/// Ad-Hoc
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
int romanToInt(string s) {

unordered_map<char, int> map = {
{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};

int res = 0;
for(int i = 0; i < s.size(); i ++)
if(i + 1 < s.size() && map[s[i]] < map[s[i + 1]]){
res += map[s[i + 1]] - map[s[i]];
i ++;
}
else
res += map[s[i]];

return res;
}
};


int main() {

return 0;
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ email: [[email protected]](mailto:[email protected])
| | | | | | |
| 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/) | | |
| | | | | | |
| 013 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/description/) | [] | [C++](0013-Roman-to-Integer/cpp-0013/) | | |
| 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/) | | |
| 016 | [3Sum Closest](https://leetcode.com/problems/3sum-closest/description/) | [] | [C++](0016-3Sum-Closest/cpp-0016/) | | |
Expand Down

0 comments on commit 96e616d

Please sign in to comment.