2020-11-15:手寫代碼:行有序、列也有序的二維陣列中,找num,找到回傳true,否則false?#福大大架構師每日一題#
uj5u.com熱心網友回復:
從右上查找
public boolean find(int num, int [][] array) {
int rows = array.length;
if(rows == 0){
return false;
}
int cols = array[0].length;
if(cols == 0){
return false;
}
// 右上
int row = 0;
int col = cols-1;
while(row<rows && col>=0){
if(array[row][col] < num){
row++;
}else if(array[row][col] > num){
col--;
}else{
return true;
}
}
return false;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/218714.html
標籤:Java相關
上一篇:java為什么我一運行程式就出現Launch configuration Tomcat.1 references closed project game
下一篇:虛函式&虛繼承
