[LeetCode]Two Sum 【Vector全局指针的使用】

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

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;
    }
};
时间: 2024-12-28 16:10:57

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

[LeetCode] Two Sum [17]

题目 Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note th

Leetcode:Path Sum 二叉树路径和

Path Sum: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return

LeetCode: Combination Sum [038]

[题目] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target)

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

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 sum. 通过一个p指针,遍历 二叉树,并将每次的值 保存在 sum2 中 . 遇到右节点,将右节点+depth 保存在 temp中,当再次使用 该节点时,根据depth 将sum2中的长度削减成 depth-1 /** * Definition for a binary tree node. * st

Leetcode:Combination Sum 子集和问题

Combination Sum: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includ

LeetCode: Path Sum II [113]

[题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] [题意] 判断二叉树中是否存在一条从根到

LeetCode: Two Sum 题解

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that

Leetcode | Combination Sum I &amp;&amp; II

Combination Sum I Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note:All numbers (includ