-
Notifications
You must be signed in to change notification settings - Fork 72
/
Leetcode.txt
35 lines (31 loc) · 1.2 KB
/
Leetcode.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
--------------------------------------------------------------------------------------------------------
Problem 1:
Link to question: https: // leetcode.com/problems/two-sum/
Solution:
In Python 3:
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)):
sum = nums[i]+nums[j]
if sum == target:
return i, j
return []
--------------------------------------------------------------------------------------------------------
Problem 2:
Link to question: https: // leetcode.com/problems/valid-parentheses/
Solution:
In Python 3:
class Solution:
def isValid(s):
st = [s[0]]
for i in range(1, len(s)):
if len(st) == 0:
st += s[i]
else:
if st[-1]+s[i] in ['()', '[]', '{}']:
st.pop()
else:
st += s[i]
return len(st) == 0
--------------------------------------------------------------------------------------------------------