Leetcode:454. 4Sum II (解决时间和空间复杂度是关键)
2017-12-06 20:18
489 查看
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1. Example: Input: A = [ 1, 2] B = [-2,-1] C = [-1, 2] D = [ 0, 2] Output: 2 Explanation: The two tuples are: 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
这个题主要解决空间和时间的问题,我的一种错误解法就是控制时间复杂度在n*n但是空间复杂度不满足OJ要求。
错误代码:
public static int fourSumCount(int[] A, int[] B, int[] C, int[] D) { 4000 int count = 0; int index = 0; int nums1[] =new int[A.length*B.length]; int nums2[] =new int[A.length*B.length*C.length]; int nums3[] =new int[A.length*B.length*C.length*D.length]; for (int i = 0; i < A.length; i++) { for (int j = 0; j < B.length; j++) { nums1[index++]=A[i]+B[j]; } } index = 0; for (int i = 0; i < nums1.length; i++) { for (int j = 0; j < C.length; j++) { nums2[index++]=nums1[i]+C[j]; } } index = 0; for (int i = 0; i < nums2.length; i++) { for (int j = 0; j < D.length; j++) { nums3[index++]=nums2[i]+D[j]; } } for (int i = 0; i < nums3.length; i++) { if (nums3[i]==0) { count++; } } for (int i = 0; i < nums1.length; i++) { System.out.println(nums1[i]); } for (int i = 0; i < nums2.length; i++) { System.out.println(nums2[i]); } for (int i = 0; i < nums3.length; i++) { System.out.println(nums3[i]); } return count; }
借助HashMap,其实这个非常的好想到,这样想,我们是要来记录两个数字的和,但是如果用数组,也就是上面的情况,那么肯定有重复的值存在,所以现在我们用Map来存值,可以解决时间复杂度不满足的情况,如果sums不存在这个sum,那么就加入,键为sum 值为1 但是我们知道键值对不允许键重复,当存在和为重复的时候,那么我们就判断一下,然后sums加入sum,但是这个值加1,这样就可以统计当前sum的个数,为后面计算元组个数做准备。
之后再第二个循环里面 如果存在-sum那么这个元组就可以,取出值相加就可以了。
———简单清晰。
public static int fourSumCount(int[] A, int[] B, int[] C, int[] D) { Map<Integer,Integer> sums = new HashMap<>(); int count = 0; for(int i=0; i<A.length;i++) { for(int j=0;j<B.length;j++){ int sum = A[i]+B[j]; if(sums.containsKey(sum)) { sums.put(sum, sums.get(sum)+1); } else { System.out.println(sum); sums.put(sum, 1); } } } //System.out.println(sums.toString()); for(int k=0; k<A.length;k++) { for(int z=0;z<C.length;z++){ int sum = -(C[k]+D[z]); if(sums.containsKey(sum)) { count+=sums.get(sum); } } } return count; }
相关文章推荐
- Candy [leetcode] O(n)时间复杂度,O(1)空间复杂度的方法
- [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
- 每天一道LeetCode-----找到序列中第一个没有出现的正整数,要求时间复杂度是O(n),空间复杂度是O(1)
- Candy [leetcode] O(n)时间复杂度,O(1)空间复杂度的方法
- LeetCode 148. Sort List--O(nlogn)时间复杂度和常数空间复杂度给链表排序
- LeetCode | 442. Find All Duplicates in an Array 限制时间复杂度O(n)空间复杂度O(1)的题
- leetcode 454. 4Sum II 四个数求和的总次数+Map降低运算复杂度
- [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
- Candy [leetcode] O(n)时间复杂度,O(1)空间复杂度的方法
- [LeetCode] Set Matrix Zeroes (!!!!时间空间复杂度)
- Leetcode 234 Palindrome Linked List 复杂度为时间O(n) 和空间(1)解法
- Leetcode 234 Palindrome Linked List 复杂度为时间O(n) 和空间(1)解法
- LeetCode 第三题,官方给的标准答案的一个分析,利用空间换时间,将复杂度降到了O(n)
- 时间复杂度和空间复杂度详解
- 排序算法之 计数排序 及其时间复杂度和空间复杂度
- 对于一颗完全二叉树,要求给所有节点加上一个pNext指针,指向同一层的相邻节点;如果当前节点已经是该层的最后一个节点,则将pNext指针指向NULL;给出程序实现,并分析时间复杂度和空间复杂度。
- 一道时间复杂度为O(N)空间复杂度为O(1)的排序问题
- 算法的时间复杂度和空间复杂度
- 【leetcode】454. 4Sum II 总结
- Leetcode 238 Product of Array Except Self 时间O(n)和空间O(1)解法