Leetcode (1) Two Sum 解题报告
来源:互联网 发布:windows to go装win10 编辑:程序博客网 时间:2024/06/05 03:47
题目:Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
- Tags:
Array
、Hash Table
题目分析:
- 题目要求:在一个数组中找到两个数,使其和为目标值,返回下标。
- 注意事项:
- 小坐标在前,大坐标在后;
- 坐标从1开始;
方案
方案一:排序
- 将原数组拷贝到一个新数组中,对新数组进行排序;定义两个下标i,j,其中i=0,j=size-1,取出新数组数据求和。若sum小于 target,i++;若sum大于target,j-1;若相等,则证明找到了。然后在原数组找到对应的下标。
- 或者建立一个类,类中包括值与下标,对该类按照值进行排序后按1中进行查找,找到后直接可以得到对应下标。
代码如下:(Runtime: 12 ms)
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> temp(nums); sort(temp.begin(),temp.end()); vector<int> twoNum; int i=0,j=nums.size()-1; while(i<j) { int sum=temp[i]+temp[j]; if(sum==target){ int index1=find(nums.begin(),nums.end(),temp[i])-nums.begin()+1; int index2; if(temp[i]!=temp[j]) index2=find(nums.begin(),nums.end(),temp[j])-nums.begin()+1; else index2=find(nums.begin()+index1,nums.end(),temp[j])-nums.begin()+1; if(index1>index2) index1^=index2^=index1^=index2; twoNum.push_back(index1); twoNum.push_back(index2); break; }else if(sum<target) i++; else j--; } return twoNum; }};
方案二:哈希表
建立一个哈希表,将value映射到index上。对原数组遍历,对于num[i],查找target-num[i]在哈希表中是否存在,若不存在,则将num[i]加入哈希表中;若存在,则输出index。
代码如下:(Runtime: 24 ms)
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { // hash table; vector<int> twoNum; unordered_map<int,int> hmap; for(int i=0;i<nums.size();i++){ unordered_map<int,int>::iterator it = hmap.find(target-nums[i]); if(it==hmap.end()){ hmap.insert(pair<int,int>(nums[i],i+1)); } else{ twoNum.push_back(it->second); twoNum.push_back(i+1); break; } } return twoNum; }};
0 0
- Leetcode (1) Two Sum 解题报告
- LeetCode(1) Two Sum解题报告
- LeetCode 解题报告 Two Sum
- LeetCode-Two Sum-解题报告
- [LeetCode] Two Sum 解题报告
- 【LeetCode】Two Sum 解题报告
- LeetCode OJ 第1题 Two Sum 解题报告
- [Leetcode]之一《two sum》解题报告
- [leetcode] 1. Two Sum 解题报告
- LeetCode 1. Two Sum 解题报告
- LeetCode 1.Two Sum 解题报告
- 【LeetCode】1. Two Sum 解题报告
- LeetCode 167.Two Sum II 解题报告
- LeetCode 1.Two Sum 解题报告
- [Leetcode] 1. Two Sum 解题报告
- leetcode解题报告1. Two Sum
- leetcode | 1. Two sum 解题报告
- [LeetCode 解题报告]001.Two Sum
- 自定义TextView的TextSize属性getDimension和setTextSize的冲突
- 【转】PLSQL Developer使用技巧整理
- Deep Hashing for Compact Binary Codes Learning
- 游戏开发中,回调机制和事件机制的灵活运用
- android 根据字母排序ListView item数据
- Leetcode (1) Two Sum 解题报告
- 常用分类器的效果对比
- 【Java】双等号和equals的区别
- NSFileManager的理解
- C#事件(event)解析
- Makefile自动生成工具-----autotools的使用(详细)
- UVa 1297 - The Minimum Number of Rooks
- TIMIT语音库(续)
- Opencv提取不规则ROI