일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- data science
- 컴퓨터의 구조
- t1
- concurrency
- 밴픽
- 프로그래머스
- 43. Multiply Strings
- Python
- 30. Substring with Concatenation of All Words
- shiba
- 시바견
- 109. Convert Sorted List to Binary Search Tree
- 파이썬
- 715. Range Module
- LeetCode
- Substring with Concatenation of All Words
- Generator
- attribute
- iterator
- DWG
- 운영체제
- Convert Sorted List to Binary Search Tree
- Python Implementation
- Class
- 315. Count of Smaller Numbers After Self
- Python Code
- Protocol
- Regular Expression
- Decorator
- kaggle
Archives
- Today
- Total
Scribbling
LeetCode: 18. 4Sum 본문
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
ret = []
nums.sort()
previ = None
for i in range(len(nums)-3):
if nums[i] == previ:
continue
prevj = None
for j in range(i+1, len(nums)-2):
if nums[j] == prevj:
continue
preSum = nums[i] + nums[j]
l, r = j + 1, len(nums) - 1
while l < r:
summation = preSum + nums[l] + nums[r]
if summation < target:
l += 1
elif summation > target:
r -= 1
else:
ret.append([nums[i], nums[j], nums[l], nums[r]])
prevl, prevr = nums[l], nums[r]
while l <= r and nums[l] == prevl:
l += 1
while r >= l and nums[r] == prevr:
r -= 1
prevj = nums[j]
previ = nums[i]
return ret
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 22. Generate Parentheses (0) | 2021.08.24 |
---|---|
LeetCode: 19. Remove Nth Node From End of List (0) | 2021.08.22 |
LeetCode: 17. Letter Combinations of a Phone Number (0) | 2021.08.21 |
LeetCode: 16. 3Sum Closest (0) | 2021.08.21 |
LeetCode: 15. 3Sum (0) | 2021.08.19 |