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.
給定一個(gè)整形數(shù)組,返回兩個(gè)數(shù)的索引,使對(duì)應(yīng)的數(shù)組元素之和為目標(biāo)值。 每次輸入都恰好有且只有一組解,單個(gè)數(shù)組元素只能使用一次。
Example: Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
遍歷數(shù)組求和,此法需遍歷2次,使用HashMap好像只需要遍歷一次,日后補(bǔ)上。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注