LeetCode:Tow Sum程序以及分析
来源:互联网 发布:企业网管软件下载 编辑:程序博客网 时间:2024/06/08 05:16
URL:https://leetcode.com/problems/two-sum/description/
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].
Code:
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { map<int,int> array; vector<int> result; for(int i = 0;i < nums.size(); i ++) { array[nums[i]] = i; } for(int i = 0;i < nums.size(); i ++) { result.push_back(i); int dif = target - nums[i]; if(array.find(dif) != array.end() && i != array[dif]) { result.push_back(array[dif]); return result; } else result.pop_back(); } }};
阅读全文
0 0
- LeetCode:Tow Sum程序以及分析
- LeetCode: Tow Sum (JavaScript)
- LeetCode : Tow Sum [java]
- leetcode-001 tow sum
- [leetcode]Tow Sum(java)
- [LeetCode]--Tow Sum
- leetcode 1. Tow Sum
- leetcode 001 Tow Sum(java)
- LeetCode 1---Tow Sum
- Leetcode系列(#1 Tow Sum)
- 1 Tow Sum
- leetcode: Add Tow Numbers
- leetcode-Add Tow Numbers
- 【面试准备】letcode-Tow Sum
- LeetCodeOJ——1.Tow Sum
- 算法课第一周作业 | tow sum
- 算法第一周Tow Sum[easy]
- Leetcode Combination Sum 分析
- JavaScript内存管理机制以及四种常见的内存泄漏解析
- 博弈——Wizards and Numbers
- Hive 设置map 和 reduce 的个数
- Docker本地仓库管理
- 创始人你具备开发一款产品的能力吗!(需要了解的产品技术坑)
- LeetCode:Tow Sum程序以及分析
- Activity的启动模式
- 装机软件清单
- 理论篇~第七章 数据质量
- 使用query.qrcode.min.js在网页上生成二维码
- Gym 101147.J
- Linux系统管理及系统安全命令
- 软件工程导论第三章作业3.4
- Scrapy输出CSV指定顺序的列表