C# LeetCode:三个和(不能隐式转换类型)

C# LeetCode:三个和(不能隐式转换类型),c#,c,c++,java,python,javascript,go,rust,C#,C,C++,Java,Python,Javascript,Go,Rust,给定n个整数的数组num,num中是否有元素a、b、c,使得a+b+c=0?查找数组中所有唯一的三元组,该数组的和为零 注: 解决方案集不得包含重复的三元组 例如: Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] 公共类解决方案{ 公共IList三和(int[]nums){ IList solutionList=新列表(); 列表子列表=新列表(); in

给定n个整数的数组num,num中是否有元素a、b、c,使得a+b+c=0?查找数组中所有唯一的三元组,该数组的和为零

注:

解决方案集不得包含重复的三元组

例如:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
公共类解决方案{
公共IList三和(int[]nums){
IList solutionList=新列表();
列表子列表=新列表();
int checkNum,solCounter=0;
圣殿骑士名单;

对于(int z=0;z不确定您的错误,但似乎没有找到最佳解决方案。此解决方案是
O(N^2)
,将被接受。我不认为
O(N^3)
算法会被接受用于此问题,我可能错了:

public class Solution {
    public IList<IList<int>> ThreeSum(int[] nums) {
        IList<IList<int>> res = new List<IList<int>>();

        if (nums.Length < 3) {
            return res;
        }

        
        int start = 0;
        int lo;
        int hi;
        int target;
        Array.Sort(nums);
        while (start < nums.Length - 2) {
            target = -nums[start];
            lo = -~start;
            hi = nums.Length - 1;

            while (lo < hi) {
                if (nums[lo] + nums[hi] > target) {
                    hi--;

                } else if (nums[lo] + nums[hi] < target) {
                    lo++;

                } else {
                    List<int> Init = new List<int>() { nums[start], nums[lo], nums[hi] };
                    res.Add(Init);

                    while (lo < hi && nums[lo] == Init[1]) {
                        lo++;
                    }

                    while (lo < hi && nums[hi] == Init[2]) {
                        hi--;
                    }
                }

            }

            int currStart = nums[start];

            while (start < nums.Length - 2 && nums[start] == currStart) {
                start++;
            }
        }

        return res;
    }
}
公共类解决方案{
公共IList三和(int[]nums){
IList res=新列表();
如果(单位长度<3){
返回res;
}
int start=0;
内卢;
int hi;
int目标;
Array.Sort(nums);
while(开始目标){
嗨——;
}否则如果(nums[lo]+nums[hi]

工具书类
  • 如需更多详细信息,您可以查看。其中有大量可接受的解决方案,包括各种解释、有效算法以及渐近/复杂性分析
如果您正在准备:
  • 我们希望编写基于标准和约定(例如,,,,,,,,,)的代码

我相信您可以通过使用System添加
来修复第一个编译器错误。Linq
您可以指出代码中的第19行和第21行吗?错误在
解决方案列表中。添加(子列表[solCounter])
而且它确实试图添加错误类型的对象。我意识到编程的一部分是知道什么时候寻求帮助,但解决这些难题的目的不是自己动手吗?这些都是编译器错误,这意味着编译器实际上是在告诉你做错了什么。@stuartd你是绝对正确的我并不是在问我的算法,只是想知道为什么我会出现这个编译器错误,谢谢你的回答。