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