码迷,mamicode.com
首页 > 其他好文 > 详细

今日头条编程题

时间:2017-04-18 21:51:13      阅读:510      评论:0      收藏:0      [点我收藏+]

标签:print   元素   log   ann   logs   ==   int   system.in   pre   

1.两数组找相同的元素-array

50%

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int m = cin.nextInt();
        int[] a = new int[m];
        for (int i = 0; i < m; i++) {
            a[i] = cin.nextInt();
        }
        int n = cin.nextInt();
        int[] b = new int[n];
        for (int i = 0; i < n; i++) {
            b[i] = cin.nextInt();
        }
        helper(a, b);
    }
    
    public static void helper(int[] a, int[] b) {
        int m = a.length;
        int n = b.length;
        for (int j = 0; j < n; j++) {
            for (int i = 0; i < m; i++) {
                if (a[i] == b[j]) 
                    System.out.println(b[j]);
            }
        }
    }
}

 

50% TLE/OLE 

import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        
        int m = cin.nextInt();
        int[] a = new int[m];
        for (int i = 0; i < m; i++) {
            a[i] = cin.nextInt();
        }
        
        int n = cin.nextInt();
        int[] b = new int[n];
        for (int i = 0; i < n; i++) {
            b[i] = cin.nextInt();
        }
        
        //将a中的数据添加到Hashset中,利用hashset的不重复性来比较b数组中的数据;
        Set<Integer> set = new HashSet<Integer>();
        String rst = "";
        for (int i = 0; i < m; i++) {
            set.add(a[i]);
        }
        
        for (int j = 0; j < n; j++) {
            if (!set.add(b[j])) {
                rst += b[j] + " ";
            }
        }
        
        System.out.println(rst.trim());
    }
}

 

今日头条编程题

标签:print   元素   log   ann   logs   ==   int   system.in   pre   

原文地址:http://www.cnblogs.com/buwenyuwu/p/6730660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!