《程序员代码面试指南》第八章 数组和矩阵问题 在行列都排好序的矩阵中找数...
生活随笔
收集整理的這篇文章主要介紹了
《程序员代码面试指南》第八章 数组和矩阵问题 在行列都排好序的矩阵中找数...
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目
在行列都排好序的矩陣中找數(shù)java代碼
package com.lizhouwei;/*** @Description: 在行列都排好序的矩陣中找數(shù)* @Author: lizhouwei* @CreateDate: 2018/5/7 20:49* @Modify by:* @ModifyDate:*/ public class Chapter8_7 {public boolean isContains(int[][] matrix, int k) {int tR = 0;int tC = 0;int dR = matrix.length-1;int dC = matrix[0].length-1;while (tR <= dR && tC <= dC) {if (matrix[tR][dC] < k) {tR++;} else if (matrix[tR][dC] > k) {dC--;} else {return true;}}return false;}//測試public static void main(String[] args) {Chapter8_7 chapter = new Chapter8_7();int[][] matrix = {{0, 1, 2, 5}, {2, 3, 4, 7}, {4, 4, 4, 8}, {5, 7, 7, 9}};System.out.println("矩陣 matrix = {{0,1,2,5},{2,3,4,7},{4,4,4,8},{5,7,7,9}}中");System.out.println("是否包含 7:" + chapter.isContains(matrix, 7));} }結(jié)果
轉(zhuǎn)載于:https://www.cnblogs.com/lizhouwei/p/9004936.html
總結(jié)
以上是生活随笔為你收集整理的《程序员代码面试指南》第八章 数组和矩阵问题 在行列都排好序的矩阵中找数...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: amazeui学习笔记一(开始使用4)-
- 下一篇: SOFA 源码分析 — 连接管理器