Coursera algorithms week2 Basic sort interview Questions:1 intersection of the sets

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.