CC150 - 11.6
Question:
Given an M x N matrix in which each row and each column is sorted in ascending order, write a method to find an element.
 package POJ;
 public class Main {
     /**
      *
      * 11.6 Given an M x N matrix in which each row and each column is sorted in
      * ascending order, write a method to find an element.
      *
      */
     public static void main(String[] args) {
         Main so = new Main();
     }
     public static Coordinate findElement(int[][] matrix, int x) {
         Coordinate origin = new Coordinate(0, 0);
         Coordinate dest = new Coordinate(matrix.length - 1,
                 matrix[0].length - 1);
         return findElement(matrix, origin, dest, x);
     }
     public static Coordinate findElement(int[][] matrix, Coordinate origin,
             Coordinate dest, int x) {
         if (!origin.inbounds(matrix) || !dest.inbounds(matrix)) {
             return null;
         }
         if (matrix[origin.row][origin.column] == x)
             return origin;
         if (!origin.isBefore(dest))
             return null;
         Coordinate start = (Coordinate) origin.clone();
         int diagDist = Math.min(dest.row - origin.row, dest.column
                 - origin.column);
         Coordinate end = new Coordinate(start.row + diagDist, start.column
                 + diagDist);
         Coordinate p = new Coordinate(0, 0);
         while (start.isBefore(end)) {
             p.setToAverage(start, end);
             if (x > matrix[p.row][p.column]) {
                 start.row = p.row + 1;
                 start.column = p.column + 1;
             } else {
                 end.row = p.row - 1;
                 end.column = p.column - 1;
             }
         }
         return partitionAndSearch(matrix, origin, dest, start, x);
     }
     private static Coordinate partitionAndSearch(int[][] matrix,
             Coordinate origin, Coordinate dest, Coordinate pivot, int elem) {
         // TODO Auto-generated method stub
         Coordinate lowerLeftOrigin = new Coordinate(pivot.row, origin.column);
         Coordinate lowerLeftDest = new Coordinate(dest.row, pivot.column - 1);
         Coordinate upperRightOrigin = new Coordinate(origin.row, pivot.column);
         Coordinate upperRightDest = new Coordinate(pivot.row - 1, dest.column);
         Coordinate lowerLeft = findElement(matrix, lowerLeftOrigin,
                 lowerLeftDest, elem);
         if (lowerLeft == null)
             return findElement(matrix, upperRightOrigin, upperRightDest, elem);
         return lowerLeft;
     }
 }
 class Coordinate implements Cloneable {
     int row;
     int column;
     public Coordinate(int r, int c) {
         row = r;
         column = c;
     }
     public boolean inbounds(int[][] matrix) {
         return row >= 0 && column >= 0 && row < matrix.length
                 && column < matrix[0].length;
     }
     public boolean isBefore(Coordinate p) {
         return row <= p.row && column <= p.column;
     }
     public Object clone() {
         return new Coordinate(row, column);
     }
     public void setToAverage(Coordinate min, Coordinate max) {
         row = (min.row + max.row) / 2;
         column = (min.column + max.column) / 2;
     }
 }
CC150 - 11.6的更多相关文章
- CC150 - 11.5
		Question: Given a sorted array of strings which is interspersed with empty strings, write a method t ... 
- CC150 - 11.3
		Question: Given a sorted array of n integers that has been rotated an unknown number of times, write ... 
- CC150 - 11.2
		Question: Write a method to sort an array of strings so that all the anagrams are next to each other ... 
- CC150 - 11.1
		Question: You are given two sorted arrays, A and B, where A has a large enough buffer at the end to ... 
- 地区sql
		/*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ... 
- 11.8---维护x的秩(CC150)
		思路:比较easy.就是借助hashset让他有序然后就能够比较节省时间了. 答案: public static int[] getRankOfNumber(int[] a, int n){ int[ ... 
- 11.7---叠罗汉表演节目(CC150)
		1,牛客网第一题:这其实跟找最长递增子序列是一个东西.注意的地方是,返回的是最大的dp,而不是dp[N-1]. 答案: public static int getHeight(int[] men, i ... 
- 11.6---矩阵查找元素(CC150)
		思路,一旦提到查找就要想到二分查找. public static int[] findElement(int[][] a, int n, int m, int key) { // write code ... 
- 11.5---含有空字符串的字符串查找(CC150)
		注意,1,"" 和 " ".是不同的,空字符串指的是"": 2,注意String的compareTo.小于是指<0.并不是==-1: ... 
随机推荐
- nginx lua处理图片
			user apache apache; worker_processes 4; worker_rlimit_nofile 100000; #error_log logs/error.log; #err ... 
- .NET Reflector 7.6.1.824 Edition .NET程序反编译神器(附插件安装教程2012-10-13更新) 完全破解+使用教程
			原文来自VAllen cnblogs 一.使用教程1.解压后,双击Reflector.exe,如果有选择默认版本的.Net Framework,根据需要选择即可.你选择的版本不同则出现的默认程序集也不 ... 
- HTML表单元素Emil和密码
			<form action="" method="post" name="myform"><p>E-mail:< ... 
- iOS CALayer动画中使用的3个tree
			在网上经常看到关于layer的tree的描述,不太理解,今天找到了官方文档,原文在Core Animation Programming Guide 中. Layer Trees Reflect Dif ... 
- smarty模版出现错误提示出现了不期望的字符
			2013年7月5日 08:38:49 提示 unexpected "字符或字符串" 查找前边的代码,看是否有字符串单引号或双引号没有成对出现的情况 
- codeforces  A. Strange Addition  解题报告
			题目链接:http://codeforces.com/problemset/problem/305/A 题目意思:给出一个序列,需要从中选择一些数,这些数需要满足:任意的两个数中每一位至少有一个数满足 ... 
- 爱改名的小融 2(codevs 3149)
			3149 爱改名的小融 2 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description Wikioi上有个人 ... 
- SVN服务器搭建和使用(一)(转载)
			转载地址:http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html Subversion是优秀的版本控制工具,其具体的的优点 ... 
- C#控制管理VisualSVN Server                                                    分类:            C#             2014-05-29 15:51    796人阅读    评论(0)    收藏
			VisualSVN Server可以用WMI接口管理(Windows Management Instrumentation). VisualSVN Server安装的计算机中,位于%VISUALSVN ... 
- poj 1459  多源多汇点最大流
			Sample Input 2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20 7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 ... 
