首页 > 编程语言 > 详细

Coursera Algorithms week2 基础排序 Interview Questions: 1 Intersection of two sets

时间:2017-07-22 00:03:28      阅读:377      评论:0      收藏:0      [点我收藏+]

题目原文:

Given two arrays a[] and b[], each containing n distinct 2D points in the plane, design a subquadratic algorithm to count the number of points that are contained both in array a[] and array b[].

题目的目标就是计算重复point的个数,很简单,代码如下

 1 import java.awt.Point;
 2 import java.util.Arrays;
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 import edu.princeton.cs.algs4.StdRandom;
 7 
 8 public class PlanePoints {
 9     private Set<Point> s = new HashSet<Point>();
10     private int samePointsNum;
11     PlanePoints(int n,Point[] inputa, Point[] inputb){
12             for(int i=0;i<n;i++){
13                 s.add(inputa[i]);
14                 s.add(inputb[i]);
15         }
16             samePointsNum = 2*n - s.size();
17     }
18     
19     public int samePointsNum(){
20         return samePointsNum;
21     }
22     
23     public static void main(String[] args){
24         int n = 10;
25         Point[] a = new Point[n];
26         Point[] b = new Point[n];
27         System.out.println(a.length);
28         for(int i=0;i<n;i++){
29             a[i] = new Point();
30             a[i].setLocation(StdRandom.uniform(n), StdRandom.uniform(n));
31             b[i] = new Point();
32             b[i].setLocation(StdRandom.uniform(n), StdRandom.uniform(n));
33         }
34         System.out.println(Arrays.toString(a));
35         System.out.println(Arrays.toString(b));
36         PlanePoints pp = new PlanePoints(n,a,b);
37         System.out.println(pp.samePointsNum);
38     }
39 }

 

Coursera Algorithms week2 基础排序 Interview Questions: 1 Intersection of two sets

原文:http://www.cnblogs.com/evasean/p/7220036.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!