From 8c5e916bb39448b362d1d04a4dcfc758ed658c8c Mon Sep 17 00:00:00 2001 From: Alka107 <78424521+Alka107@users.noreply.github.com> Date: Wed, 27 Oct 2021 21:50:15 +0530 Subject: [PATCH 1/7] two-sum --- 0001/two_sum.py | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) 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 + From 4368812057d0943b52e653c4c02f3a97c0b03ceb Mon Sep 17 00:00:00 2001 From: Alka107 <78424521+Alka107@users.noreply.github.com> Date: Wed, 27 Oct 2021 21:50:51 +0530 Subject: [PATCH 2/7] twosum --- 0001/twosum | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 0001/twosum 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 + From f987ce1f99205d1386d3fd82e1858498660f4ae7 Mon Sep 17 00:00:00 2001 From: Alka107 <78424521+Alka107@users.noreply.github.com> Date: Wed, 27 Oct 2021 21:51:39 +0530 Subject: [PATCH 3/7] two --- 0001/two | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 0001/two 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 + From cefeaaea7697cbdf72f11ba2d8469d9cdabe6b12 Mon Sep 17 00:00:00 2001 From: Alka107 <78424521+Alka107@users.noreply.github.com> Date: Wed, 27 Oct 2021 21:59:29 +0530 Subject: [PATCH 4/7] roman 2 integer --- 0013/roman to integer.py | 11 +++++++++++ 0013/roman_to_integer.py | 12 ------------ 2 files changed, 11 insertions(+), 12 deletions(-) create mode 100644 0013/roman to integer.py delete mode 100644 0013/roman_to_integer.py 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 From 80ff563d10c43a7ff3552880f398624b31b2180b Mon Sep 17 00:00:00 2001 From: Alka107 <78424521+Alka107@users.noreply.github.com> Date: Wed, 27 Oct 2021 22:00:14 +0530 Subject: [PATCH 5/7] roman 2 integer --- 0013/roman 2 integer | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 0013/roman 2 integer 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 + From e528db49f78b734ceb131415529002c7b336876c Mon Sep 17 00:00:00 2001 From: Alka107 <78424521+Alka107@users.noreply.github.com> Date: Thu, 28 Oct 2021 10:54:08 +0530 Subject: [PATCH 6/7] valid --- 0020/ValidParenthese.py | 33 +++++++++++++++++++++++++++++++++ 0020/ValidParentheses.py | 15 --------------- 2 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 0020/ValidParenthese.py delete mode 100644 0020/ValidParentheses.py 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 deleted file mode 100644 index a1a5a2b..0000000 --- a/0020/ValidParentheses.py +++ /dev/null @@ -1,15 +0,0 @@ -# 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: - 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()]: - return False - return len(st) == 0 From e003df6890118cdaee069f548fd8030f77446e89 Mon Sep 17 00:00:00 2001 From: Alka107 <78424521+Alka107@users.noreply.github.com> Date: Thu, 28 Oct 2021 10:56:06 +0530 Subject: [PATCH 7/7] valid parentheness --- 0020/ValidParentheses.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 0020/ValidParentheses.py diff --git a/0020/ValidParentheses.py b/0020/ValidParentheses.py new file mode 100644 index 0000000..a8d4012 --- /dev/null +++ b/0020/ValidParentheses.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 == []