面试算法——6.Mingo Game
2015-12-08 09:57
489 查看
每行,每列,对角线,存在互异的一组数据,则称找到了一个Mingo;
input: 随机1到1,000,000的数填充100*100的数组
output:Mingo的个数,以及最先出现的位置。
input: 随机1到1,000,000的数填充100*100的数组
output:Mingo的个数,以及最先出现的位置。
package ch_1_stackandqueue; import java.util.HashSet; import java.util.Set; /** * @author xiongquan *Timestamp:20151208 */ public class Pro_1_MingoSet { /* * 使用集合来判断每行,每列以及对角线元素是否 互异 * * 根据集合中元素个数,可以得到结果 * * */ private static Set<Integer> set; private static int iarr[][]; private int len; private int count; public Pro_1_MingoSet() { set = new HashSet<>(); } public void deal() { len = iarr.length; int x=-1; int y=-1; //set.clear(); //row for(int i=0;i<len;i++) { set.clear(); int j; for(j=0;j<len;j++) { set.add(iarr[i][j]); } if(set.size()==len) { if(x==-1 && y==-1) { x=i; y=0; }else if(i<=x) { x=i; y=0; } count++; } } //column for(int i=0;i<len;i++) { set.clear(); int j; for(j=0;j<len;j++) { set.add(iarr[j][i]); } if(set.size()==len) { if(x==-1) { x=0; y=i; }else { if(j<=x) { x=j; y=i; } } count++; } } //+diagonal int k; set.clear(); for(k=0;k<len;k++) { set.add(iarr[k][k]); } if(set.size()==len) { if(x==-1) { x=k; y=k; }else { if(0<=x&&0<=y) { x=0; y=0; } } count++; } //- set.clear(); for(k=0;k<len;k++) { set.add(iarr[k][len-1-k]); } if(set.size()==len) { if(x==-1) { x=0; y=len-1; }else { if(0<=x) { x=0; y=0; } } count++; } System.out.println("坐标:"+x+","+y+"\nmingo个数:"+count); } public static void main(String[] args) { Pro_1_MingoSet test = new Pro_1_MingoSet(); //input the test data iarr = new int[][] { { 1, 2, 2 }, { 1, 3, 3 }, { 3, 1, 2 } }; // test.deal(); System.out.println(Integer.MAX_VALUE); } }
相关文章推荐
- 传智播客展望2016年大学毕业生求职现状
- 黑马程序员——Java重点基础之IO流(三)
- Java 程序员们值得一看的好书推荐[转载]
- 面试最后如何向面试官发问?
- <黑马程序员> 第九篇:反射
- 黑马程序员-C学习日记-数组
- 如何让你的程序员不要厌倦工作?
- 一位求职者发来的感谢信
- 慢腾腾成长的程序员
- 黑马程序员——Java重点基础之IO流(二)
- 黑马程序员——OC之self关键字、多态
- 黑马程序员——Java重点基础之IO流(一)
- 黑马程序员——java基础之继承和多态
- 黑马程序员--第四篇:包、权限修饰符、内部类、匿名内部类
- 黑马程序员—java基础复习—基础知识
- 黑马程序员--IO其他流
- 黑马程序员--第三篇:多态、抽象类、接口
- 嵌入式经典面试题
- 一道面试题 有20个数组,每个有500个元素,升序排列,找出前500的数
- 前端面试题准备 3