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 ...
随机推荐
- PC京东登录页分析 curl
w 正确的组合,没有显示新页面的数据. <!doctype html> <html> <head> </head> <?php include(' ...
- php5.4 的 php-fpm 的重启
php 5.3.3以后 源码中已经内嵌了 php-fpm,不用象以前的php版本一样专门打补丁了,只需要在configure的时候添加编译参数即可. 关于php-fpm的编译参数有 –enable-f ...
- TCP原理
1.http://coolshell.cn/articles/11564.html 2.http://coolshell.cn/articles/11609.html 3.一站式学习wireshark ...
- 【我的Android进阶之旅】如何去除ListView中Header View、Footer View中的分割线
最近的项目中给ListView 加入了一个Header View之后,发现Header View的下方也有了分割线,很难看,UI要求将Header View的分割器去掉,好吧.现在就来说一说如何如何去 ...
- yield的表达式形式、面向过程编程(grep -rl 'root' /etc)
一.yield的表达形式 def foo(): print('starting') while True: x=yield None#return 2 print('value :',x) g=foo ...
- Lua(1)
1.the use of functions in table fields is a key ingredient for some advanced uses of Lua, such as mo ...
- 007-shiro与spring web项目整合【一】基础搭建
一.需求 将原来基于url的工程改成使用shiro实现 二.代码 https://github.com/bjlhx15/shiro.git 中的permission_shiro 三.去除原项目拦截器 ...
- 【Navicat连接Oracle数据库】-Navicat连接Oracle数据库设置
1.navicat连接数据配置信息如下图所示: 点击"确定"按钮,进入到软件 按照图中所画的步骤顺序操作,最后重新启动navicat就可. 关于里面的这个文件夹 insta ...
- eslasticsearch操作集锦
索引-index:一个索引就是一个拥有几分相似特征的文档的集合.比如说,你可以有一个客户数据的索引,另一个产品目录的索引,还有一个订单数据的索引.一个索引由一个名字来标识(必须全部是小写字母的),并且 ...
- xcode中全文查询某个中文字
查询所有中文 [^"]*[\u4E00-\u9FA5]+[^"\n]*? 查询某个中文字“中”字 [^"]*[\u4e2d]+[^"\n]*? 中文字转成uni ...