外传
经过室友推荐blog转战Markdown编辑,初次使用还觉得挺不方便需要那么多快捷键,不如鼠标点的快。今天这一篇文章才写一半就觉得很好用,就像电脑操作用鼠标点击和windows快捷键操作一样爽,在此向还没转战的朋友推荐,顺便练习自己的操作。
不方便的是代码高亮显示不好。参考快速上手
记录插入链接格式
题目
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].
题意
给定一个整型数组和一个整数target,当数组中两个元素的和等于target,返回这两个元素的索引。
注意
- 返回的索引是从0开始还是从1开始
- 无解返回什么
- 多个解返回什么
- 该题保证了有唯一解并且同一个元素不可能使用两次
思路1
时间复杂度:O(n),查找表实现
将nums中的元素统计出现的频率,遍历nums查找target-nums[i]的元素是否存在,存在则返回i和target-nums[i]元素索引。