#!/usr/bin/perl use strict; ######################################## 用grep 和map 获取两个列表的交集并集.补集####################################### my @a=("a","b","c","d","e");my @b=("b","g","f&…
第六期Power8大赛 1.1 比赛题目 题目: 计算两个集合的差集: 详细说明: 分别有集合A和B两个大数集合,求解集合A与B的差集(A中有,但B中无的元素),并将结果保存在集合C中,要求集合C中的元素升序. 输入为两个文件,分别为A.txt,B.txt,一行一个值,并且是无序的.结果输出到C.txt,即输入文件的差集,一行一个值,并且要求结果升序排列. 考量点: (1) 大数集合求差集: (2) 大数据集合排序: 题目实例: 例如,若集合A={5,20,10,15,25,30},集合B={1…
假设我们现在有两个文件 a.txt .b.txt a.txt 中的内容如下: a c 1 3 d 4 b.txt 中的内容如下: a b e 2 1 5 # Example 01 计算并集: [root@VM_81_181_centos ~]# sort -u a.txt b.txt 1 2 3 4 5 a b c d e [root@VM_81_181_centos ~]# # Exmaple 02 计算交集: [root@VM_81_181_centos ~]# grep -F -f a.t…
前提需要明白List是引用类型,引用类型采用引用传递. 我们经常会遇到一些需求求集合的交集.差集.并集.例如下面两个集合: List<String> list1 = new ArrayList<String>(); list1.add("A"); list1.add("B"); List<String> list2 = new ArrayList<String>(); list2.add("B");…
循环判断2个数组 将相同的公共元素复制到新数组中即可 import java.util.Arrays; public class count_same_number { public static int[] join(int[] a,int[] b) { int count=0; int new_target[]=new int[Math.max(a.length, b.length)];//新数组 int index=0; for(int i=0;i<a.length;i++) { for(…
问题分析: 用两个指针分别遍历即可. 问题求解: public class Solution { /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ public int[] intersection(int[] nums1, int[] nums2) { List<Integer> list = new ArrayList<Integer>…
问题分析: 既然返回值没有重复,我们不妨将结果放进set中,然后对两个set进行比较. 问题求解: public class Solution { /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> set1 = new…
问题分析: 用两个指针分别遍历即可. 问题求解: public class Solution { /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ public int[] intersection(int[] nums1, int[] nums2) { List<Integer> list = new ArrayList<Integer>…
问题分析: 既然返回值没有重复,我们不妨将结果放进set中,然后对两个set进行比较. 问题求解: public class Solution { /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> set1 = new…
求连个集合的交集: import java.util.ArrayList; import java.util.List; public class TestCollection { public static void main(String[] args) { List<String> strList = new ArrayList<String>(); List<String> strList2 = new ArrayList<String>(); fo…