题目:

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的更多相关文章

  1. 【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 ...

  2. 【LeetCode OJ】Path Sum

    Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...

  3. 【LeetCode练习题】Combination Sum

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  4. 【Leetcode 167】Two Sum II - Input array is sorted

    问题描述:给出一个升序排列好的整数数组,找出2个数,它们的和等于目标数.返回这两个数的下标(从1开始),其中第1个下标比第2个下标小. Input: numbers={2, 7, 11, 15}, t ...

  5. 【LeetCode OJ】Two Sum

    题目:Given an array of integers, find two numbers such that they add up to a specific target number. T ...

  6. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  7. 【LeetCode题解】136_只出现一次的数字

    目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...

  8. 【LeetCode题解】2_两数相加

    目录 [LeetCode题解]2_两数相加 描述 方法一:小学数学 思路 Java 代码(非递归写法) Java 代码(递归写法) Python 代码(非递归写法) [LeetCode题解]2_两数相 ...

  9. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

随机推荐

  1. C和指针 第三章--数据

    简要概述: <C和指针>第三章对数据进行了描述. 其中主要讲解了---变量的三个属性:作用域.链接属性和存储类型. 这三个属性决定了该变量在“什么地方可以使用”以及“该变量的值能够保持多久 ...

  2. 局部加权线性回归(Locally weighted linear regression)

    首先我们来看一个线性回归的问题,在下面的例子中,我们选取不同维度的特征来对我们的数据进行拟合. 对于上面三个图像做如下解释: 选取一个特征,来拟合数据,可以看出来拟合情况并不是很好,有些数据误差还是比 ...

  3. openssh 在32位、64位操作系统上的安装配置

    openssh是安装在windows系统上的提供SSH服务的服务端软件,可以提供安全的命令行远程连接管理.下面介绍下openssh成功安装使用的操作步骤. 工具/原料   openssh window ...

  4. 初学者上传文件到github

    http://blog.csdn.net/steven6977/article/details/10567719 我的github是wzb19960208,怕忘了=.=

  5. 腾讯安全反病毒实验室解读“Wannacry”勒索软件

    背景 针对昨日英国医院被攻击,随后肆虐中国高校的 WannaCry 勒索事件,腾讯安全反病毒实验室第一时间给出了深度权威的分析.此次勒索事件与以往相比最大的亮点在于,勒索病毒结合了蠕虫的方式进行传播, ...

  6. Set8087CW

    Set8087CWThis example accesses the Floating Point Unit (FPU) control register. Try turning floating ...

  7. Java中instanceof和isInstance的具体区别

    Java中instanceof和isInstance的具体区别 在Think in Java泛型这一章遇到这个问题,一些博客模糊提到了isInstance是instanceof的动态实现,查阅文档参考 ...

  8. 网络编程-socket开发

    练习: 1.什么是C/S架构? 2.互联网协议是什么?分别介绍五层协议中每一层的功能? 3.基于tcp协议通信,为何建立链接需要三次握手,而断开链接却需要四次挥手 4.为何基于tcp协议的通信比基于u ...

  9. ”$-”与shell默认选项

    一.前言 之所以整理这篇博客,主要是写Linux环境设置文件 的时候,在查看/etc/profile时看到这么一段代码: for i in /etc/profile.d/*.sh ; do if [ ...

  10. devcloud

    zone名字:devcloud 外网dns:8.8.8.8 内网dns:10.0.2.3   提供点名称:devcloud   cs bug问题:https://issues.apache.org/j ...