[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 ...
随机推荐
- 杂项-公司:Sun
ylbtech-杂项-公司:Sun Sun Microsystems是IT及互联网技术服务公司(已被甲骨文收购)Sun Microsystems 创建于1982年.主要产品是工作站及服务器.1986年 ...
- 栗染-Myeclispe连接SQL Server数据库
第一步,在SQL server方面 这里是以身份验证登录. 这里我是建了一个hw的数据库,其他没啥说的. 第二步,最主要的一部分 因为第一次连接SQL Server数据库,所以就不知道还有这一步.不然 ...
- bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会【tarjan】
几乎是板子,求有几个size>1的scc 直接tarjan即可 #include<iostream> #include<cstdio> #include<cstri ...
- ATX 学习 (三)-atxserver2-android-provider
服务端代码 代码clone到本地,搭好相应环境(怎么搭的这里就不介绍了,很好搭的哈)一般库首先查看main.py文件,debug模式开始运行 一开始就是没接触过的tornado.ioloop,有点偏底 ...
- ASP.NET MVC应用程序中支持用户使用腾讯QQ和微信以及新浪微博的第三方登录
什么是第三方授权登录,就是一些大家都会有的帐号如QQ.微信.淘宝.微博等账户.通过那些巨头公司提供的api直接实现登录. 当然,我们是不可能得到你的用户名和密码的.不了解的人,可能会存在这个疑虑.我们 ...
- idea安装插件plugin(主要针对网络连接不上的情况)
https://blog.csdn.net/duoduo1636546/article/details/80104711
- [ TJOI 2010 ] 打扫房间
\(\\\) Description 给出一个\(N\times M\) 的网格,一些格子是污点,求是否能用多个封闭的环路覆盖所有不是污点的格点. 封闭的环路覆盖的含义是,每条路径都必须是一个环,且每 ...
- Position属性四个值:static、fixed、absolute和relative的区别
1.static(静态定位):默认值.没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明). 2.relative(相对定位):生成相对 ...
- sublime 自定义快捷键
[ { "keys": ["alt+space"], "command": "auto_complete" }, // ...
- ubuntu下删除和新建用户(并有su权限)
http://blog.csdn.net/speedme/article/details/8206144ubuntu下删除和新建用户(并有su权限) 如何创建ubuntu新用户?输入:sudo add ...