Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions C++/Roman_to_Integer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <string>

/*** 13. Roman to Intege (Easy)***/

/*
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
*/

class Solution {
public:
int romanToInt(string s) {
int current = 0, last =0;
int sum=0;
for(int i=0;i<s.length();i++){
switch(s[i]){
case 'I':
current = 1;
break;
case 'V':
current = 5;
break;
case 'X':
current = 10;
break;
case 'L':
current = 50;
break;
case 'C':
current = 100;
break;
case 'D':
current = 500;
break;
case 'M':
current = 1000;
break;
default:
break;
}
sum+=current;
/*
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900
*/
if(i!=0&&current>last)
sum-=2*last;
last = current;

}
return sum;
}
};
58 changes: 58 additions & 0 deletions C++/Valid_Parentheses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <list>
#include <string>
/*
Example 1:
Input: s = "()"
Output: true

Example 2:
Input: s = "()[]{}"
Output: true

Example 3:
Input: s = "(]"
Output: false

Example 4:
Input: s = "([)]"
Output: false

Example 5:
Input: s = "{[]}"
Output: true
*/

class Solution {
public:
bool isValid(string s) {
list<char> open;
if(s.length()%2!=0)
return false;
for(int i=0; i<s.length();i++){
if(s[i]=='{'||s[i]=='['||s[i]=='(')
open.push_front(s[i]);
else{
switch(s[i]){
case '}':
if(open.front()!='{')
return false;
break;
case ']':
if(open.front()!='[')
return false;
break;
case ')':
if(open.front()!='(')
return false;
break;
default :
return true;
}
open.pop_front();
}
}
if(open.size()!=0)
return false;
return true;
}
};
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu

| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
| ---- | ------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- | ------ | ------ | ---------- | ---------------------- | ---- |
| 020 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Python](./Python/20_ValidParentheses.py) | _O(n)_ | _O(n)_ | Easy | Stack | |
| 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 | |
| 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 |
| 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 | |
| 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 | |
Expand Down Expand Up @@ -309,7 +309,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 202 | [Happy Number](https://leetcode.com/problems/happy-number) | [Java](./Java/Happy-Number.java) | _O(n^2)_ | _O(n)_ | Easy | Math | |
| 326 | [Power of Three](https://leetcode.com/problems/power-of-three) | [Java](./Java/Power-of-Three.java) | _O(logn)_ | _O(n)_ | Easy | Math | |
| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [Java](./Java/integer-to-roman.java) | _O(n)_ | _O(1)_ | Medium | Math | |
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Java](./Java/roman-to-integer.java) | _O(n)_ | _O(1)_ | Easy | Math | |
| 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 | |
| 14 | [Arithmetic Subarrays](https://leetcode.com/problems/arithmetic-subarrays/) | [Java](./Java/Arithmetic-Subarrays.java) | _O(m\*n)_ | _O(n)_ | Medium | Math | Pattern Count |
| 263 | [Ugly Number](https://leetcode.com/problems/ugly-number/) | [Java](./Java/Ugly-Number.java) | _O(n)_ | _O(n)_ | Easy | Math | |

Expand Down