[TwoPointers][2]Lintcode587. Two Sum - Unique pairs

本文介绍了一种算法问题——寻找数组中和为目标值的唯一数对数量。通过两个指针法解决该问题,并考虑了重复元素的去重处理。提供了完整的Java实现代码。

Amazon | OA 2019 | Two Sum - Unique Pairs

Given an int array nums and an int target, find how many unique pairs in the array such that their sum is equal to target. Return the number of pairs.

Example 1:
Input: nums = [1, 1, 2, 45, 46, 46], target = 47
Output: 2
Explanation:
1 + 46 = 47
2 + 45 = 47
Example 2:
Input: nums = [1, 1], target = 2
Output: 1
Explanation:
1 + 1 = 2
Example 3:
Input: nums = [1, 5, 1, 5], target = 6
Output: 1
Explanation:
[1, 5] and [5, 1] are considered the same.

Related problems:

  • https://leetcode.com/problems/two-sum
  • https://leetcode.com/problems/two-sum-ii-input-array-is-sorted
Solution1: TwoPointers

1.注意重复元素的去重
注意去重的方法;
如果用nums[start] == nums[start+1] start ++, 如果nums[start+1]还是一个重复的元素,nums[start+2]不是重复的元素,那么这种方法就不能去掉nums[start+1]。

public class Solution {
    /**
     * @param nums: an array of integer
     * @param target: An integer
     * @return: An integer
     */
    public int twoSum6(int[] nums, int target) {
        // write your code here
        
        if(nums == null || nums.length < 2) return 0;
        
        Arrays.sort(nums);
        
        int start = 0, end = nums.length -1;
        int sum = 0;
        
        while(start < end) {

            if(nums[start] + nums[end] == target){
                ++ sum;
                ++ start;
                -- end;
                while(start < end && nums[start] == nums[start - 1] ){
                    ++ start;
                }
                
                while(start < end && nums[end] == nums[end + 1] ){
                -- end;
                }
            }
            else if(nums[start] + nums[end] > target){
                -- end;
                
            }else {
                ++ start;
            }
            
        }
        
        return sum;
    }
}
用中文思考:G. Tree Parking time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input output: standard outputConsider the following problem statement: - You are given a tree with $n$ vertices rooted at $1$. For each $1 \leq i \leq n$, a car will enter the root at time $l_i$. It will then instantaneously travel from the root to vertex $i$ through the unique simple path and park there. It will leave through the same path in the other direction at time $r_i$. During the time when a car is parked in a vertex, it blocks other cars from traveling through that vertex. The tree is called valid if and only if all cars are able to enter and leave the tree at their desired times. Count the number of pairs of sequences $l$, $r$ such that $l_i < r_i$, their concatenation is a permutation of $1 \ldots 2n$, and the tree is valid. Calculate the sum of the answers to the problem over all labeled trees$^{\text{∗}}$ with $n$ vertices and $k$ leaves. The root is not considered a leaf. Since the answer may be large, calculate it modulo $998\,244\,353$. $^{\text{∗}}$Two labeled trees are considered different if and only if their edge sets are different.**Input** Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The only line of each test case contains two integers $n$, $k$ ($1 \leq k < n \leq 2 \cdot 10^5$) — the number of vertices and leaves of the tree, respectively. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.**Output** For each test case, output one integer — the answer modulo $998\,244\,353$.InputCopy 3 2 1 8 3 65 43 OutputCopy 3 899171636 38330886**Note** In the first case, there is only one tree that satisfies the constraints. The correct pairs of sequences are: $$ (l, r) = \bigl([1, 3], [2, 4]\bigr),\:\bigl([3, 1], [4, 2]\bigr),\:\bigl([2, 1], [3, 4]\bigr). $$ C++代码,用中文思考
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值