题目:

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. Centos下Apache+Tomcat集群--搭建记录

    一.目的 利用apache的mod_jk模块,实现tomcat集群服务器的负载均衡以及会话复制,这里用到了<Cluster>. 二.环境 1.基础:3台主机,系统Centos6.5,4G内 ...

  2. Julia - 数学运算

    算术运算符 算术运算符适用于所有的基本数值类型 +x,一元加法,就是 x 本身 -x,一元减法,x 的相反数 x + y,二元加法,做加法运算 x - y,二元减法,做减法运算 x * y,乘法,做乘 ...

  3. Windows系统版本型号MSDN版、OEM版、RTM版、VOL版区别

    我们常常听说操作系统的MSDN版.OEM版.RTM版.VOL版等等,它们到底是什么意思,有什么不同呢? (一)MSDN (Microsoft Developer Network)版MSDN软件是微软公 ...

  4. 十年Java架构师分享

    看到一篇十年java架构师分享需要掌握的技术点,有时间对照一下,好好学习一下. ------------------------------------------------------------ ...

  5. redis存session问题测试内容

    转至元数据起始   官网,现网由于是双节点,session是存储在redis作为共享的. +1是单节点.目前是存储成文件的 本次的问题根源是 由于 session是存储在redis,所造成的. 所以需 ...

  6. Sass 入门 (一) 安装Sass

    Sass安装 ruby安装 因为sass依赖于ruby环境,所以装sass之前先确认装了ruby.先导官网下载个ruby 在安装的时候,请勾选Add Ruby executables to your ...

  7. C++Primer笔记-----day02

    ====================================================================day02=========================== ...

  8. win7+jdk环境变量配置

    进行java开发,首先要安装jdk,安装了jdk后还要进行环境变量配置:1.下载jdk(http://java.sun.com/javase/downloads/index.jsp),我下载的版本是: ...

  9. 【Rsync项目实战一】备份全网服务器数据

    目录 [Rsync项目实战]备份全网服务器数据 [企业案例] 1.1 环境部署 1.2 开始部署backup服务器:Rsync服务端过程: 1.3 开始部署nfs01服务器:Rsync客户端过程: [ ...

  10. VUE,使用物理引擎Box2D设计类愤怒小鸟的击球游戏--基本架构设置