【发布时间】:2021-01-14 21:49:05
【问题描述】:
谜题说明
*给定一个包含 n 个整数的数组 nums,在 nums 中是否存在元素 a、b、c 使得 a + b + c = 0?在数组中找到所有唯一的三元组,其总和为零。 请注意,解集不得包含重复的三元组。
示例 1: 输入:nums = [-1,0,1,2,-1,-4] 输出:[[-1,-1,2],[-1,0,1]]
示例 2: 输入:nums = [] 输出:[]
示例 3: 输入:nums = [0] 输出:[]
约束: 0
描述结束
我在通过 leetcode 解决这个难题时遇到了一些问题,我的运行时间超过了时间限制,因此我的代码需要优化。
我知道可以修复的两个位置是双循环条目和检查任何列表是否匹配的循环。
一个额外的问题并没有阻止我通过,但在某些情况下可能会导致此失败是检查列表以查看它是否包含距离并且不等于自 IndexOf 以来的第一次或第二次迭代将返回找到的第一个索引。
至于循环检查,我在 python 中找到了一个更快的解决方案,但我不确定 c# equivilent
Python代码查找是否是一个列表
存在于列表列表中。
# Input List Initialization
Input = [[1, 1, 1, 2], [2, 3, 4], [1, 2, 3], [4, 5, 6]]
# List to be searched
list_search = [1, 1, 1, 2]
# Using in to find whether
# list exists or not
if list_search in Input:
print("True")
else:
print("False")
这是我当前的代码解决方案(TLDR:它可以工作,但太慢了)
public class Solution {
public IList<IList<int>> ThreeSum(int[] nums) {
/*total must always = 0
ideas to use a sort on each list to check if there are
multiple arrays that are the same combination of values
the last sum can be assumed and pulled via Arraylist since there can only be one answer assumed
values can be quickly ignored if there is no corrisponding 3rd value that sums to 0
ex -1,-1, do we have a value of 2 if not than we can ignore this combination
also if we do than we will want to temporarly store it than sort the list and compare to our already existing
list
Something to think about if the numbs array is sorted ahead of time can we ignore using the sort operation
in some conditions to save cpu time
If the size of the nums array is less than 3 than return a blank array
also if the size of the array = 3 we can assume the solution and return nums (maybe)
A possible way to make this faster would be to find a way to assume all possible values have been found or use a Addoku/Arrow-Sudoku method of solving and see if for example a value in the array is 9 and there are no negative values in a set of 2 that can = -9 than 9 can be instantly ignored and removed since the combination is not possible.
I dont think the index check is perfect since indexof checks for the first value but it has been passing all of my test cases so I will leave it for now but I will need to find a better way to remove I and Y elements from the index check to avoid duplicates.
*/
IList<IList<int>> mainLs = new List<IList<int>>();
if(nums.Length < 3){
return mainLs;
}
for(int i = 0; i< nums.Length; i++){
int currentVal = nums[i];
for(int y = 0; y< nums.Length; y++){
//is there an easier way to skip the matching index
if(i == y){
continue;
}else{
/*
We will sum the two elements together and check the distance
between the sum and 0. Than if we have the distance in our index and the value to sum to 0
is not the index we are on since we need to avoid duplicates than add that value to the array
*/
ArrayList tempNumls = new ArrayList(nums);
int sum = currentVal + nums[y];
int distance = Math.Abs(sum - 0);
//Math abs always produces a postive value which will work for us if the sum is negative but if the sum is positive it will return a positive value so we multiply by -1 to produce the proper result.
if(sum > 0){
distance = distance * -1;
}
//output the results found
// Console.WriteLine("distance: "+distance + " X & Y = "+ currentVal + " & " + nums[y]);
if(tempNumls.Contains(distance) && tempNumls.IndexOf(distance) != y && tempNumls.IndexOf(distance) != i){
ArrayList tempIls = new ArrayList() {currentVal, nums[y], distance};
//sorting and comparing will be the easiest way to make sure we do not have matching lists
tempIls.Sort();
var list = tempIls.Cast<int>().ToList();
bool match = false;
//best to find a better way to traverse and compare the list of list without
//needing to loop over them all
foreach (List<int> listname in mainLs)
{
if (listname.SequenceEqual(list))
{
match = true;
break;
}
}
if(!match){
mainLs.Add(list);
}
}
}
}
}
return mainLs;
}
}
【问题讨论】:
-
您知道
Contains、IndexOf、SequenceEqual和ToList都包含自己的循环吗? -
旁注:请避免使用
ArrayList将代码发布到其他人看到的位置,例如 SO - 如果不偏离问题,它没有任何用处。见stackoverflow.com/questions/2309694/… -
代码中也没有
Dictionary或HashSet...因此,虽然起点很好,但无法接受此代码作为答案。但在进入竞争之前先让它运行。 -
@AlexeiLevenkov 谢谢,我刚刚回到 c# 我不确定要使用什么容器我只是想要一些可以排序并检查包含的东西我会切换它们。另外,您如何建议我在我的解决方案中加入 Dictionary 和 Hashset?
-
@madreflection 好点,这对我来说可能是最好的做法,看看我可以在哪里删除或替换其中的一些。我试试看,你还有什么建议吗?
标签: python c# loops optimization indexof