首页 > 代码库 > 今日头条编程题
今日头条编程题
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()); } }
今日头条编程题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。