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. Jquery局部打印插件

    局部打印插件 jquery.PrintArea.js js代码 (function ($) {     var printAreaCount = 0;     $.fn.printArea = fun ...

  2. hdu1561-The more, The Better(树形dp)

    Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有 ...

  3. html 表单初步学习

    <html> <head> <title> 静态页面</title> </head> <body> 这是一个静态页面<br ...

  4. 笔试、面试重点总结:WIN32、MFC与Linux

    win32 1. Win32应用程序的基本类型. 2. 创建win32窗口程序的几个步骤,及使用到的函数. 3. nmake 与 makefile. 4. 有哪些字符集? Win32对于各种字符集如何 ...

  5. IOS消息推送情况总结

      App没有启动的时候,接受到了消息通知.这个时候操作系统会按默认方式来展示一个alert,在App Icon上标记一个数字 .当程序处于关闭状态收到推送消息时,点击图标或消息栏会调用 - (BOO ...

  6. Maven项目部署方案

    以xbank项目做为应用背景,简单说明一下Maven项目的部署方案: 1.项目说明 xbank基础服务端的11个子项目均采用Maven搭建: 其中各项目功能设计如下: froad-xbank-serv ...

  7. FZU 2102 Solve equation(水,进制转化)&& FZU 2111(贪心,交换使数字最小)

    C Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Pra ...

  8. OMX Codec详细解析

    概述 OMX Codec是stagefrightplayer中负责解码的模块. 由于遵循openmax接口规范,因此结构稍微有点负责,这里就依照awesomeplayer中的调用顺序来介绍. 主要分如 ...

  9. TortoiseSVN和VisualSVN-下载地址

    isualSVN的下载地址http://www.visualsvn.com/visualsvn/ 它可以以插件的形式嵌入到visual studio里面,让团队协作更轻松,最新的版本已经支持Visua ...

  10. C#winform修改IP,dns

     /// 将IP,DNS设置为自动获取        ///        private void setDHCP()        {            string  _doscmd = & ...