1. Two Sum
題目原文
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
解題思路
先計算每一個數字還差多少才會等於target,相差值暫稱為complement。
去map裡找是否存在complement,如果發現返回數字的index與complement的index。
如果沒有發現complement,把數字放入map裡,指向下一個數字,回到步驟2。
程式解答
Last updated
Was this helpful?