【LeetCode C++】Two Sum
题目:
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 = [, , , ], target = , Because nums[] + nums[] = + = ,
return [, ].
解题:
题目的意思是给定一个一维的数组nums和一个目标值target,查找数组中是否存在两个整数元素的和为目标值target。返回符合条件的整数元素在一维数组中的位置。
遍历法:
时间复杂度:O(n2) Runtime: 760 ms
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> index;for(int i = ; i< nums.size(); i++)
{
for(int j = i+; j< nums.size(); j++)
{
if(target==(nums[i]+nums[j]))
{
index.push_back(i);
index.push_back(j);
return index;
}
}
}
return index;
}
};
运行结果:


遍历法是最耗时的算法,因此需要进一步优化算法。
【LeetCode C++】Two Sum的更多相关文章
- 【LeetCode OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode OJ】Path Sum
Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...
- 【LeetCode练习题】Combination Sum
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【Leetcode 167】Two Sum II - Input array is sorted
问题描述:给出一个升序排列好的整数数组,找出2个数,它们的和等于目标数.返回这两个数的下标(从1开始),其中第1个下标比第2个下标小. Input: numbers={2, 7, 11, 15}, t ...
- 【LeetCode OJ】Two Sum
题目:Given an array of integers, find two numbers such that they add up to a specific target number. T ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 【LeetCode题解】136_只出现一次的数字
目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...
- 【LeetCode题解】2_两数相加
目录 [LeetCode题解]2_两数相加 描述 方法一:小学数学 思路 Java 代码(非递归写法) Java 代码(递归写法) Python 代码(非递归写法) [LeetCode题解]2_两数相 ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
随机推荐
- js控制手机震动
js控制手机震动 <button onclick="vibrate()">震动</button> <script> function vibra ...
- 【Eclipse】开发专题
Eclipse插件安装 参考以下几个网页内容 不同版本Eclipse对JDK版本要求http://blog.csdn.net/kevin_pso/article/details/54971739 Ec ...
- 5.docker学习之容器
容器创建 我们已经知道,镜像是只读的,而基于镜像创建出来的容器是可读写的,所以,一般我们实际中,会经常使用对应镜像创建容器并且使用这些容器.同样,如果我们想要使用容器,那么我们必须首先需要创建容器.而 ...
- 2019 最新 阿里天猫、蚂蚁、钉钉ava 面试题汇总,附答案
Java面试前需要做足各方面的准备工作,肯定都会浏览大量的面试题,本人也不例外,这是一些最新面试题,分享给大家. Java基础 面向对象的特征:继承.封装和多态 int 和 Integer 有什么区别 ...
- leetcode462
public class Solution { public int MinMoves2(int[] nums) { var list = nums.OrderBy(x => x).ToList ...
- 用Python将word文件转换成html(转)
用Python将word文件转换成html 序 最近公司一个客户大大购买了一堆医疗健康方面的科普文章,希望能放到我们正在开发的健康档案管理软件上.客户大大说,要智能推送!要掌握节奏!要深度学习!要 ...
- jquery 三元运算
三元运算: 条件 ? 条件为真取此值 : 条件为假取此值; var v = $(:check).prop('checked')?faule:true; $(:check).prop('checked ...
- delphi IOS 后台状态保存
FormSaveState procedure TFrm.FormSaveState(Sender: TObject);begin end; http://stackoverflow.com/ques ...
- 查看端口占用情况lsof,并关闭对应进程kill
lsof -n -P| grep ":<端口号>" | grep LISTEN #监听对应端口号的进程 lsof -i tcp:<端口号> #和对应端口号有 ...
- 结对作业——四则运算 Part3. 对于结对编程的总结与思考
结对作业——四则运算 Part3. 对于结对编程的总结与思考 PB15061303 刘梓轩PB16061489 艾寅中 GITHUB 地址 戳这里 目录 Part 1. Core代码编写部分 Part ...