Created
March 27, 2013 06:01
-
-
Save guolinaileen/5252053 to your computer and use it in GitHub Desktop.
if wishing to speed up, hashmap should be used because we cann't ensure that all the results are in included. if using hashmap, fetching time would be O(1)
C++ version can be improved just like java version did.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<vector<int> > fourSum(vector<int> &num, int target) { | |
// Start typing your C/C++ solution below | |
// DO NOT write int main() function | |
vector<vector<int> > result; | |
if(num.size()<4) return result; | |
sort(num.begin(), num.end()); | |
for(int i=0; i<=num.size()-4; i++) | |
{ | |
if(i!=0 && num[i]==num[i-1]) continue; | |
for(int j=i+1; j<=num.size()-3; j++) | |
{ | |
if(j!=i+1 && num[j]==num[j-1]) continue; | |
int p=j+1, q=num.size()-1; | |
while(p<q) | |
{ | |
int sum=num[i]+num[j]+num[p]+num[q]; | |
if(sum==target) | |
{ | |
vector<int> tempResult(4); | |
tempResult[0]=num[i]; | |
tempResult[1]=num[j]; | |
tempResult[2]=num[p]; | |
tempResult[3]=num[q]; | |
result.push_back(tempResult); | |
p++; | |
q--; | |
}else if(sum<target) | |
{ | |
p++; | |
}else if(sum>target) | |
{ | |
q--; | |
} | |
while(p!=j+1 && num[p]==num[p-1]) | |
{ | |
p++; | |
} | |
while(q!=num.size()-1 && num[q]==num[q+1]) | |
{ | |
q--; | |
} | |
} | |
} | |
} | |
return result; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Solution { | |
//record the indexes of two numbers | |
class Pair | |
{ | |
int x; | |
int y; | |
Pair(int i, int j) | |
{ | |
x=i; | |
y=j; | |
} | |
} | |
public boolean isSame(int [] resultIdx) | |
{ | |
int length=resultIdx.length; | |
for(int i=0; i<length; i++) | |
{ | |
for(int j=i+1; j<length; j++) | |
{ | |
if(resultIdx[i]==resultIdx[j]) | |
return true; | |
} | |
} | |
return false; | |
} | |
public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) { | |
int length=num.length; | |
/*table is used to store the sum of two numbers and their ids | |
the pair of nodes can have the same total | |
*/ | |
Hashtable<Integer, ArrayList<Pair>> table=new Hashtable<Integer, ArrayList<Pair>>(); | |
//set is used for checking duplicate indexes | |
HashSet<ArrayList<Integer>> set=new HashSet<ArrayList<Integer>>(); | |
ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>(); | |
//form the hashtable | |
for(int i=0; i<length; i++) | |
{ | |
for(int j=i+1; j<length; j++) | |
{ | |
int temp=num[i]+num[j]; | |
ArrayList<Pair> p=new ArrayList<Pair>(); | |
if(table.containsKey(temp)) | |
{ | |
p=table.get(temp); | |
} | |
p.add(new Pair(i, j)); | |
table.put(temp, p); | |
} | |
} | |
Set<Integer> keys=table.keySet(); | |
int keylength=keys.size(); | |
int []keyset= new int[keylength]; | |
int i=0; | |
Iterator<Integer> c=keys.iterator(); | |
while(c.hasNext()) | |
{ | |
keyset[i++]=c.next(); | |
} | |
Arrays.sort(keyset); | |
int start=0; | |
int end=keylength-1; | |
while(start<=end) | |
{ | |
int temp=keyset[start]+keyset[end]; | |
if(temp==target) | |
{ | |
ArrayList<Pair> a=table.get(keyset[start]); | |
ArrayList<Pair> b=table.get(keyset[end]); | |
for(Pair x:a) | |
{ | |
for(Pair y:b) | |
{ | |
int[] resultIdx={x.x, x.y, y.x, y.y}; | |
if(isSame(resultIdx)) continue; | |
int[] resultNum={num[x.x], num[x.y],num[ y.x], num[y.y]}; | |
Arrays.sort(resultNum); | |
ArrayList<Integer> subResult=new ArrayList<Integer> (); | |
for(int k: resultNum) | |
{ | |
subResult.add(k); | |
} | |
if(!set.contains(subResult)) | |
{ | |
result.add(subResult); | |
set.add(subResult); | |
} | |
} | |
} | |
start++; | |
end--; | |
}else if(temp<target) | |
{ | |
start++; | |
}else | |
{ | |
end--; | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment