无序数组返回两个元素和为给定值的下标。

tricks:无序、返回下标增序、返回的是原始数组的下标。

vector<int>*pa;
bool cmp(int x,int y){
return (*pa)[x]<(*pa)[y];
}
class Solution {
public:
vector<int> twoSum(vector<int> &a, int t) {
int n=a.size();
pa=&a;
int* id=new int[n],i=0,j=n-1;
for(i=0;i<n;++i){
id[i]=i;
}
sort(id,id+n,cmp);
for(i=0,j=n-1;i<j;){
if(a[id[i]]+a[id[j]]==t)break;
else if(a[id[i]]+a[id[j]]<t)
++i;
else --j;
}
vector<int>v;
if(id[i]<id[j]){
v.push_back(id[i]+1);v.push_back(1+id[j]);
}
else {v.push_back(1+id[j]);
v.push_back(id[i]+1);
}
delete[]id;
return v;
}
};

[LeetCode]Two Sum 【Vector全局指针的使用】的更多相关文章

  1. [LeetCode] Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  2. leetcode: Path Sum II 迭代法

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  3. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  4. LeetCode OJ--Path Sum II **

    https://oj.leetcode.com/problems/path-sum-ii/ 树的深搜,从根到叶子,并记录符合条件的路径. 注意参数的传递,是否需要使用引用. #include < ...

  5. 【leetcode】Sum Root to Leaf Numbers(hard)

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  6. [LeetCode] Combination Sum (bfs)

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  7. LeetCode Path Sum II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  8. [leetcode] Path sum路径之和

    要求给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径.比如下图中,给定路径之和为22,存在路径<5,4,11,2>,因此返回true;否则返回false. 5 / \ 4 8 / ...

  9. [LeetCode] Range Sum Query - Mutable 题解

    题目 题目 思路 一看就是单点更新和区间求和,故用线段树做. 一开始没搞清楚,题目给定的i是从0开始还是从1开始,还以为是从1开始,导致后面把下标都改掉了,还有用区间更新的代码去实现单点更新,虽然两者 ...

随机推荐

  1. (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)

    openssl  rsa      是RSA对称密钥的处理工具 openssl  pkey   是通用非对称密钥处理工具,它们用法基本一致,所以只举例说明openssl rsa. 它们的用法很简单,基 ...

  2. bash实现自动补全

    yum install -y bash-completion source /usr/share/bash-completion/bash_completion 执行后yum拥有选项自动补全功能 对于 ...

  3. 利用system-config-kickstart实现半自动化安装

    老司机开车了… 上车请坐稳… centos7系统 首先确认已经安装了system-config-kickstart包,如果没有安装就yum install system-config-kickstar ...

  4. js实现复制粘贴功能

    在项目中使用到复制粘贴功能,虽然网上有很多大牛封装了很多的插件,但是还是想不去使用插件,就像自己来实现这个功能. 另一篇是禁止复制粘贴 前端er怎样操作剪切复制以及禁止复制+破解等 初步想法: 1. ...

  5. SONP 是什么

    JSONP 是什么 说实话,我学了这么久,其实也没有好好了解这个东西,当然平常自己在前端方面也涉猎较浅. 1) jsonp 是什么 JSONP(JSON with Padding)是JSON的一种&q ...

  6. Android 图片设置圆角 方法之二

    Android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片.接下来我们再介绍一种方法. 首先, 自定义ImageView: android:id="@+id/iv" ...

  7. LeetCode(12)Integer to Roman

    题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  8. Java比较两个数组中的元素是否相同的最简单方法

    import java.util.Arrays; public class Test { /** * Java比较两个数组中的元素是否相同 */ public static void main(Str ...

  9. ul标签中,li标签的移除、属性值获取

  10. 使用cURL和用户名和密码?

    问题描述 我想访问一个需要用户名/密码的URL.我想尝试用 curl 来访问它.现在我正在做一些事情: curl http://api.somesite.com/test/blah?something ...