日期:2014-05-20 浏览次数:20726 次
/** * */ package houlei.test; import java.util.Arrays; /** * 该类创建于 2008-8-29 下午04:19:04 * * @version 1.0.0 * @author 侯磊 */ public class E { public static void main(String[] args) { int a[][] = new int[][] { { 1, 2, 3, 4, 5, 6 }, { 3, 45, 5, 6, 7, 44 } }; int b[][] = new int[][] { { 33, 56, 77, 9, 10, 80 }, { 6, 8, 9, 45, 88, 90 } }; int c[][] = new int[][] { { 122, 55, 67, 99, 80, 199 }, { 777, 595, 97, 234, 898, 102 } }; System.out.println(dissimilitude(a, b)); System.out.println(dissimilitude(a, c)); } public static boolean dissimilitude(int a[][], int b[][]) { int ta[] = sort(a); int tb[] = sort(b); int i = 0, j = 0; while (i < ta.length && j < tb.length) { if (ta[i] == tb[j]) return false; else if (ta[i] < tb[j]) i++; else j++; } return true; } public static int[] sort(int a[][]) { int t[] = new int[a.length * a[0].length]; int index = 0; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[0].length; j++) { t[index++] = a[i][j]; } Arrays.sort(t); return t; } }
------解决方案--------------------
import java.util.Set; import java.util.HashSet; public class Test10 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[][] a = { {1, 2, 3, 4, 5, 6}, {3, 45, 5, 6, 7, 44} }; int[][] b = { {33, 56, 77, 9, 10, 80}, {6, 8, 9,45, 88, 90} }; int[][] c = { {122, 55, 67, 99, 80, 199}, {777, 595, 97, 234, 898, 102} }; if (isContained(a, b)) { System.out.println("The array B contains some same elements with A"); } else { System.out.println("The array B does not contain some same elements with A"); } if (isContained(a, c)) { System.out.println("The array C contains some same elements with A"); } else { System.out.println("The array C does not contain some same elements with A"); } } public static boolean isContained(int[][] a, int[][] b) { Set<Integer> set = new HashSet<Integer>(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { set.add(a[i][j]); } } for (int i = 0; i < b.length; i++) { for (int j = 0; j < b[i].length; j++) { if (set.contains((Integer)b[i][j])) { return true; } } } return false; } }