Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

two sum #102

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 0001/two
Original file line number Diff line number Diff line change
@@ -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

29 changes: 11 additions & 18 deletions 0001/two_sum.py
Original file line number Diff line number Diff line change
@@ -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

11 changes: 11 additions & 0 deletions 0001/twosum
Original file line number Diff line number Diff line change
@@ -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

11 changes: 11 additions & 0 deletions 0013/roman 2 integer
Original file line number Diff line number Diff line change
@@ -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

11 changes: 11 additions & 0 deletions 0013/roman to integer.py
Original file line number Diff line number Diff line change
@@ -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

12 changes: 0 additions & 12 deletions 0013/roman_to_integer.py

This file was deleted.

33 changes: 33 additions & 0 deletions 0020/ValidParenthese.py
Original file line number Diff line number Diff line change
@@ -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 == []
44 changes: 31 additions & 13 deletions 0020/ValidParentheses.py
Original file line number Diff line number Diff line change
@@ -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 == []