Two Sum
Last updated
Was this helpful?
Last updated
Was this helpful?
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
์ฒซ๋ฒ์งธ ์ธ์๋ก ์ซ์๋ค๋ก ์ด๋ฃจ์ด์ง ๋ฐฐ์ด์ด ๋ค์ด์ด
๋๋ฒ์งธ ์ธ์๋ ์ฒซ๋ฒ์งธ ์์๋ก ๋ค์ด์จ ๋ฐฐ์ด์์์์ 2๊ฐ๋ฅผ ์ด์ฉํ์ฌ ๋ํ๊ฐ์ด ๋๋ฒ์งธ ์ธ์ ๊ฐ์ด ๋์ด์ผ ํ๋ค.
๋ฆฌํด๊ฐ์ [์ธ๋ฑ์ค, ์ธ๋ฑ์ค]
for๋ฌธ์ ๋๋ ค ๋ฐฐ์ด์ ์ํํ๋ค.
slice๋ฅผ ํตํด index + 1 ํ ์์์์ target์ ๋ง๋ค ์ ์๋ ์์๋ฅผ ์ฐพ๋๋ค.
Runtime: 176 ms
Memory Usage: 41.1 MB