java中:
获取二维数组的行数:array.length
获取二维数组的列数:array[0].length
是.length String是.length()
C++中:
获取数组的长度为.size()
从左上开始寻找target,大则–列,小则++行
java实现:
public class Solution {
public boolean Find(int target, int [][] array) {
int hang = 0;
int lie =array[0].length-1;
while(hang<array.length && lie>=0){
if(array[hang][lie]==target)
return true;
else{
if(array[hang][lie]>target)
--lie;
else
++hang;
}
}
return false;
}
}
C++:
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int hang = 0;
int lie = array[0].size() - 1;
while(hang <= array.size() -1 && lie >=0){
if(array[hang][lie]==target)
return true;
else{
if(array[hang][lie]>target)
--lie;
else
++hang;
}
}
return false;
}
};