Skip to content

added my solution for problem 1 #504

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

Open
wants to merge 3 commits into
base: master
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
21 changes: 21 additions & 0 deletions Python/20_Valid_parentheses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def isValid(self, s: str) -> bool:
stack = []
for i in range(len(s)):
if s[i] == "(" or s[i] == "[" or s[i] == "{": #check if an open bracket, push to stack
stack.append(s[i])
if s[i] == ")" or s[i] == "]" or s[i] == "}": #check if closing bracket, pop from stack and compare
if len(stack) == 0:
return False
prev = stack.pop()
if prev == "(" and s[i] != ")":
return False
if prev == "[" and s[i] != "]":
return False
if prev == "{" and s[i] != "}":
return False

if len(stack) == 0:
return True
else:
return False
8 changes: 8 additions & 0 deletions Python/26_Remove_Duplicates_from_Sorted_Array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
i = 1
while i<len(nums): #traverse array
if nums[i] == nums[i-1]: #if the current element is same as previous, then remove
nums.pop(i)
else:
i += 1
7 changes: 7 additions & 0 deletions Python/TwoSum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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:
ans = [i, j]
return ans