1、在二维数组中查找一个值,如果有则返回true,否则返回false。
要求:测试数组是经过排序的,从左到右依次递增,从上到下依次递增。
public class FIndTarget {
public boolean Find(int target, int [][] array) {
if(array==null || array.length==0 || array[0].length==0){
return false;
}
int row = 0;
int column = array[0].length-1;
while( row < array.length && column >= 0){
if(array[row][column] == target){
return true;
}else if(array[row][column] > target){
--column;
}else{
row++;
}
}
return false;
}
}
2、将字符串中的所有空格替换为%20,这是在原字符串上进行修改,所有空间复杂度不高,时间复杂度为O(n)
public class ReplaceSpace {
public String replaceSpace(StringBuffer str) {
if(str==null || str.length()==0){
return "";
}
for(int i = 0; i < str.length(); i++){
char ch = str.charAt(i);
if(ch == ' '){
str.replace(i,i+1,"%20");
}
}
return str.toString();
}
}