Skip to content

Commit 0fb8bd0

Browse files
committed
more python solution
1 parent e66ddbd commit 0fb8bd0

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#Brute Force
2+
class Solution:
3+
def maxArea(self, height:List[int])->int:
4+
maxarea = 0
5+
6+
for i in range(0, len(height), 1):
7+
for j in range(i + 1, len(height), 1):
8+
maxarea = max(maxarea, min(height[i], height[j]) * (j - i))
9+
10+
return maxarea
11+
12+
# Two pointer Approach
13+
class Solution:
14+
def maxArea(self, height:List[int])->int:
15+
#init the param
16+
left, right, width, res = 0, len(height) - 1, len(height) - 1, 0
17+
for w in range (width, 0, -1):
18+
if height[left] < height[right]:
19+
res, left = max(res, height[left] * w), left + 1
20+
else:
21+
res, right = max(res, height[right] * w), right - 1
22+
23+
return res
24+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
# solution using set
8+
class Solution:
9+
def hasCycle(self, haed:ListNode)->bool:
10+
# check the input param
11+
if head is None:
12+
return False
13+
14+
node_set = {}
15+
while head.next:
16+
node_set[head] = True
17+
if head.next in node_set:
18+
return True
19+
head = head.next
20+
21+
return False
22+
23+
# solution using two pointer
24+
class Solution:
25+
def hasCycle(self, head:ListNode)->bool:
26+
# check the input param
27+
if head == None or head.next == None:
28+
return False
29+
30+
pslow = pfast = head
31+
while pfast != None and pfast.next != None:
32+
pslow = pslow.next
33+
pfast = pfast.next.next
34+
if pfast == pslow:
35+
break
36+
37+
if pfast == None or pfast.next == None:
38+
return False
39+
elif pfast == pslow:
40+
return True
41+
42+
return False
43+

python/leetcode167_two_sum_ii/solution.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
class Solution:
2+
def twoSum(self, number:List[int], target: int)->List[int]:
3+
for i in range (0, len(numbers), 1):
4+
for j in range (i + 1, len(numbers), 1):
5+
if numbers[i] + numbers[j] == target:
6+
return [i + 1, j + 1]
7+
8+
return []
9+
110
class Solution:
211
"""
312
" type namber: List[int]

0 commit comments

Comments
 (0)