起源
一道编程题,如下:
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:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
我的做法是两个for遍历,然而效率不高,尤其是在大量数据的情况下,劣势明显。看到discuss区有人采用hashmap,非常高效。之前没有接触过,于是学习一波。
hashmap
先占坑,等我快忘了后再翻翻我的笔记再来写。