Leetcode Week2 Two Sum
Question
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].
Answer
思路: 集合中每两个元素相加与target比较一次即可。
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for (int i = ; i < nums.size(); i++) {
for (int j = i+; j < nums.size(); j++){
if (i == j)
continue;
if (nums[i] + nums[j] == target) {
vector<int> vec;
vec.push_back(i);
vec.push_back(j);
return vec;
}
}
}
}
};
Leetcode Week2 Two Sum的更多相关文章
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- css实现文字过长显示省略号的方法
<div class="title">当对象内文本溢出时显示省略标记</div> 这是一个例子,其实我们只需要显示如下长度: css实现网页中文字过长截取. ...
- cache和内存屏障
1 cache简介 1.1 cache缓存映射规则 tag查看cache是否匹配,set index |tag |set index |block offset ||20-bit |7-bit |5b ...
- Python3标准库:itertools迭代器函数
1. itertools迭代器函数 itertools包括一组用于处理序列数据集的函数.这个模块提供的函数是受函数式编程语言(如Clojure.Haskell.APL和SML)中类似特性的启发.其目的 ...
- Android Studio 快捷方式记录
- 【python数据挖掘】爬取豆瓣影评数据
概述: 爬取豆瓣影评数据步骤: 1.获取网页请求 2.解析获取的网页 3.提速数据 4.保存文件 源代码: # 1.导入需要的库 import urllib.request from bs4 impo ...
- Linux系统的安装和常用命令
(1)切换到目录 /usr/bin: (2)查看目录/usr/local 下所有的文件: (3)进入/usr 目录,创建一个名为 test 的目录,并查看有多少目录存在: (4)在/usr 下新建目录 ...
- 五种编程语言解释数据结构与算法——顺序表3(JavaScript与Python语言实现)
7.JavaScript语言实现 7.1.用ES6语法编写顺序表类 //1.创建类 class MyList { //1. initList(&L):初始化表.构造一个空的线性表.放回值应该是 ...
- 解决 Windows 编译 Fast R-CNN 的 bbox 和 nms 出现的错误 error: Unable to find vcvarsall.bat
在 Windows 下安装一个底层的 Python 包时(Fast R-CNN 的 bbox 和 nms),遇到 error: Unable to find vcvarsall.bat 错误,看到这个 ...
- vue(六)--计算属性(computed)
计算属性关键词: computed demo1: <div id="app"> <p>原始字符串: {{ message }}</p> < ...
- 数据结构(集合)学习之Set
集合 框架关系图: Collection接口下面有三个子接口:List.Set.Queue.此篇是关于Set<E>的简单学习总结. 补充:HashTable父类是Dictionary,不是 ...