[Algorithm] 9. Two Sum
Description
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, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Solution
vector<int> twoSum(vector<int>& nums, int target) {
int i,j;
// Loop for the first item in the array.
for ( i=; i<nums.size(); i++){
// Loop for the second item after the first item.
for ( j=i+1; j<nums.size(); j++){
// Check if the sum equals to target.
if( (nums[i] + nums[j]) == target ){
return {i,j};
}
}
}
return {-,-};
}
[Algorithm] 9. Two Sum的更多相关文章
- cdoj 1255 斓少摘苹果 贪心
斓少摘苹果 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1255 Descr ...
- HDU 3507 PrintArticle (单调队列优化)
题意:给出一个数列C,一个数字M,将数列分成若干段,每段的代价为(设这段的数字为k个): dp[i]=min(dp[j]+(sum[i]-sum[j])*(sum[i]-sum[j])+M) 若j1& ...
- Greedy Change
Greedy Change time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 【Rain in ACStar HDU-3340】
·你正从AC星球返回,天又下起凸包雨,只好到线段树下躲雨. ·英文题,述大意: 一个竖直平面的美丽天空,会下凸包雨.凸包雨指的是边数为3~6的多边形,并且每一个它都遵守一个神奇定律,那就是 ...
- 动态规划法(八)最大子数组问题(maximum subarray problem)
问题简介 本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...
- 【LibreOJ】#6299. 「CodePlus 2018 3 月赛」白金元首与克劳德斯
[题意]给出坐标系中n个矩形,类型1的矩形每单位时间向x轴正方向移动1个单位,类型2的矩形向y轴正方向,初始矩形不重叠,一个点被矩形覆盖当且仅当它在矩形内部(不含边界),求$(-\infty ,+\i ...
- 【计算机视觉】stitching_detail算法介绍
已经不负责图像拼接相关工作,有技术问题请自己解决,谢谢. 一.stitching_detail程序运行流程 1.命令行调用程序,输入源图像以及程序的参数 2.特征点检测,判断是使用surf还是orb, ...
- Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树
题目链接: Sum of Medians Time Limit:3000MSMemory Limit:262144KB 问题描述 In one well-known algorithm of find ...
- Algorithm for Maximum Subsequence Sum z
MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...
随机推荐
- 内容原发网站seo不重视2个标签,导致seo效果不如转发网站
采集数据,挖掘观点,小心求证,得出结论 时间经过 今日凌晨,爬虫热点采集,其中第一财经是目标站之一,采集到了 http://www.yicai.com/news/5391233.html 谷歌去年悄然 ...
- LAMP安装问题【已解决】
1.编译不通过提示cannot find mysql header files under /usr/local/mysql解决办法:修改./configuer --with-mysql=/usr/ ...
- ATM网络
ATM是Asynchronous Transfer Mode(ATM)异步传输模式的缩写,是实现B-ISDN的业务的核心技术之一.ATM是以信元为基础的一种分组交换和复用技术
- linux下I2C驱动架构全面分析【转】
本文转载自:http://blog.csdn.net/wangpengqi/article/details/17711165 I2C 概述 I2C是philips提出的外设总线. I2C只有两条线,一 ...
- 洛谷 P2822 [ NOIP 2017 ] 组合数问题 —— 数学
题目:https://www.luogu.org/problemnew/show/P2822 阶乘太大,算不了: 但 k 只有 8 个质因子嘛,暴力60分: #include<iostream& ...
- JQuery操作下拉框
转载自下面的链接,很有用的. http://www.cnblogs.com/yrhua/archive/2012/11/04/2753571.html 要实现这种效果: HTML代码 <scri ...
- 杂项-Java:MyBatis
ylbtech-杂项-Java:MyBatis 1.返回顶部 1. MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundatio ...
- 设计模式 | 组合模式(composite)
定义: 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性. 结构:(书中图,侵删) 一个Component接口:定义统一的方法 若干树枝(Com ...
- LOGO免费在线设计
http://www.logomaker.com.cn/ 藏经阁技术资料分享群二维码
- Redis集群创建和配置
1.检查GCC是否安装,可以看看版本号 gcc -v 安装命令:yum install gcc-c++ 2.安装Ruby和Rubygems 如果有网的话,则通过yum命令进行安装,自动将关联的依赖包全 ...