LeeCode-Two Sum
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的更多相关文章
- LeeCode 1-Two Sum
Two Sum Total Accepted: 125096 Total Submissions: 705262 Question Solution Given an array of integer ...
- leecode系列--Two Sum
学习这件事在任何时间都不能停下.准备坚持刷leecode来提高自己,也会把自己的解答过程记录下来,希望能进步. Two Sum Given an array of integers, return i ...
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
- 树中是否存在路径和为 sum leecode java
https://oj.leetcode.com/problems/path-sum/ /** * Definition for binary tree * public class TreeNode ...
- 算法题思路总结和leecode继续历程
2018-05-03 刷了牛客网的题目:总结思路(总的思路跟数学一样就是化简和转化) 具体启发点: 1.对数据进行预处理排序的思想:比如8皇后问题 2.对一个数组元素进行比较的操作,如果复杂,可以试试 ...
- leecode刷题(8)-- 两数之和
leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输 ...
- LeeCode(No2 - Add Two Numbers)
LeeCode是一个有意思的编程网站,主要考察程序员的算法 第二题: You are given two non-empty linked lists representing two non-neg ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
随机推荐
- (DP)House Robber
题目: You are a professional robber planning to rob houses along a street. Each house has a certain am ...
- Pythoner | 你像从前一样的Python学习笔记
Pythoner | 你像从前一样的Python学习笔记 Pythoner
- vs2008如何创建DLL和使用DLL
一 动态库的编译 文件->新建->项目 选择下一步:然后在应用程序类型里选择DLL 在test项目的头文件里加上test.h;并添加下列代码 在test.cpp里增加如下代码 然后F7编译 ...
- C++按值和按址传递对象的思考和优化
C++是一门面向对象(OOP)编程语言,在这门语言中也有函数,函数的参数可以是变量数值,当然也可以是对象.所以,传统地就有关于对象是按值传递还是按址传递的讨论. 在C语言中,按值传递在很多情况下可以出 ...
- Direct3D 纹理映射
纹理映射是将2D的图片映射到一个3D物体上面,物体上漂亮图案被称为纹理贴图, 一个表面可以支持多张贴图等等,下面简单介绍下纹理贴图 纹理贴图UV: 贴图是一个个像素点组成,每一个像素点都由一个坐标最后 ...
- InnoDB和MyISAM存储引擎的区别
在MySQL数据库的使用过程中我们经常会听到存储引擎这个名词.MySQL的存储引擎有好多种如InnoDB.MyISAM.Memory.NDB等等,多存储引擎也是MySQL数据库的特色. InnoDB和 ...
- jqGrid源代码分析(一)
废话少说.先上grid.base.js 整体结构图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3B5MTk4ODEyMDE=/font/5a6L5L2 ...
- Ajax发送Post请求
Ajax发送post请求与发送get请求大致类似.以下看详细实例.首先看JSP显示页面: <form action="servlet/LoginServlet" method ...
- SQLLoader7(只导入数据文件的其中几行记录)
数据文件: D:\oracletest\test1.txt SMITH CLERK ALLEN SALESMAN WARD SALESMAN JONES MANAGER MARTIN SALESMAN ...
- javascript的本地存储 cookies、localStorage
一.cookies 本地存储 首先是常用的cookies方法,网上有很多相关的代码以及w3cSchool cookies. // 存储cookies function setCookie(name,v ...