Skip to content

Commit 56c1fa2

Browse files
authored
Added 13.Roman_to_Integer.cpp (#185)
* added 13.Roman_to_Integer.cpp * Update Math table on README Added c++ linked selution to Roman_to_Integer * Added 20. Valid_Parentheses.cpp * Added The README missing part * Changed file name from 20. Valid_Parentheses to Valid_Parentheses * Fixed link Indeed I've forgotten to adjust the path as well. Thanks for the patience
1 parent c046825 commit 56c1fa2

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed

Diff for: C++/Roman_to_Integer.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <string>
2+
3+
/*** 13. Roman to Intege (Easy)***/
4+
5+
/*
6+
Symbol Value
7+
I 1
8+
V 5
9+
X 10
10+
L 50
11+
C 100
12+
D 500
13+
M 1000
14+
*/
15+
16+
class Solution {
17+
public:
18+
int romanToInt(string s) {
19+
int current = 0, last =0;
20+
int sum=0;
21+
for(int i=0;i<s.length();i++){
22+
switch(s[i]){
23+
case 'I':
24+
current = 1;
25+
break;
26+
case 'V':
27+
current = 5;
28+
break;
29+
case 'X':
30+
current = 10;
31+
break;
32+
case 'L':
33+
current = 50;
34+
break;
35+
case 'C':
36+
current = 100;
37+
break;
38+
case 'D':
39+
current = 500;
40+
break;
41+
case 'M':
42+
current = 1000;
43+
break;
44+
default:
45+
break;
46+
}
47+
sum+=current;
48+
/*
49+
I can be placed before V (5) and X (10) to make 4 and 9.
50+
X can be placed before L (50) and C (100) to make 40 and 90.
51+
C can be placed before D (500) and M (1000) to make 400 and 900
52+
*/
53+
if(i!=0&&current>last)
54+
sum-=2*last;
55+
last = current;
56+
57+
}
58+
return sum;
59+
}
60+
};

Diff for: C++/Valid_Parentheses.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <list>
2+
#include <string>
3+
/*
4+
Example 1:
5+
Input: s = "()"
6+
Output: true
7+
8+
Example 2:
9+
Input: s = "()[]{}"
10+
Output: true
11+
12+
Example 3:
13+
Input: s = "(]"
14+
Output: false
15+
16+
Example 4:
17+
Input: s = "([)]"
18+
Output: false
19+
20+
Example 5:
21+
Input: s = "{[]}"
22+
Output: true
23+
*/
24+
25+
class Solution {
26+
public:
27+
bool isValid(string s) {
28+
list<char> open;
29+
if(s.length()%2!=0)
30+
return false;
31+
for(int i=0; i<s.length();i++){
32+
if(s[i]=='{'||s[i]=='['||s[i]=='(')
33+
open.push_front(s[i]);
34+
else{
35+
switch(s[i]){
36+
case '}':
37+
if(open.front()!='{')
38+
return false;
39+
break;
40+
case ']':
41+
if(open.front()!='[')
42+
return false;
43+
break;
44+
case ')':
45+
if(open.front()!='(')
46+
return false;
47+
break;
48+
default :
49+
return true;
50+
}
51+
open.pop_front();
52+
}
53+
}
54+
if(open.size()!=0)
55+
return false;
56+
return true;
57+
}
58+
};

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
201201

202202
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
203203
| ---- | ------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- | ------ | ------ | ---------- | ---------------------- | ---- |
204-
| 020 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Python](./Python/20_ValidParentheses.py) | _O(n)_ | _O(n)_ | Easy | Stack | |
204+
| 020 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Python](./Python/20_ValidParentheses.py) [C++](./C++/Vlalid_Parentheses.cpp) | _O(n)_ | _O(n)_ | Easy | Stack | |
205205
| 084 | [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/) | [C++](./C++/Largest-Rectangle-in-Histogram.cpp) | _O(n)_ | _O(n)_ | Hard | Stack |
206206
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Python](./Python/150.EvaluateReversePolishNotation.py) <br> [Java](./Java/evaluate_reverse_polish_notation.java) | _O(n)_ | _O(1)_ | Medium | Stack | |
207207
| 1047 | [Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [C++](./C++/remove-all-adjacent-duplicates-in-string.cpp) | _O(n)_ | _O(n)_ | Easy | Stack | |
@@ -309,7 +309,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
309309
| 202 | [Happy Number](https://leetcode.com/problems/happy-number) | [Java](./Java/Happy-Number.java) | _O(n^2)_ | _O(n)_ | Easy | Math | |
310310
| 326 | [Power of Three](https://leetcode.com/problems/power-of-three) | [Java](./Java/Power-of-Three.java) | _O(logn)_ | _O(n)_ | Easy | Math | |
311311
| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [Java](./Java/integer-to-roman.java) | _O(n)_ | _O(1)_ | Medium | Math | |
312-
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Java](./Java/roman-to-integer.java) | _O(n)_ | _O(1)_ | Easy | Math | |
312+
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Java](./Java/roman-to-integer.java) <br> [C++](./C++/Roman_to_Integer.cpp)| _O(n)_ | _O(1)_ | Easy | Math | |
313313
| 14 | [Arithmetic Subarrays](https://leetcode.com/problems/arithmetic-subarrays/) | [Java](./Java/Arithmetic-Subarrays.java) | _O(m\*n)_ | _O(n)_ | Medium | Math | Pattern Count |
314314
| 263 | [Ugly Number](https://leetcode.com/problems/ugly-number/) | [Java](./Java/Ugly-Number.java) | _O(n)_ | _O(n)_ | Easy | Math | |
315315

0 commit comments

Comments
 (0)