Skip to content

Commit 05b5dfe

Browse files
committed
New solutions
1 parent d1d0526 commit 05b5dfe

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

Hashing/4-sum.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
3+
'''
4+
class Solution:
5+
# @param A : list of integers
6+
# @param B : integer
7+
# @return a list of list of integers
8+
def fourSum(self, arr, target):
9+
i = 0
10+
j = 1
11+
flag = 0
12+
n = len(arr)
13+
ans = []
14+
if n < 4:
15+
return ans
16+
arr.sort()
17+
while i < n-3:
18+
j = i+1
19+
while j < n-2:
20+
num = target - (arr[i]+arr[j])
21+
k = j+1
22+
l = n-1
23+
while k < l:
24+
sum_ = arr[k]+arr[l]
25+
if sum_ == num:
26+
ans.append([arr[i],arr[j],arr[k],arr[l]])
27+
while arr[k] == arr[k+1] and k < n-2:
28+
k += 1
29+
while arr[l] == arr[l-1] and l > 0:
30+
l -= 1
31+
k += 1
32+
l -= 1
33+
elif sum_ < num:
34+
k += 1
35+
else:
36+
l -= 1
37+
while arr[j] == arr[j+1] and j < n-2:
38+
j += 1
39+
j += 1
40+
while arr[i] == arr[i+1] and i < n-2:
41+
i += 1
42+
i += 1
43+
return ans

Hashing/longest-substring-without-repeat.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,3 @@ def lengthOfLongestSubstring(self, A):
3333
prev_best = min(i-prev[A[i]], prev_best+1)
3434
best = max(best, prev_best)
3535
return best
36-
Close

0 commit comments

Comments
 (0)