suleesulee
suleesulee
suleesulee
전체 방문자
오늘
어제
  • 분류 전체보기 (39)
    • Personal (7)
      • 개발자sulee (2)
      • 회고록 (1)
      • 여행 (0)
    • Computer Science (31)
      • JAVA (4)
      • Python (0)
      • Html&CSS (0)
      • Spring (1)
      • JPA (1)
      • MSA (12)
      • Algorithm (10)
      • DevOps (0)
      • Go (1)
      • Swift (1)
      • 기타 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • 개발자 sulee의 블로그입니다.

인기 글

태그

  • 회고
  • 백엔드
  • 전문연구요원
  • SOA
  • 이직뽀개기
  • 네이버
  • 카카오
  • 라인
  • 개발자
  • Monolithic
  • 전문연
  • 나의 재취업 도전기
  • MSA
  • 이직

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
suleesulee

suleesulee

Computer Science/Algorithm

[리트코드]16. 3Sum Closest

2021. 9. 3. 23:32
class Solution:
    def threeSumClosest(self, nums: List[int], target: int) -> int:
        diff = 1e9
        nums = sorted(nums)
        
        for i in range(len(nums)):
            s, e = i + 1, len(nums)-1
            while s < e:
                sum = nums[i] + nums[s] + nums[e]                
                if abs(target - sum) < abs(diff):
                    diff = target - sum
                if sum < target:
                    s += 1
                else:
                    e -= 1
            if diff == 0:
                break
                    
        return target - diff

 

'''

투포인터문제

 

정렬하고 범위 줄여가면서 계산한다.

 

'''

저작자표시 비영리 변경금지 (새창열림)

'Computer Science > Algorithm' 카테고리의 다른 글

[리트코드]1002. Find Common Characters  (0) 2021.09.06
[리트코드]740. Delete and Earn  (0) 2021.09.03
[리트코드]18. 4Sum  (0) 2021.09.03
[리트코드]1762. Buildings With an Ocean View  (0) 2021.09.02
leetcode 5.Longest Palindromic Substring  (0) 2021.02.24
    'Computer Science/Algorithm' 카테고리의 다른 글
    • [리트코드]740. Delete and Earn
    • [리트코드]18. 4Sum
    • [리트코드]1762. Buildings With an Ocean View
    • leetcode 5.Longest Palindromic Substring
    suleesulee
    suleesulee
    IT Engineer, SW Developer, Traveler

    티스토리툴바