diff --git a/0001/two b/0001/two new file mode 100644 index 0000000..0edf0ba --- /dev/null +++ b/0001/two @@ -0,0 +1,11 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i in range(len(nums)): + for j in range(i + 1, len(nums)): + + if nums[i] + nums[j] == target: + + return [i,j] + else: + pass + diff --git a/0001/two_sum.py b/0001/two_sum.py index 6cade55..0edf0ba 100644 --- a/0001/two_sum.py +++ b/0001/two_sum.py @@ -1,18 +1,11 @@ -class Solution(object): - def twoSum(self, nums, target): - """ - :type nums: List[int] - :type target: int - :rtype: List[int] - """ - hash_table = dict() - - # Iterating the list - for i, num in enumerate(nums): - - # If the target-num in dict then, we found the solution - try: - hash_table[target - num] - return [hash_table[target - num], i] - except KeyError: - hash_table[num] = i +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i in range(len(nums)): + for j in range(i + 1, len(nums)): + + if nums[i] + nums[j] == target: + + return [i,j] + else: + pass + diff --git a/0001/twosum b/0001/twosum new file mode 100644 index 0000000..0edf0ba --- /dev/null +++ b/0001/twosum @@ -0,0 +1,11 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i in range(len(nums)): + for j in range(i + 1, len(nums)): + + if nums[i] + nums[j] == target: + + return [i,j] + else: + pass + diff --git a/0013/roman 2 integer b/0013/roman 2 integer new file mode 100644 index 0000000..534506a --- /dev/null +++ b/0013/roman 2 integer @@ -0,0 +1,11 @@ +roman = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} + +class Solution: + def romanToInt(self, s: str) -> int: + ans = 0 + for i in range(len(s)-1,-1,-1): + num = roman[s[i]] + if 4 * num < ans: ans -= num + else: ans += num + return ans + diff --git a/0013/roman to integer.py b/0013/roman to integer.py new file mode 100644 index 0000000..534506a --- /dev/null +++ b/0013/roman to integer.py @@ -0,0 +1,11 @@ +roman = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} + +class Solution: + def romanToInt(self, s: str) -> int: + ans = 0 + for i in range(len(s)-1,-1,-1): + num = roman[s[i]] + if 4 * num < ans: ans -= num + else: ans += num + return ans + diff --git a/0013/roman_to_integer.py b/0013/roman_to_integer.py deleted file mode 100644 index 077f610..0000000 --- a/0013/roman_to_integer.py +++ /dev/null @@ -1,12 +0,0 @@ -class Solution: - def romanToInt(self, s): - roman = {'M': 1000, 'D': 500 , 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1} - result = 0 - for i in range(len(s) - 1): - if roman[s[i]] < roman[s[i + 1]]: - result -= roman[s[i]] - else: - result += roman[s[i]] - else: - result += roman[s[-1]] - return result diff --git a/0020/ValidParenthese.py b/0020/ValidParenthese.py new file mode 100644 index 0000000..a8d4012 --- /dev/null +++ b/0020/ValidParenthese.py @@ -0,0 +1,33 @@ +class Solution(object): + def isValid(self, s): + """ + :type s: str + :rtype: bool + """ + + # We'll use a stack to track the open brackets we've seen so far. + stack = [] + + # As we encounter each closed bracket, we'll use this map to + # help us determine if we've seen its corresponding open bracket. + bracket_map = {')': '(', + ']': '[', + '}': '{'} + + for bracket in s: + + # If we see an open bracket, add it to the top of the stack. + if bracket not in bracket_map: + stack.append(bracket) + + # If we see a closed bracket, check if its corresponding + # open bracket is at the top of the stack. If so, pop it off. + elif stack and bracket_map[bracket] == stack[-1]: + stack.pop(-1) + + # Otherwise, the string is not valid. + else: + return False + + # If the resulting stack is empty, then we have a valid string. + return stack == [] diff --git a/0020/ValidParentheses.py b/0020/ValidParentheses.py index a1a5a2b..a8d4012 100644 --- a/0020/ValidParentheses.py +++ b/0020/ValidParentheses.py @@ -1,15 +1,33 @@ -# We declare a stack of unmatched parentheses. As we move, if it is a -# closing parenthesis, we will check if it matches the parentheses on the top -# of stack. +class Solution(object): + def isValid(self, s): + """ + :type s: str + :rtype: bool + """ + + # We'll use a stack to track the open brackets we've seen so far. + stack = [] - -class Solution: - def isValid(self, s: str) -> bool: - pair = dict(('()', '[]', '{}')) - st = [] - for x in s: - if x in '([{': - st.append(x) - elif len(st) == 0 or x != pair[st.pop()]: + # As we encounter each closed bracket, we'll use this map to + # help us determine if we've seen its corresponding open bracket. + bracket_map = {')': '(', + ']': '[', + '}': '{'} + + for bracket in s: + + # If we see an open bracket, add it to the top of the stack. + if bracket not in bracket_map: + stack.append(bracket) + + # If we see a closed bracket, check if its corresponding + # open bracket is at the top of the stack. If so, pop it off. + elif stack and bracket_map[bracket] == stack[-1]: + stack.pop(-1) + + # Otherwise, the string is not valid. + else: return False - return len(st) == 0 + + # If the resulting stack is empty, then we have a valid string. + return stack == []