[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 ...
随机推荐
- 谈谈C++私有继承
很多C++程序猿从来没用过私有继承来设计他的类.的确,假设是本该用私有继承的地方却用了公有继承.对程序的功能的实现并无影响. 但这样的误用是一种错位的描写叙述.会引起阅读者的误解,甚至会引起类的使用者 ...
- Jenkins重启 在Windows GUI上
To restart Jenkins manually, you can use either of the following commands: (jenkins_url)/safeRestart ...
- pgsql数据库应用两点注意
今天在写一个sql脚本时遇到了两个问题,记录一下. 1,pgsql中没有select top n语句,可以用limit n代替. 2,pgsql可以在定义函数存储过程时使用变量,但要注意函数定义中的函 ...
- Excel设定编辑列权限的方法
工具---保护--允许用户编辑区域 --新建-- 选择(或输入)引用单元格 ,区域密码:对不同的人不同的区域用不同的密码,设置完成后,保护工作表(密码用管理员的),即可
- JS动态加载JS
1.直接document.write <script language="javascript"> document.write("<scrip ...
- Ruby和Swift的Range
意义 Swift Ruby [1, 2, 3, 4, 5] 1...5 1..5 [1, 2, 3, 4] 1..<5 1...5 ...
- 台哥原创:java 连连看源码
2010年,迷上了玩连连看 随手就做了这个,正好手头有这些图片素材 游戏启动时,界面先铺上了一层透明幕布,然后这些兵器图片交替从上到下,从左到右出现.. 鼠标停在兵器格子上时,所在格子会有红色 ...
- ASP.NET MVC 导出CSV文件
ASP.NET MVC 导出CSV文件.直接贴代码 /// <summary> /// ASP.NET MVC导出CSV文件Demo1 /// </summary> /// ...
- fread/fwrite实现复制功能
1. fread/fwrite实现复制功能 #include <stdio.h> #include <stdlib.h> #define BUFFSIZE 4096 //执行 ...
- Windows平台下Oracle 11g R2监听文件日志过大,造成客户端无法连接的问题处理
近期部署在生产环境的应用突然无法访问,查看应用日志发现无法获取数据库连接. SystemErr R Caused by: oracle.net.ns.NetException: The Network ...