您现在的位置是:首页 > 文章详情

1-3 交集

日期:2018-07-12点击:409

给定两个数组(数组中不包含相同元素),求两个数组的交集中元素的个数(即共同出现的数,如没有则输出为None) 如输入:
5
1 2 4 6 8
6
1 2 5 6 7 8
输出: 4

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int m = scan.nextInt();
        //使用hash的思想,第一个数组中出现,标为1
        //check第二个数组的hash值是否为1 如果是则重复 用rcnt记录
        int[] hash = new int[100000];
        int temp = 0;
        for(int i = 0; i < m; i++) {
            temp = scan.nextInt();
            hash[temp] = 1;
        }

        int rcnt = 0;
        int n = scan.nextInt();
        for(int j = 0; j < n; j++) {
            temp = scan.nextInt();
            if(hash[temp] == 1) {
                rcnt++;
            }
        }
        if(rcnt > 0) {
            System.out.println(rcnt);
        }else {
            System.out.println("None");
        }

    }
}
原文链接:https://yq.aliyun.com/articles/620722
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章