Original title:
Given Arrays a[] and b[], each containing n distinct 2D points in the plane, design a subquadratic algorithm to count The number of points that is contained both in array a[] and array b[].
The goal of the topic is to calculate the number of duplicate point, very simple, the code is as follows
1 ImportJava.awt.Point;2 Importjava.util.Arrays;3 ImportJava.util.HashSet;4 ImportJava.util.Set;5 6 ImportEdu.princeton.cs.algs4.StdRandom;7 8 Public classplanepoints {9 Privateset<point> s =NewHashset<point>();Ten Private intSamepointsnum; OnePlanepoints (intn,point[] Inputa, point[] inputb) { A for(inti=0;i<n;i++){ - S.add (Inputa[i]); - S.add (Inputb[i]); the } -Samepointsnum = 2*n-s.size (); - } - + Public intSamepointsnum () { - returnSamepointsnum; + } A at Public Static voidMain (string[] args) { - intn = 10; -Point[] A =NewPoint[n]; -Point[] B =NewPoint[n]; - System.out.println (a.length); - for(inti=0;i<n;i++){ inA[i] =NewPoint (); - a[i].setlocation (Stdrandom.uniform (n), Stdrandom.uniform (n)); toB[i] =NewPoint (); + b[i].setlocation (Stdrandom.uniform (n), Stdrandom.uniform (n)); - } the System.out.println (Arrays.tostring (a)); * System.out.println (arrays.tostring (b)); $Planepoints pp =Newplanepoints (n,a,b);Panax Notoginseng System.out.println (pp.samepointsnum); - } the}
Coursera algorithms week2 Basic sort interview Questions:1 intersection of the sets