From 96e616d8475c9118c32f33ca0b832e89020e8833 Mon Sep 17 00:00:00 2001 From: liuyubobobo Date: Mon, 17 Sep 2018 00:27:08 -0700 Subject: [PATCH] 013 solved. --- 0013-Roman-to-Integer/cpp-0013/CMakeLists.txt | 7 ++++ 0013-Roman-to-Integer/cpp-0013/main.cpp | 38 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 0013-Roman-to-Integer/cpp-0013/CMakeLists.txt create mode 100644 0013-Roman-to-Integer/cpp-0013/main.cpp diff --git a/0013-Roman-to-Integer/cpp-0013/CMakeLists.txt b/0013-Roman-to-Integer/cpp-0013/CMakeLists.txt new file mode 100644 index 00000000..b4005238 --- /dev/null +++ b/0013-Roman-to-Integer/cpp-0013/CMakeLists.txt @@ -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}) \ No newline at end of file diff --git a/0013-Roman-to-Integer/cpp-0013/main.cpp b/0013-Roman-to-Integer/cpp-0013/main.cpp new file mode 100644 index 00000000..d8994b9e --- /dev/null +++ b/0013-Roman-to-Integer/cpp-0013/main.cpp @@ -0,0 +1,38 @@ +/// Source : https://leetcode.com/problems/roman-to-integer/description/ +/// Author : liuyubobobo +/// Time : 2018-09-17 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + int romanToInt(string s) { + + unordered_map 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; +} \ No newline at end of file diff --git a/readme.md b/readme.md index a4590228..d76bd3c2 100644 --- a/readme.md +++ b/readme.md @@ -56,7 +56,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 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/) | | |