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

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

 class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<int>::iterator it,jt;
int index1,index2;
vector<int> OutputIndex;
vector<int>::size_type number_size=numbers.size();
int Judge=;
for(index1=,it=numbers.begin()-;it<numbers.end()-;index1++,it++)
{
for(index2=+index1,jt=numbers.begin();jt<numbers.end()-;index2++,jt++)
{
if((*jt+*it)==target)
{
Judge=;
break;
}
} if(Judge==)
break; }
OutputIndex.push_back(index1);
OutputIndex.push_back(index2); return OutputIndex;
}
};

LeeCode-Two Sum的更多相关文章

  1. LeeCode 1-Two Sum

    Two Sum Total Accepted: 125096 Total Submissions: 705262 Question Solution Given an array of integer ...

  2. leecode系列--Two Sum

    学习这件事在任何时间都不能停下.准备坚持刷leecode来提高自己,也会把自己的解答过程记录下来,希望能进步. Two Sum Given an array of integers, return i ...

  3. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...

  4. 树中是否存在路径和为 sum leecode java

    https://oj.leetcode.com/problems/path-sum/ /** * Definition for binary tree * public class TreeNode ...

  5. 算法题思路总结和leecode继续历程

    2018-05-03 刷了牛客网的题目:总结思路(总的思路跟数学一样就是化简和转化) 具体启发点: 1.对数据进行预处理排序的思想:比如8皇后问题 2.对一个数组元素进行比较的操作,如果复杂,可以试试 ...

  6. leecode刷题(8)-- 两数之和

    leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输 ...

  7. LeeCode(No2 - Add Two Numbers)

    LeeCode是一个有意思的编程网站,主要考察程序员的算法 第二题: You are given two non-empty linked lists representing two non-neg ...

  8. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  9. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  10. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

随机推荐

  1. 3 Sum 解答

    Question Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Fi ...

  2. UVA 10594-Date Flow(无向图的最小费用网络流+题目给的数据有误)

    题意:给一个有N个点的无向图,要求从1向N传送一定的数据,每条边的容量是一定的,如果能做到,输出最小的费用,否则输出Impossible. 解析:由于是无向图,所以每个有连接的两个点要建4条边,分别是 ...

  3. 【jquery插件】-网页下雪效果

        又到了年底,从圣诞节开始,可以发现各大网站的活动是越来越多,都变的银装素裹,本人由于业务需求也需要做这么一个效果,所以就通过google大神找了一下相关资源,结果就出现了N种雪花效果的方式.种 ...

  4. linux使用mysql的命令

    1.连接到mysql服务器的命令 mysql -h 服务器主机地址 -u 用户名 -p 用户密码 例:mysql -h 192.168.1.1 -u root -p   //指定服务器的主机地址和用户 ...

  5. Unity 开发游戏Game分辨率设置

    最近自己开发小游戏,突然又被Game视图中设置分辨率被诱惑了, 我到底该怎么设置分辨率设置的图片才能让电脑和手机尺寸显示的大小一模一样呢? 然后又被手机尺寸和分辨率迷惑了! =.= 越搞越混   分辨 ...

  6. DIV + CSS 盒子模型

    盒子模型有两种,分别是 IE 盒子模型和标准 W3C 盒子模型.他们对盒子模型的解释各不相同, 先来看看我们熟悉的标准盒子模型: 图片看不清楚?请点击这里查看原图(大图). 从上图可以看到标准 W3C ...

  7. Guava源码分析——ServiceManager

    ServiceManager类:      用于监控服务集的管理器,该类提供了诸如startAsync.stopAsync.servicesByState方法来运行.结束和检查服务集,而且,通过监听器 ...

  8. Android应用程序键盘(Keyboard)消息处理机制分析

    在Android系统中,键盘按键事件是由WindowManagerService服务来管理的,然后再以消息的形 式来分发给应用程序处理,不过和普通消息不一样,它是由硬件中断触发的:在上一篇文章< ...

  9. SlipButton——滑动开关

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjI1MjUwMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  10. ServiceAccount 枚举

    指定服务的安全上下文,安全上下文定义其登录类型. 命名空间:  System.ServiceProcess程序集:  System.ServiceProcess(在 System.ServicePro ...