• 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:

    • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
    • 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)


    分析:先对数组进行排序,先固定第一个数,然后两边夹逼。特别要注意的地方是 如何排除相同的结果

    进行排序后,连续的几个数字可能相同。因此,要略过相同的数字进行检查。
    Java: run time 28ms, memory usage 44.3MB
     1 class Solution {
     2     public List<List<Integer>> threeSum(int[] nums) {
     3         List<List<Integer> > result = new ArrayList<>();
     4         int n = nums.length;
     5         int a = 0;
     6         Arrays.sort(nums);
     7         while (a < n - 2 && nums[a] <= 0) { // eliminate unnecessary checks
     8             if(a != 0 && nums[a] == nums[a - 1]) {
     9                 a++;
    10                 continue;
    11             }
    12             int b = a + 1;
    13             int c = n - 1;
    14             while(b < c) {
    15                 int currentSum = nums[a] + nums[b] + nums[c];
    16                 if(currentSum == 0) {
    17                     if(b != a + 1 && nums[b] == nums[b - 1]) {
    18                         b++;
    19                     } else if (c != n - 1 && nums[c] == nums[c + 1]) {
    20                         c--;
    21                     } else {
    22                         result.add(Arrays.asList(new Integer[] {nums[a], nums[b], nums[c]}));
    23                         b++;
    24                         c--;
    25                     }
    26                 } else if (currentSum > 0) {
    27                     c--;
    28                 } else {
    29                     b++;
    30                 }
    31             }
    32             a++;
    33         }
    34         
    35         return result;
    36     }
    37 }
    
    
    


    C++: 运行时间65ms
     1 class Solution {
     2 public:
     3     vector<vector<int> > threeSum(vector<int>& nums) {
     4         vector<vector<int> > result;
     5         if(nums.size() < 3) return result;
     6         sort(nums.begin(), nums.end());
     7         
     8         for(int a = 0; a < nums.size() - 2; a++){
     9             if(a != 0 && nums[a] == nums[a-1]) continue;
    10             int b = a + 1, c = nums.size() - 1;
    11             while(b < c){
    12                 if(nums[a] + nums[b] + nums[c] > 0) c--;
    13                 else if(nums[a] + nums[b] + nums[c] < 0) b++;
    14                 else{
    15                     if(b != a + 1 && nums[b] == nums[b-1]) b++;
    16                     else if(c != nums.size() - 1 && nums[c] == nums[c+1]) c--;
    17                     else{
    18                         result.push_back({nums[a], nums[b], nums[c]});
    19                         b++;
    20                         c--;
    21                     }
    22                 }
    23             }
    24         }
    25         sort(result.begin(), result.end());
    26         return result;
    27     }
    28 };
    
    
    
     
  • 相关阅读:
    通过HTTP发包工具了解HTTP协议
    Oracle之数据库安全
    SQL注入深入剖析
    apache中如何调用CGI脚本
    fastcgi php-cgi与php-fpm区别和之间的关系
    使用PHPExcel实现Excel文件的导入和导出(模板导出)
    学会数据库读写分离、分表分库
    框架Thinkphp5 简单的实现行为 钩子 Hook
    php文件下载
    PHP为JSON数据的API返回空数组或者空对象
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4494342.html
Copyright © 2020-2023  润新知