zoukankan      html  css  js  c++  java
  • 20. 三数和为0

    有一个长度为n的整型数组arr,从中选出3个数a,b,c,使得a+b+c=0,找出所有满足条件的三元组(a,b,c)。重复的三元组只算一次。

    样例:数组[2, 1, -3, -3, 0, -1],满足条件的三元组有2个:(2, 1, -3)和(1, 0, -1)。

    提示:本题存在空间复杂度O(1)的算法(不使用任何辅助空间),没必要利用set等数据结构消除重复的三元组。

    不知道咋实现LIST的三元数组啊……找了找,没找到。只能用蠢办法了。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication16
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] input = { 2, 1, -3, -3, 0, -1 };
                int[,] result = FindThreeNumSumZero2(input);
                for (int i = 0; i < result.GetLength(0); i++)
                {
                    for (int j = 0; j < result.GetLength(1); j++)
                    {
                        Console.Write(result[i, j] + ",");
                    }
                    Console.WriteLine();
                }
            }
    
            static int[,] FindThreeNumSumZero(int[] input)
            {
                if (input.Length < 2 || input == null)
                {
                    throw new Exception("input must be greater than 2.");
                }
    
                int[,] result = new int[input.Length, 3];
                int x = 0;
                for (int i = 0; i < input.Length; i++)
                {
                    for (int j = i + 1; j < input.Length; j++)
                    {
                        for (int k = j + 1; k < input.Length; k++)
                        {
                            if (input[i] + input[j] + input[k] == 0)
                            {
                                result[x, 0] = input[i];
                                result[x, 1] = input[j];
                                result[x, 2] = input[k];
                                x++;
                            }
                        }
                    }
                }
                return result;
            }
    
            static int[,] FindThreeNumSumZero2(int[] input)
            {
                if (input.Length < 2 || input == null)
                {
                    throw new Exception("input length must be greater than 2");
                }
    
                Array.Sort(input);
                int[,] results = new int[input.Length, 3];
                int n = input.Length;
                int c = 0;
                for (int i = 0; i < n - 2; i++)
                {
                    int j = i + 1;
                    int k = n - 1;
                    while (j < k)
                    {
                        int x = input[i] + input[j] + input[k];
                        if (x > 0)
                        {
                            k--;
                        }
                        if (x < 0)
                        {
                            j++;
                        }
                        if (x == 0)
                        {
                            results[c, 0] = input[i];
                            results[c, 1] = input[j];
                            results[c, 2] = input[k];
                            c++;
                            k--;
                            j++;
                        }
                    }
                }
                return results;
            }
        }
    }
    View Code

      

  • 相关阅读:
    在ASP.Net和IIS中删除不必要的HTTP响应头
    Json对象与Json字符串互转
    Jquery ajax传递复杂参数给WebService
    HTTP的KeepAlive是开启还是关闭?
    MQ产品比较-ActiveMQ-RocketMQ
    RocketMQ(7)——通信协议
    mq使用经验
    mq
    RocketMQ
    发送短信验证码实现方案
  • 原文地址:https://www.cnblogs.com/Ligeance/p/3544560.html
Copyright © 2011-2022 走看看