当前位置:网站首页>Sword finger offer 04 Search in two-dimensional array

Sword finger offer 04 Search in two-dimensional array

2022-07-05 05:28:00 ThE wAlkIng D

Title Description

 Insert picture description here

Problem analysis

  1. Notice how the two-dimensional array is incremented , Locate the lower left corner as the starting point and start traversal according to the incremental method
  2. Be careful .length( For any array , Take out its capacity ) and .length()( For Strings ) The difference between

Code instance

class Solution {
    
    public boolean findNumberIn2DArray(int[][] array, int target) {
    
        int row = array.length - 1;
        int col = 0;
        while(row >= 0 && (col<array[0].length){
    
        	if(array[row][col] > target){
    
        		row--;
        	}
        	else if(array[row][col] < target){
    
        	    col++;
        	}
        	else{
    
        		return true;
        	}
        }
        return false;
    }
}
原网站

版权声明
本文为[ThE wAlkIng D]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050525448771.html