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: ...
随机推荐
- Recover Rotated Sorted Array
Given a rotated sorted array, recover it to sorted array in-place. Clarification What is rotated arr ...
- 用几条shell命令快速去重10G数据
试想一下,如果有10G数据,或者更多:怎么才能够快速地去重呢?你会说将数据导入到数据库(mysql等)进行去重,或者用java写个程序进行去重,或者用Hadoop进行处理.如果是大量的数据要写入数据库 ...
- windows下批量删除文件
FORFILES /P d:\www /D -7 /S /M ex*.log /C "cmd /c del @path" 删除d:\www目录下7天前ex*.log的所有文件 例子 ...
- NSUrlConnection 和 NSUrlRequest 的关系
开始看到这2个名字,总感觉NSUrlConnection才是主要的网络请求类,其实不是,先看官方文档 An NSURLConnection object lets you load the conte ...
- Linux服务器通过rz/sz轻松上传下载文件
Linux服务器通过命令行远程访问时,上传文件还需要ftp所以不太方便,可以使用rz这个小工具来上传不太大的文件,方法如下: 输入rz,如果提示命令不存在,证明还没有安装,以CentOS为例,安装命令 ...
- Java for LeetCode 172 Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Java for LeetCode 051 N-Queens
Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a d ...
- [编解码] 关于base64编码的原理及实现
转载自: http://www.cnblogs.com/hongru/archive/2012/01/14/2321397.html [Base64]-base64的编码都是按字符串长度,以每3个8b ...
- 【图文详解】python爬虫实战——5分钟做个图片自动下载器
python爬虫实战——图片自动下载器 之前介绍了那么多基本知识[Python爬虫]入门知识,(没看的先去看!!)大家也估计手痒了.想要实际做个小东西来看看,毕竟: talk is cheap sho ...
- hp unix
HP-UNIX 命令大全 #vgdisplay -v vgname #lvdisplay -v lvfullpath #pvdisplay -v pvfullpath # ioscan –fnkC d ...