[leetcode 1]TwoSum
来源:互联网 发布:花生壳域名忘记了 编辑:程序博客网 时间:2024/05/17 22:31
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.//原先索引从1开始,现在改成从0开始.
哈希表,先取出一个数,然后用target减去这个数得到gap,查找gap是否存在于之后的数组里.
最后返回一个vector<int>类型的索引.
vector<int> TwoSum(vector<int>&nums, int target){unordered_map<int, int >hashTable;vector<int>result;for (int i = 0; i < nums.size(); i++)hashTable[nums[i]] = i;//赋上坐标值for (int i = 0; i < nums.size(); i++) //i为取出值的坐标{const int gap = target - nums[i];if (hashTable.find(gap) != hashTable.end() && hashTable[gap]>i)//规定index0小于index1{result.push_back(i);result.push_back(hashTable[gap]);break;}}return result;}
0 0
- leetcode 1----twoSum
- LeetCode 1 # TwoSum
- LeetCode 1 TwoSum
- leetcode 1 twoSum
- leetcode #1 TwoSum
- leetcode 1 TwoSum
- leetcode-1 TwoSum
- {Leetcode} 1。TwoSum
- [leetcode 1]TwoSum
- leetcode-1-TwoSum 哈希表
- [C++]LeetCode #1 twoSum
- leetcode 1 TwoSum
- LeetCode(1)--TwoSum
- Leetcode#1: twoSum
- 1-Leetcode-TwoSum
- LeetCode【1】TwoSum
- LeetCode#1-TwoSum
- leetcode 1:TwoSum (C#语言版)
- RecycleView(二)
- poj 2186 Popular Cows 强连通分量
- Jmeter常见问题集合
- 欢迎使用CSDN-markdown编辑器
- 面试题14:调整数组顺序使奇数在偶数前面
- [leetcode 1]TwoSum
- SQLiteOpenHelper/SQLiteDatabase/Cursor源码解析
- Mysql总结,超实用和详细(初级篇)
- 最大期望值算法(EM算法)---opencv3.1
- C语言数字转换为字符的问题
- java事件处理机制(自定义事件)
- Struts2 文件上传,下载,删除
- iOS 本地通知的实现
- 从WI-FI密码破解来看如何提高WI-FI的安全性