Java数组面试问题总结

1.字符串替换问题,将字符串中的每一个“a”字符替换成3个“#”字符。要求:时间复杂度:O(n)。备注:用数组操作。

import java.util.Arrays;

public class Day2ZiFuChuanTiHuan {
    public static void main(String[] args) {
        String str = "I am a student";
        /*
        字符串放进数组里
         */
        int length = str.length();//获取字符串的长度
        char ch[] = new char[length];//创建一个和字符串长度大小的一维数组
        ch = str.toCharArray();//字符串放进数组里
        /*
        获取需要扩容的空间
         */
        int countaBlank=getablank(ch);
        /*
        进行扩容
         */
        ch = Arrays.copyOf(ch, ch.length+countaBlank);
        /*
        遍历数组,找a
         */
        for (int i = ch.length-1; i >= 0; i--) {
            if (ch[i] == 'a') {
                /*
                新建一维数组{'#','#','#'};
                 */
                char[] ch1=new char[]{'#','#','#'};
                /*
                数据右移
                 */
                for (int j = ch.length- 3; j >= i; j--) {
                    ch[j + 2] = ch[j];
                }
                /*
                字符串深拷贝
                 */
                System.arraycopy(ch1, 0, ch, i, ch1.length);
            }
        }
        System.out.print(Arrays.toString(ch));
    }
    /*
    统计需要扩容的大小
     */
    private static int getablank(char[] arr){
        int count=0;
        for(int i=0;i

2.整形数组中,通过代码实现奇数在前偶数再后的问题,要求:时间复杂度O(n),空间复杂度O(1)。

public static void fun(int[] arr){
   int start=0;//start指针指向开始
   int end=arr.length-1;//end指针指向末位
if(arr.length==0){
    return;
}
while(start=0 && arr[end]%2==0){
end--;
}
if(start

3.二维数组查找值问题,注意:二位数组从上到下一次递增有序,从左向右一次递增有序。考虑算法效率。

一般方法:

    public static boolean fun(int[][] arr,int key){
       for(int i=0;i

考虑算法效率:

a       a
         
         
         
a       a

目标数字x,选取4个点

(1)当a在左上角时,目标数字x如果小于a,则可以直接退出,返回false,如果目标数字x大于a,查找区域是整个数组范围,花费时间较多。

(2)当a在右上角时,如果目标数字x小于a,则a所在的列可以排除,如果目标数字x大于a,则a所在的行可以排除。所以无论目标数字x大于还是小于a,都可以排除当前的行和列。缩小查找范围。

(3)当a在左下角时,如果目标数字x小于a,则a所在的行可以排除,如果目标数字x大于a,则a所在的列可以排除。同样可以缩小查找区域。该点可以选择。

(4)当a在右下角时,如果目标数字x小于a时,查找区域是整个数组,花费时间较多,当目标数字x大于a时,可直接退出,返回false。

示例分析:

1 2 4 6 9
2 4 5 10 11
4 6 8 12 14
6 7 9 13 16
8 9 10 15 19

选取右上角的数字的方法进行查找7。

(1)7<9,排除9所在的列,在数组row:0-4,column:0-3范围内查找。

(2)7>6,排除6所在的行,在数组row:1-4,column:0-3范围内查找。

(3)7<10,排除10所在的列,在数组row:1-4,column:0-2范围内查找。

(4)7>5,排除5所在的行,在数组row:2-4,column:0-2范围内查找。

(5)7<8,排除8所在的列,在数组row:2-4,column:0-1范围内查找。

(6)7>6,排除6所在的行,在数组row:3-4,column:0-1范围内查找。

(7)7=7,找到目标数字7。返回true。

代码演示:

public static boolean rtfind(int[][] arr,int rows,int columns,int key){
        boolean flag=false;
        if(arr!=null && rows>0 && columns>0){
           int row=0;
           int column=columns-1;
           while(row=0  ){
               if(keyarr[row][column]){
                   row++;
               }else{
                   flag=true;
                   break;
               }
               }
               }
        return flag;
}
public static boolean ltfind(int[][] arr,int rows,int columns,int key){
        boolean flag=false;
        if(arr!=null && rows>0 && columns>0){
            int row=rows-1;
            int column=0;
            while(row=0 && columnarr[row][column]){
                    column++;
                }else
                    flag=true;
                     break;
                }
                }
                }
         return flag;
}
    public static void test(int[][] arr, int rows, int columns, int key, String method) {
        if (arr == null) {
            System.out.println("arr is null!");
        } else {
            if (method == "rtfind") {
                System.out.println(rtfind(arr, rows, columns, key));
            } else if (method == "ltfind") {
                System.out.println(ltfind(arr, rows, columns, key));
            } else {
                System.out.println("error");
            }
        }
    }

    public static void main(String[] args) {
        int[][] arr = {{1, 2, 4, 6, 9}, {2, 4, 5, 10, 11}, {4, 6, 8, 12, 14}, {6, 7, 9, 13, 16}, {8, 9, 10, 15, 19}};
        test(arr, 5, 5, 7, "ltfind");
        test(arr, 5, 5, 12, "rtfind");
        test(arr, 5, 5, 20, "rtfind");
        test(arr, 5, 5, 20, "rtfind");

    }
运行结果:
true
true
false
false

 

你可能感兴趣的:(JavaSE基础)