454. 四数相加 II
思路: 这道题目是四个独立的数组,只要找到A[i] + B[j] + C[k] + D[l] = 0就可以,不用考虑有重复的四个元素相加等于0的情况,所以相对于题目18. 四数之和,题目15.三数之和,还是简单了不少!
public static int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4)
{
Dictionary<int, int> dict = new Dictionary<int, int>();
for (int i = 0; i < nums1.Length; i++)
for (int j = 0; j < nums2.Length; j++)
{
if (dict.ContainsKey(nums1[i] + nums2[j]))
{
dict[nums1[i] + nums2[j]]++;
}
else
{
dict.Add(nums1[i] + nums2[j], 1);
}
}
int sum = 0;
for (int i = 0; i < nums3.Length; i++)
for (int j = 0; j < nums4.Length; j++)
{
if (dict.ContainsKey(-nums3[i] - nums4[j]))
{
sum += dict[-nums3[i] - nums4[j]];
}
}
return sum;
}
383. 赎金信
public static bool CanConstruct(string ransomNote, string magazine)
{
if (ransomNote.Length < magazine.Length) return false;
int[] data1 = new int[26];
int[] data2 = new int[26];
for (int i = 0; i < ransomNote.Length; i++)
{
data1[ransomNote[i] - 'a']++;
}
for (int i = 0; i < magazine.Length; i++)
{
data1[magazine[i] - 'a']--;
}
for (int i = 0; i < data1.Length; i++)
{
if (data1[i] > 0) return false;
}
return true;
}
- 时间复杂度: O(n)
- 空间复杂度:O(1)
15. 三数之和
思路: 双指针法
public static IList<IList<int>> ThreeSum(int[] nums)
{
Array.Sort(nums);
IList<IList<int>> result = new List<IList<int>>();
if (nums[0] > 0) return result;// 排序之后如果第一个元素已经大于零,那么无论如何组合都不可能凑成三元组,直接返回结果就可以了
for (int i = 0; i < nums.Length - 2; i++)
{
if (i>0 && nums[i] == nums[i - 1]) continue;// 去重
int left = i + 1;
int right = nums.Length - 1;
while (left < right)
{
if (nums[i] + nums[left] + nums[right] > 0)
{
right--;
}
else if (nums[i] + nums[left] + nums[right] < 0)
{
left++;
}
else
{
result.Add(new List<int> { nums[i], nums[left], nums[right] });
while (left < right && nums[left] == nums[left + 1]) left++;//去重
while (left < right && nums[right] == nums[right - 1]) right--;//去重
left++;
right--;
}
}
}
return result;
}
18.四数之和
思路: 难在去重和剪枝
public static IList<IList<int>> FourSum(int[] nums, int target)
{
Array.Sort(nums);
IList<IList<int>> result = new List<IList<int>>();
//if (nums[0] > target) return result;//如果target < nums[0] < 0, 负数是越加越小
for (int i = 0; i < nums.Length - 3; i++)
{
if (nums[i] > 0 && nums[i] >= target) break;//剪枝
if (i > 0 && nums[i] == nums[i - 1]) continue;//对nums[i]去重
for (int j = i + 1; j < nums.Length - 2; j++)
{
if (nums[i] + nums[j] > 0 && nums[i] + nums[j] > target) break;//剪枝
if (j > i+1 && nums[j] == nums[j - 1]) continue;//对nums[j]去重
int left = j + 1;
int right = nums.Length-1;
while (left < right)
{
if ((long)nums[i] + nums[j] + nums[left] + nums[right] > target) //nums[k] + nums[i] + nums[left] + nums[right] < target 会溢出
{
right--;
}
else if ((long)nums[i] + nums[j] + nums[left] + nums[right] < target)
{
left++;
}
else
{
result.Add(new List<int> { nums[i], nums[j], nums[left], nums[right] });
while (left < right && nums[left] == nums[left + 1]) { left++; } // 对nums[left]和nums[right]去重
while (left < right && nums[right] == nums[right - 1]) { right--; } // 对nums[left]和nums[right]去重
left++;
right--;
}
}
}
}
return result;
}