本来以为只要在3Sum外面再包一层循环就好了,可是。。。在Judge Large的时候还是超时了 呜呜呜
。。。。居然还有可能超时可能不超时的情况。。。囧 多run了几次就过了 汗啊 去google上找找答案好了
额 似乎没有什么其他好的方法了
倒是发现了两个小问题:
1、Java中ArrayList的contains方法
我没有重写contains方法,但是为什么ArrayList判断contrains的时候,明明是两个不同的ArrayList,只是里面的数都一样,这样就判断相等了?
了解了 contains中使用到了equals方法,然后ArrayList的equals方法是比较他们是不是相等而不是看对象指针是不是一样~~
2、搜索结果中提到了brute-force方法,可是这和简单模式匹配有什么关系呢?
public class Solution { public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) { Arrays.sort(num); ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> tempList; for (int i = 0; i < num.length - 3; ++i) for (int j = i + 1; j < num.length - 2; ++j){ int tempTarget = target - num[i] - num[j]; int m = j + 1; int n = num.length - 1; while (m < n){ if (num[m] + num[n] > tempTarget) n--; else if (num[m] + num[n] < tempTarget) m++; else { tempList = new ArrayList<Integer>(); Collections.addAll(tempList, num[i], num[j], num[m], num[n]); if (!results.contains(tempList)) results.add(tempList); m++; n--; } } } return results; } }
。。。。居然还有可能超时可能不超时的情况。。。囧 多run了几次就过了 汗啊 去google上找找答案好了
额 似乎没有什么其他好的方法了
倒是发现了两个小问题:
1、Java中ArrayList的contains方法
我没有重写contains方法,但是为什么ArrayList判断contrains的时候,明明是两个不同的ArrayList,只是里面的数都一样,这样就判断相等了?
了解了 contains中使用到了equals方法,然后ArrayList的equals方法是比较他们是不是相等而不是看对象指针是不是一样~~
2、搜索结果中提到了brute-force方法,可是这和简单模式匹配有什么关系呢?
评论
1 楼
superich2008
2012-11-14
1、去重可以用Set集合
2、在排序后,相邻2个元素如果相同可以减少一次循环(在多重循环中效率更高)
2、在排序后,相邻2个元素如果相同可以减少一次循环(在多重循环中效率更高)
public static java.util.List<java.util.List<Integer>> fourSum(int[] nums, int sum) { final int n = nums.length; if (n < 4) return java.util.Collections.emptyList(); // 排序 java.util.Arrays.sort(nums); java.util.Set<java.util.List<Integer>> set = new java.util.HashSet<java.util.List<Integer>>(); // i j k m 为数组下标 int i = 0, j = i + 1, k = j + 1, m = n - 1; for (i = 0; i < n; i++) { if (i > 0 && nums[i] == nums[i - 1]) { continue; } for (j = i + 1; j < n; j++) { if (j > i + 1 && nums[j] == nums[j - 1]) { continue; } k = j + 1; m = n - 1; while (k < m) { if (k > j + 1 && nums[k] == nums[k - 1]) { k++; continue; } int t = nums[i] + nums[j] + nums[k] + nums[m]; if (t == sum) { java.util.List<Integer> temp = new java.util.ArrayList<Integer>(); java.util.Collections.addAll(temp, Integer.valueOf(nums[i]), Integer.valueOf(nums[j]), Integer.valueOf(nums[k]), Integer.valueOf(nums[m])); set.add(temp); k++; m--; } else if (t > sum) { m--; } else { k++; } } // end-while } // end-for } // end-for return new java.util.ArrayList<java.util.List<Integer>>(set); }
发表评论
-
Insert Interval
2012-11-11 01:33 543各种条件真复杂,不仅是边界条件,而且还要分很多种情况讨论 而且 ... -
Implement strStr()
2012-11-07 15:44 582唉 终于到了要记算法的时候了 KMP。。。还没写完 回去再写。 ... -
Flatten Binary Tree to Linked List && Generate Parentheses && Gray Code
2012-11-07 00:08 1093Flatten太简单了 递归 一遍过 oh yeah = = ... -
First Missing Positive
2012-11-06 22:50 589唉 想了很久都没想出来 后来还是看了网上的答案 >_&l ... -
Edit Distance
2012-11-06 00:27 670动规 就是递推。。。比较难想 然后数组长度设置比字符串长度多一 ... -
Divide Two Integers
2012-11-05 00:12 714自己实现除法 太太太恶心了。。。。 就是用位移代替了乘法,然后 ... -
Distinct Subsequences
2012-11-04 21:44 657动规,从前到后用T的每一个字符i,扫描S的每一个字符j。维护一 ... -
Count and Say
2012-11-04 18:46 693public class Solution { ... -
Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Sear
2012-11-04 17:36 826/** * Definition for binary ... -
Container With Most Water
2012-11-04 00:25 697本来以为是个简单的题目,直接二重循环,结果小测试过了,大测试超 ... -
Construct Binary Tree from Inorder and Postorder Traversal
2012-11-03 23:40 748不知道为什么错了。。。eclipse上明明是正确的啊 leet ... -
Combinations
2012-11-03 22:19 606全排列 按理说很简单,可是用递归写,边界条件就还是难想清楚,s ... -
Combination Sum I && II
2012-11-03 21:41 751还是递归 但是边界条件以及边界上的处理不容易搞清楚(一开始我就 ... -
climbing stairs
2012-11-03 17:18 610一开始觉得是简单的组合数学题,但是写完之后发现,首先组合数不是 ... -
Binary Tree Inorder Traversal
2012-11-02 23:51 615I简单 直接递归就好 addAll函数很好用 /** ... -
Best Time to Buy and Sell Stock I & II
2012-11-02 22:05 1045啊 第一次直接过small和big测试 好爽!虽然主要是以前知 ... -
Balanced Binary Tree
2012-11-01 23:38 610/** * Definition for binary ... -
Anagrams
2012-10-31 00:33 587这题实在是没懂它的意思。。。囧啊 import java. ... -
Add Two Numbers
2012-10-30 23:03 614这题不难 直接上递归就行 /** * Definiti ... -
Add Binary
2012-10-29 00:07 601public String addBinary(Strin ...
相关推荐
双指针算法,python数组双指针算法求和问题LeetCode2sum3sum4sum含代码
文档python数组双指针算法求和问题LeetCode2sum3sum4sum含代码提取方式是百度网盘分享地址
此算法解决如下问题:给定数据数均为n(1≦n≦4000)的四张数据表A、B、C、D,数据类型为整型,试从每张表中选出一个数,使得四个数的和为0,问这样的组合共有多少?
sum(2,3,4)和sum(2)(3)(4)输出值一样
For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four different sums that equal 4: 4,3+1,2+2, and 2+1+1.(A number can be used within a sum as many times as it appears in the ...
17 4Sum 55 18 3Sum Closest 57 19 String to Integer (atoi) 59 20 Merge Sorted Array 61 ... ... 231 Counting Bits 561 232 Maximum Product of Word Lengths 563 233 Gray Code 565 234 Permutations 567 235 ...
18. 4Sum 19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 23. Merge k Sorted Lists 24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group 26...
18| [4 Sum](https://leetcode.com/problems/4sum/) | [C++](./C++/4sum.cpp) [Python](./Python/4sum.py) | _O(n^3)_ | _O(1)_ | Medium || Two Pointers 26 | [Remove Duplicates from Sorted Array]...
Multiplicate_4_4 num1_1 Position_adc_ctrl Position_Adjust pulse_16 pulse_16_sum pulse_sum Rate_Adjust Rate_Measure ROM_3_4 RS_FF second_pulse_latch Serial Shift_Latch SRAM_8_8 stopwatch subtracter_1 ...
Public Function tx_read_frame(leixing As Byte, data1 As Byte, data2 As Byte, data3 As Byte, data4 As Byte) Static Byteout(0 To 7) As Byte, i As Byte '向外发送 Dim sum As Integer Byteout(0) = &H55; ...
matlab函数sum和size用法-matlab函数sum和size用法.doc matlab函数sum和size用法.doc sum函数解释函数功能 求数组元素的总和 使用方法B = sum 返回数组A不同维数的总和。 如果A是一个...
ERP迁移至S4HANA 第五步: Technical conversion with SUM(DMO)
ZEROSUM鳞波X4ZEROSUMチヌX4PWZEROSUM鳞-宇崎日新.pdf
Calculating The Sum of Its Parts 数据结构,树的一道题。
4总和 Leetcode 问题 18 给定一个由 n 个整数组成的数组 nums 和一个整数目标,nums 中是否有元素 a、b、c 和 d 使得 a + b + c + d = target? 找到数组中所有唯一的四元组,给出目标的总和。 笔记: The solution ...
4Sum Remove Element Move Zeroes Next Permutation Permutation Sequence Valid Sudoku Trapping Rain Water Rotate Image Plus One Climbing Stairs Set Matrix Zeroes Gas Station Candy Majority Element Rotate...
第4章 集成运算放大器及其应用SUM4.pptx
http://www.centospub.com/make/install.html这个页面上的md5sum下载地址无法下载。 http://mirror.tini4u.net/centos/4.4/isos/i386/md5sum
DirectX 9.0 SDK (summer 2004)
ISO 7870-4:2011 Control charts - Part 4:Cumulative sum charts -