leetcode 2SUM
struct vp{
int value;
int place;
};
bool comp(const struct vp a, const struct vp b){
return a.value<b.value;
}
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<struct vp> v ;
for(int i = ; i < numbers.size(); ++i){
struct vp tmp;
tmp.value = numbers[i];
tmp.place = i;
v.push_back(tmp);
}
sort(v.begin(), v.end(),comp);
for(int i = ; i < v.size(); i++){
for(int j = i+; j < v.size(); j++){
if(v[i].value + v[j].value > target){
break;
}
if(v[i].value + v[j].value < target){
continue;
}
if(v[i].value + v[j].value == target){
vector<int> t ;
t.push_back(v[i].place+);
t.push_back(v[j].place+);
sort(t.begin(),t.end());
return t;
}
}
}
return numbers;
}
};
leetcode 2SUM的更多相关文章
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- k sum 问题系列
转自:http://tech-wonderland.net/blog/summary-of-ksum-problems.html (中文旧版)前言: 做过leetcode的人都知道, 里面有2sum, ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
- LeetCode 3Sum Closest 最近似的3sum(2sum方法)
题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- Leetcode OJ 刷题
Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- LeetCode 259. 3Sum Smaller (三数之和较小值) $
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
随机推荐
- Elasticsearch集群 管理
第7章 深入Elasticsearch集群 启动一个Elasticsearch节点时,该节点会开始寻找具有相同集群名字并且可见的主节点.如 果找到主节点,该节点加入一个已经组成了的集群:如果没有找到, ...
- tomcat和jboss的区别
1. Tomcat是Apache鼎力支持的Java Web应用服务器(注:servlet容器),由于它优秀的稳定性以及丰富的文档资料,广泛的使用人群,从而在开源领域受到最广泛的青睐. 2. Jboss ...
- 贪玩ML系列之一个BP玩一天
手写串行BP算法,可调batch_size 既要:1.输入层f(x)=x 隐藏层sigmoid 输出层f(x)=x 2.run函数实现单条数据的一次前馈 3.train函数读入所有数据for循环处理 ...
- scrapy spider
spider 定义:在spiders文件夹中由用户自定义,继承scrapy.Spider类或其子类 Spider并没有提供什么特殊的功能. 其仅仅请求给定的 start_urls/start_requ ...
- rest_framework 响应器
一 作用 根据 用户请求URL 或 用户可接受的类型,筛选出合适的 渲染组件.用户请求URL: http://127.0.0.1:8000/test/?format=json http:/ ...
- Keras网络层之常用层Core
常用层 常用层对应于core模块,core内部定义了一系列常用的网络层,包括全连接.激活层等 Dense层 keras.layers.core.Dense(units, activation=None ...
- 在Docker上安装配置Oracle教程
地址:https://github.com/wnameless/docker-oracle-xe-11g Docker shell 下: docker pull wnameless/oracle-xe ...
- Maven学习笔记—私服(包含maven的setting.xml配置)
为什么要用远程仓库(私服) 如果没有私服,我们所需的所有构件都需要通过maven的中央仓库和第三方的maven仓库下载到本地,而一个团队中的所有人都重复的从maven仓库下载构件,这样就加大了中央仓库 ...
- C# 创建单例你会几种方式?
关于为什么需要创建单例?这里不过多介绍,具体百度知. 关于C# 创建单例步骤或条件吧 1.声明静态变量:2.私有构造函数(无法实例化)3.静态创建实例的方法:至于我这里的Singleton是seal ...
- java执行cmd命令和linux命令
文章出处http://blog.csdn.net/xh16319/article/details/17302947 一:window下执行cmd指定 一:window下执行cmd指定 程序例子: [j ...