1. Two Sum

2021. 5. 5. 21:20·Algorithm
 

Two Sum - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

1. Sliding Window

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        index_list = [i for i in range(len(nums))]
        new_nums = list(map(lambda x, y: (x, y), nums, index_list))
        new_nums.sort()
        start, end = 0, len(new_nums) - 1
        while start < end:
            temp = new_nums[start][0] + new_nums[end][0]
            if temp < target:
                start += 1
            elif temp == target:
                return [new_nums[start][1], new_nums[end][1]]
            elif temp > target:
                end -= 1

너무나 유명한 문제여서 설명은 이하 생략.

개인적으로 요즘 코딩 테스트에 자주 등장하는 알고리즘 기법인 것 같다.

'Algorithm' 카테고리의 다른 글

739. Daily Temperatures  (0) 2021.05.06
78. Subsets  (2) 2021.05.06
206. Reverse Linked List  (0) 2021.05.05
234. Palindrome Linked List  (0) 2021.05.04
226. Invert Binary Tree  (1) 2021.05.03
'Algorithm' 카테고리의 다른 글
  • 739. Daily Temperatures
  • 78. Subsets
  • 206. Reverse Linked List
  • 234. Palindrome Linked List
Doljae
Doljae
  • Doljae
    Zero to Hero
    Doljae
  • 전체
    오늘
    어제
    • 분류 전체보기 (350)
      • Programming (54)
      • Algorithm (161)
      • Review (103)
      • Career (8)
      • Diary (18)
      • Shorts (4)
      • Temp (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 글 쓰기
    • 관리
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    코딩테스트
    AI
    라인
    PYTHON
    ChatGPT
    컨퍼런스
    회고
    공채
    leetcode
    나는리뷰어다
    인프콘
    나는 리뷰어다
    mysql
    java
    jpa
    백준
    BOJ
    sql
    프로그래머스
    면접
    line
    2022
    database
    2023
    한빛미디어
    코딩
    2021
    sql튜닝
    개발자
    db
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
Doljae
1. Two Sum
상단으로

티스토리툴바