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: ...
随机推荐
- 破解TP-Link路由-嗅探PPPoE拨号密码
如果你平时都使用路由器直接上网,那么你还记得你的宽带(ADSL)帐户名和密码吗?忘记密码后又该如何找回呢?别急,本文带你一同找回遗忘的ADSL密码.1.安全性较差的路由器(例如腾达的某些路由器):这里 ...
- KMP算法心得
今天又看了一遍KMP,感觉真的懂了...就来这儿发一下心得吧. KMP算法其实就是暴力的改进版.让我们看看暴力的匹配. Original string: ababababcbbababababc Pa ...
- 用VMware安装虚拟系统时出现Invalid system disk,Replace the disk and then press any key
VMware 默认是第一次从光盘启动,第二次从硬盘启动,你刚分区,里面还没有系统,当然报这个错,再次从光盘启动需要设置 VMware 的 BIOS,重新启动虚拟系统,当出现 VMware 的图标时用鼠 ...
- TypeError: document.formname.submit is not a function
<form name="formname" ...> .... <input name="submit" type="submit& ...
- 使用Webdriver执行JS小结
首先,我们使用如下方式初始化driver: WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptEx ...
- cas单点注销失败Error Sending message to url endpoint
最近在做cas单点登录时,由于是单点登录.必然会涉及到单点注销,然而在做单点注销时由于对cas注销机制不了解加之测试条件所致,所有测试都是在本机下完成(机器性能较低,没用虚拟机):导致折腾了很久.网上 ...
- CSS 样式显示为小手
因为工作需要把鼠标放上去显示小手形状, css样式如下: style="cursor:hand" 部分浏览器支持 style="cursor:pointer&quo ...
- [Android UI] Shape详解 (GradientDrawable)
转载自:http://blog.csdn.net/feng88724/article/details/6398193 在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来 ...
- 在Win8中创建热点,共享网络
在Win8中创建热点,共享网络 办公室中,我独享10M光纤,没什么要下的,便想利用来更新下Ipad里面的程序,下点公开课.那在不利用软件[用很多wifi共享的软件],从win7开始 系统本身就自带相关 ...
- java 执行command
StringBuffer buf = new StringBuffer(1000); try { Process pos = Runtime.getRuntime().exec("sh &q ...