3Sum:带重复数组中取三个元素求和为零

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
思路:第一反应深度优先遍历,但是有重复元素,所以需要排序后每次从非重复元素开始,以其之后的范围的元素为窗口扫描,找出满足题意的三个元素。

时间复杂度:O(N^2)

class Solution {  
    public List<List<Integer>> threeSum(int[] nums) {        
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(nums);
        for(int a = 0;a<nums.length-2;a++){
            if(a==0||nums[a]!=nums[a-1]){//去重
                int sum = nums[a];
                int l = a+1;
                int r = nums.length-1;
                while(l<r){
                    if((sum+nums[l]+nums[r])==0){
                        list.add(new ArrayList<Integer>(Arrays.asList(nums[a],nums[l],nums[r])));
                        while(l<r&&nums[l]==nums[l+1])l++;
                        while(l<r&&nums[r]==nums[r-1])r--;
                        l++;//多挪一个位置,彻底放弃重复元素
                        r--;
                    }else if((sum+nums[l]+nums[r])>0){
                        r--;
                    }else{
                        l++;
                    }
                }
            }            
        }
        return list;
    }
}





这是一道程序设计题目,需要编写代码实现要求。以下是一个可能的代码实现,供参考: ``` #include <iostream> #include <string> using namespace std; template<typename T> class MyArray { private: T* data; int size; public: MyArray(T* data, int size) { this->data = data; this->size = size; } T sum() { T result = 0; for (int i = 0; i < size; i++) { result += data[i]; } return result; } int search(T value) { for (int i = 0; i < size; i++) { if (data[i] == value) { return i; } } return -1; } }; int main() { int n1, n2, n3; cin >> n1; int* a = new int[n1]; for (int i = 0; i < n1; i++) { cin >> a[i]; } MyArray<int> array1(a, n1); int value1; cin >> value1; int pos1 = array1.search(value1); if (pos1 == -1) { cout << "未找到该元素" << endl; } else { cout << "是第" << pos1 + 1 << "个元素" << endl; } cout << "数组和为:" << array1.sum() << endl; cin >> n2; double* b = new double[n2]; for (int i = 0; i < n2; i++) { cin >> b[i]; } MyArray<double> array2(b, n2); double value2; cin >> value2; int pos2 = array2.search(value2); if (pos2 == -1) { cout << "未找到该元素" << endl; } else { cout << "是第" << pos2 + 1 << "个元素" << endl; } cout << "数组和为:" << array2.sum() << endl; cin >> n3; string* c = new string[n3]; for (int i = 0; i < n3; i++) { cin >> c[i]; } MyArray<string> array3(c, n3); string value3; cin >> value3; int pos3 = array3.search(value3); if (pos3 == -1) { cout << "未找到该元素" << endl; } else { cout << "是第" << pos3 + 1 << "个元素" << endl; } cout << "数组和为:" << array3.sum() << endl; delete[] a; delete[] b; delete[] c; return 0; } ``` 此代码中,我们首先定义了一个类模板 `MyArray`,其中包含了查找和求和数组元素的功能。在主函数中,我们实例化了个 `MyArray` 对象,分别对应一个 `int` 类型的数组、一个 `double` 类型的数组和一个 `string` 类型的数组。我们先输入个数组的长度和各自的元素值,然后对每个数组依次调用相应的成员函数完成题目要求,并输出结果。 需要注意的是,在实现 `MyArray` 类的成员函数时,我们在求和函数中使用了模板类型参数 `T`,因此可以适用于任何类型的数组。在搜索函数中,如果找到了目标元素,我们返回其索引;否则,返回 `-1` 表示未找到。在输出结果时,需要注意要输出“未找到该元素”和“是第X个元素”两种情况的不同输出格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值