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的更多相关文章

  1. CC150 - 11.5

    Question: Given a sorted array of strings which is interspersed with empty strings, write a method t ...

  2. CC150 - 11.3

    Question: Given a sorted array of n integers that has been rotated an unknown number of times, write ...

  3. CC150 - 11.2

    Question: Write a method to sort an array of strings so that all the anagrams are next to each other ...

  4. CC150 - 11.1

    Question: You are given two sorted arrays, A and B, where A has a large enough buffer at the end to ...

  5. 地区sql

    /*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ...

  6. 11.8---维护x的秩(CC150)

    思路:比较easy.就是借助hashset让他有序然后就能够比较节省时间了. 答案: public static int[] getRankOfNumber(int[] a, int n){ int[ ...

  7. 11.7---叠罗汉表演节目(CC150)

    1,牛客网第一题:这其实跟找最长递增子序列是一个东西.注意的地方是,返回的是最大的dp,而不是dp[N-1]. 答案: public static int getHeight(int[] men, i ...

  8. 11.6---矩阵查找元素(CC150)

    思路,一旦提到查找就要想到二分查找. public static int[] findElement(int[][] a, int n, int m, int key) { // write code ...

  9. 11.5---含有空字符串的字符串查找(CC150)

    注意,1,"" 和 " ".是不同的,空字符串指的是"": 2,注意String的compareTo.小于是指<0.并不是==-1: ...

随机推荐

  1. zookeeper 配置详解

    http://blog.csdn.net/shenlan211314/article/details/6185176  因博主原创,所以不能转载 下面是更为详细的配置说明: 前面两篇文章介绍了Zook ...

  2. Java和C#运行速度对比:Java比C#快约3倍

    测试条件: Java版本: Java 8, .NET版本:v4.0, Release模式,针对x86平台优化 单线程模式. 测试1:(测试1的代码摘自http://blog.csdn.net/houj ...

  3. python setup.py uninstall

    I have installed a python package with python setup.py install How do I uninstall it? ============== ...

  4. Android Volley获取json格式的数据

    为了让Android能够快速地访问网络和解析通用的数据格式Google专门推出了Volley库,用于Android系统的网络传输.volley库可以方便地获取远程服务器的图片.字符串.json对象和j ...

  5. google maps js v3 api教程(3) -- 创建infowindow

    原文地址 前面我们学习了地图和标记的创建.那么今天我们来学习怎样在地图上显示一个窗口(infowindow) infowindow构造函数为:InfoWindow(opts?:InfoWindowOp ...

  6. Machine Schedule(poj 1274)

    题目大意:有n个奶牛和m个谷仓,现在每个奶牛有自己喜欢去的谷仓,并且它们只会去自己喜欢的谷仓吃东西,问最多有多少奶牛能够吃到东西 输入第一行给出n与m 接着n行 每行第一个数代表这个奶牛喜欢的谷仓的个 ...

  7. 或许您还不知道的八款Android开源游戏引擎

    很多初学Android游戏开发的朋友,往往会显得有些无所适从,他们常常不知道该从何处入手,每当遇到自己无法解决的难题时,又往往会一边羡慕于iPhone下有诸如Cocos2d-iphone之类的免费游戏 ...

  8. QML入门教程

    QML是Qt推出的Qt Quick技术的一部分,是一种新增的简便易学的语言.QML是一种陈述性语言,用来描述一个程序的用户界面:无论是什么样子,以及它如何表现.在QML,一个用户界面被指定为具有属性的 ...

  9. 学习Hadoop整体理解

    HDFS是Hadoop的核心模块之一,围绕HDFS是什么.HDFS的设计思想和HDFS的体系结构三方面来介绍. Hadoop的设计思想受到Google公司的GFS设计思想的启示,基于一种开源的理念实现 ...

  10. OGNL表达式struts2标签“%,#,$”

    一.什么是OGNL,有什么特点? OGNL(Object-Graph Navigation Language),大概可以理解为:对象图形化导航语言.是一种可以方便地操作对象属性的开源表达式语言.OGN ...