博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面试需要的基础知识-二维数组中的查找
阅读量:7071 次
发布时间:2019-06-28

本文共 2914 字,大约阅读时间需要 9 分钟。

hot3.png

题目

在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

解题思路:

首先选取数组中右上角的数字。如果该数字等于要查找的数字,查找过程结束;如果该数字大于要查找的数字,剔除这个数字所在的列;如果该数字小于要查找的数字,剔除这个数字所在的行。

代码

/** *  */package com.jason.interviewQuestions.basicKnowledge;/** * 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 * @author jason zhang * */public class FindInPartiallySortedMatirx {	/**	 * 从右上角开始查找	 * @param matirx 被查找的数组	 * @param rows 数组的行数	 * @param columns 数组的列数	 * @param number 查找的数字	 * @return	 */	boolean findFromUpperRight(int[][] matrix, int rows, int columns, int number) {		boolean found = false;		if (matrix != null && rows > 0 && columns > 0) {			int row = 0;			int column = columns - 1;			while (row < rows && column >= 0) {				if (matrix[row][column] == number) {					found = true;					System.out.println("find the number at " + (row+1) + " row and " + (column+1) + " column");					break;				} else if (matrix[row][column] > number) {					//右上角的数字大于要查找的数字,剔除这个数字所在的列					column--;				} else {					//右上角的数字小于要查找的数字,剔除这个数字所在的行					row++;				}			}		}		return found;	}		/**	 * 从左下角开始查找	 * @param matirx 被查找的数组	 * @param rows 数组的行数	 * @param columns 数组的列数	 * @param number 查找的数字	 * @return	 */	boolean findFromLeftLower(int[][] matrix, int rows, int columns, int number) {		boolean found = false;		if (matrix != null && rows > 0 && columns > 0) {			int row = rows - 1;			int column = 0;			while (row >= 0 && column < columns) {				if (matrix[row][column] == number) {					found = true;					System.out.println("find the number at " + (row+1) + " row and " + (column+1) + " column");					break;				} else if (matrix[row][column] > number) {					//左下角的数字大于要查找的数字,剔除这个数字所在的行					row--;				} else {					//左下角的数字小于要查找的数字,剔除这个数字所在的列					column++;									}			}		}		return found;	}}

测试代码

package com.jason.interviewQuestions.basicKnowledge;import junit.framework.TestCase;public class FindInPartiallySortedMatirxTest extends TestCase {	private FindInPartiallySortedMatirx fipsm;	private int[][] matrix = {			{1,2,8,9},			{2,4,9,12},			{4,7,10,13},			{6,8,11,15}	};;	protected void setUp() throws Exception {		super.setUp();		this.fipsm = new FindInPartiallySortedMatirx();	}	public void testFindFromUpperRight() {		int number = 15;		boolean find = this.fipsm.findFromUpperRight(matrix, 4, 4, number);		super.assertTrue(find);			}		public void testFindFromUpperRightNF() {		int number = 3;		boolean find = this.fipsm.findFromUpperRight(matrix, 4, 4, number);		super.assertFalse(find);			}		public void testfindFromLeftLower() {		int number = 7;		boolean find = this.fipsm.findFromUpperRight(matrix, 4, 4, number);		super.assertTrue(find);			}		public void testfindFromLeftLowerNF() {		int number = 3;		boolean find = this.fipsm.findFromUpperRight(matrix, 4, 4, number);		super.assertFalse(find);			}}

备注:

题目来自于何海涛著的《剑指offer》,书中使用c实现,本文使用java实现。

转载于:https://my.oschina.net/u/914897/blog/471388

你可能感兴趣的文章
6.4. Moose File System
查看>>
Ipad也怕冷?!
查看>>
论文阅读之 Inferring Analogous Attributes CVPR 2014
查看>>
【转】Spring mvc集成ZBUS--轻量级MQ、RPC、服务总线
查看>>
5.35. application.properties
查看>>
[Everyday Mathematics]20150201
查看>>
第 75 章 Network Attached Storage(NAS 网络附加存储)
查看>>
docker -v挂载数据卷网络异常的问题
查看>>
《时间的朋友》跨年演讲金句
查看>>
移动前端UI选择
查看>>
SAP MIGO to Cancel Material Doc., Error Msg - Transaction code MBST not defined.
查看>>
食品安全中的那些事故
查看>>
[20150205]关于位图索引7.txt
查看>>
20150213关于共享池4-SQL内存结构父子游标
查看>>
井底之蛙
查看>>
careercup-扩展性和存储限制10.3
查看>>
合并多个工作薄workbooks到一个工作薄workbook
查看>>
公司的一个面试题:如何用css让一个容器水平垂直居中?
查看>>
Linux概念架构的理解(转)
查看>>
.Net 转战 Android 4.4 日常笔记目录
查看>>