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. 什么是响应式Web设计?怎样进行?

    http://beforweb.com/node/6/page/0/3 开始第一篇.老规矩,先无聊的谈论天气一类的话题.十一长假,天气也终于开始有些秋天的味道,坐在屋里甚至觉得需要热咖啡.话说两年前也 ...

  2. jekyll中文乱码问题

    一般编写都是采用utf-8的吧,但是在windows下安装的jekyll,默认是以GBK编码的方式去读取咱们编写的文件,如此便乱码了. 要解决此问题,总不至于要写GBK编码的文件吧,毕竟这个编码不怎么 ...

  3. Android Unlock Patterns

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  4. Python操作Mysql实例代码教程在线版(查询手册)

    本文介绍了Python操作MYSQL.执行SQL语句.获取结果集.遍历结果集.取得某个字段.获取表字段名.将图片插入数据库.执行事务等各种代码实例和详细介绍,代码居多,是一桌丰盛唯美的代码大餐   实 ...

  5. schedule CCCallfunc CCCallfuncN CCCallfuncND

    schedule(schedule_selector(HelloWorld::step), 1.0f); void HelloWorld::step(float dt) { CCLog("d ...

  6. 67. 总结篇:面试中随机数"等概率"vs"不等概率"生成问题[random generator with equal or unequal probability]

    [本文链接] http://www.cnblogs.com/hellogiser/p/random-generator-with-equal-or-unequal-probability.html 1 ...

  7. ORACLE查询某一字段重复的数据

    第一种方法: select a.*  from ASSET_MAINTAIN a inner join ASSET_MAINTAIN b on a.asset_id=b.asset_id and a. ...

  8. UESTC 1215 (思维题 旋转)

    Secrete Master Plan Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  9. android 实现自定义卫星菜单

    看了hyman老师的视频,听起来有点迷糊,所以就想把实现卫星菜单的实现总结一下.长话短说,下面总结一下: 一.自定义ViewGroup1).自定义属性文件 属性的定义: <attr name=& ...

  10. Git Server & Git Hook

    http://ju.outofmemory.cn/entry/16893 我喜欢 github,我现在的个人代码全部是托管在上面了,但是一些公司或者某些项目不适合放入github中,你希望能有一个完全 ...