๐Ÿ“– Two Sum

๐Ÿ“–What I thought

  1. ๋‘ ์ˆ˜๋ฅผ ๋”ํ•ด์„œ target์„ ์ฐพ์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ
  2. target์—์„œ ํ•œ ๊ฐ’์„ ๋นผ์„œ ๋‚˜์˜จ ๊ฐ’์ด nums์— ์žˆ์œผ๋ฉด ๋ฐ”๋กœ ๋‘ ์ˆ˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Œ
  3. ์ธ๋ฑ์Šค๋ฅผ returnํ•ด์•ผ ํ•˜๋‹ˆ๊นŒ ์ธ๋ฑ์Šค ๊ฐ’์„ ๋”•์…”๋„ˆ๋ฆฌ์— {num:idx} ํ˜•ํƒœ๋กœ ์ €์žฅํ•ด๋‘์ž
  4. ๋ฆฌ์ŠคํŠธ๋Š” ์•ž์—์„œ๋ถ€ํ„ฐ ๋Œ๊ณ  ๋”•์…”๋„ˆ๋ฆฌ์—๋Š” ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋˜‘๊ฐ™์€ ๊ฐ’ ์žˆ์–ด๋„ ใ„ฑใ…Š

๐Ÿ“–ํ’€์ด

๐Ÿ“ŽDictionary

def twoSum(self, nums: List[int], target: int) -> List[int]:
    hs = dict()
    
    for i, num in enumerate(nums):
        hs[num] = i
        
    for i, num in enumerate(nums):
        if target - num in hs and i != hs[target - num]:
            return [i, hs[target - num]]

๐Ÿ“–What I learned

  1. ๋”•์…”๋„ˆ๋ฆฌ (aka. hash table)์€ ์ •๋ง ๋น ๋ฅด๋‹น

๐Ÿ“–๊ด€๋ จ ์ง€์‹

๐Ÿ“ŽDictionary

๐Ÿ“–Hash Table