Easy 1 Two Sum(1)
来源:互联网 发布:大数据分析预测 知乎 编辑:程序博客网 时间:2024/06/05 17:55
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]
Solution(c++)
给的nums是个无序的状态,所以用hash查找效率可以。(unordered_map的使用)
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> res(2); unordered_map<int,int> hash; int length=nums.size(); for (int i=0;i<length;i++) { int NumToFind=target-nums[i]; if(hash.find(NumToFind)!=hash.end()){ res[0]=hash[NumToFind]; res[1]=i; return res; } hash[nums[i]]=i; } }};
0 0
- #1 Two Sum(Easy)
- Easy 1 Two Sum(1)
- Leetcode #1 Two Sum (easy)
- Leetcode #1 - Two Sum - Easy
- Easy 1题 Two Sum
- Two Sum(easy)
- Easy-题目68:1. Two Sum(增补1)
- LeetCode OJ 1 Two Sum [Easy]
- week1-leetcode #1-Two Sum[Easy]
- LeetCode-Easy刷题(1) Two Sum
- LeetCode 第1题 Two sum (easy)——python
- Leetcode[Easy] - Two Sum
- 【Leetcode】Two sum(easy)
- Two Sum(easy)
- 【Easy】1. Two Sum
- leetcode Two Sum easy
- [Easy]Two Sum
- 1. Two Sum easy
- 进程及进程控制块
- (八)ROS创建点云数据并在rviz中显示
- 一致性Hash算法的实现
- 科锐课堂笔记:2017/4/14 重载new、delete和引用计数
- 什么是css hack?
- Easy 1 Two Sum(1)
- 欢迎使用CSDN-markdown编辑器
- POJ 1228 (稳定凸包问题)
- 动态规划之最长公共子序列(LCS)
- STM32外部中断
- 实验吧——WriteUp&&涨姿势(1)
- js小案类
- 随机数
- Dubbo学习(一)入门