001.Two Sum
来源:互联网 发布:网络禁书大合集男主 编辑:程序博客网 时间:2024/06/07 14:39
题目地址:https://leetcode.com/problems/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.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.
Subscribe to see which companies asked this question
题目翻译:
给定一个整数数组,找到两个数,使得它们的和等于一个特定的目标数。
函数twoSum应返回两个数的索引(包括索引0和索引1 )是从零开始的。
可以假设每个输入有且仅有一个答案。
输入:数字 = {2,7,11,15},目标 = 9
输出:索引0 = 0,索引1 = 1
题目分析:
最简单的办法是双重循环,时间复杂度为:O(n^2),以下只写出其算法的伪代码
for(x1属于数组)
{
for(x2属于不等于x1位置的数值)
{
if(target == x1+x2)
{
reruen result;
}
}
}
而对于内层寻找我们可以用哈希hash_map,将时间复杂度变为O(n)*O(1)
此处我没有使用map由于考虑到数组数值中存储的数据有可能是重复的。
具体算法如下:
遍历数组的每一个元素,用num[i]在hash_map查找,看是否存在hash_map中,若在,这个两个只就是
如不在就存储如map,其中key为 target-num[i], value值为下标
eg:
array:[12,8,1,4] taget:9
(1)12,hash_map中不存在12,将9-12存入 hash_map<9-12,0>
(2)8,hash_map中不存在8,将9-8存入hash_map<9-8,1>
(3) 1,hash_map中存在,则,此key对应的value值为所求,此遍历的数组下标也为所求
代码:
C++
运行时间16ms
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> m; vector<int> result; for(int i=0; i<nums.size(); i++){ //没有找到第二个数 if (m.find(nums[i])==m.end() ) { // 在第一个位置存储第二个数的key m[target - nums[i]] = i; } else { // 找到第二个数 result.push_back(m[nums[i]]); result.push_back(i); break; } } return result; }};
Python
运行时间为40ms
class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ dictMap = {}#字典中保存的key为目标值与数列中当前值的差,value为当前数列值的下标 for index, value in enumerate(nums): if value in dictMap:#查找数列当前值是否在字典中存在目标 return dictMap[value], index dictMap[target - value] = index
- LeetCode 001. Two Sum
- 【LeetCode】001.Two Sum
- leetcode--001. Two Sum
- 【LeetCode】 001. Two Sum
- 001.Two Sum
- 001.Two Sum
- 【LeetCode】001. Two Sum
- [Leetcode.python] 001. Two Sum
- Leetcode Algorithm 001. Two Sum
- Two Sum
- Two Sum
- Two Sum
- two sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- hdu 5806 NanoApe Loves Sequence Ⅱ 尺取法
- Gradle的安装使用
- 有return的情况下try catch finally的执行顺序(最有说服力的总结)
- 数据结构实验之二叉树七:叶子问题
- 实现标题栏与viewPager的连动
- 001.Two Sum
- APP字体大小,不随系统的字体大小变化而变化的方法
- AM3359多路串口支持
- gridview边框线颜色
- 这里只介绍spring配置模式
- Django 中 form 表单如何用 view.py 关联到自定义 html 上
- linux c之syscall示例原型和运行时入口_start
- 数据结构实验之求二叉树后序遍历和层次遍历
- Chrome调试折腾记_(3)模拟弱网情况及请求筛选过滤