力扣---2020.6.5

面试题29. 顺时针打印矩阵

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if(matrix.length == 0) return new int[0];
        int l = 0, r = matrix[0].length - 1, t = 0, b = matrix.length - 1, x = 0;
        int[] res = new int[(r + 1) * (b + 1)];
        while(true) {
            for(int i = l; i <= r; i++) res[x++] = matrix[t][i]; // left to right.
            if(++t > b) break;
            for(int i = t; i <= b; i++) res[x++] = matrix[i][r]; // top to bottom.
            if(l > --r) break;
            for(int i = r; i >= l; i--) res[x++] = matrix[b][i]; // right to left.
            if(t > --b) break;
            for(int i = b; i >= t; i--) res[x++] = matrix[i][l]; // bottom to top.
            if(++l > r) break;
        }
        return res;
    }
}
class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return new int[0];
        }
        int rows = matrix.length, columns = matrix[0].length;
        int[] order = new int[rows * columns];
        int index = 0;
        int left = 0, right = columns - 1, top = 0, bottom = rows - 1;
        while (left <= right && top <= bottom) {
            for (int column = left; column <= right; column++) {
                order[index++] = matrix[top][column];
            }
            for (int row = top + 1; row <= bottom; row++) {
                order[index++] = matrix[row][right];
            }
            if (left < right && top < bottom) {
                for (int column = right - 1; column > left; column--) {
                    order[index++] = matrix[bottom][column];
                }
                for (int row = bottom; row > top; row--) {
                    order[index++] = matrix[row][left];
                }
            }
            left++;
            right--;
            top++;
            bottom--;
        }
        return order;
    }
}

面试题 16.21. 交换和

class Solution {
    public int[] findSwapValues(int[] array1, int[] array2) {
        /*
        * 假设array1的和为sum1,要交换的元素为x1. array2的和为sum2,要交换的元素为x2.
        *       为了满足题目要求就有: sum1 - x1 + x2 = sum2 - x2 + x1
        *                        => sum1 - sum2 = 2(x1 - x2) 
        *                        => (sum1 - sum2) / 2 = x1 - x2
        *                        => x1 = sum/2 + x2
        *                        => x2 = x1 - sum/2
        */
        Set<Integer> set = new HashSet<>();
        int sum = 0;
        for(int arr : array1){
            sum += arr;
        }
        for(int arr : array2){
            sum -= arr;
            set.add(arr);
        }
        if(sum%2!=0){
            return new int[0];
        }
        for(int arr : array1){
            if(set.contains(arr - sum/2)){
                return new int[]{arr,arr - sum/2};
            }
        }
        return new int[0];
    }
}

面试题 01.05. 一次编辑

class Solution {
    public boolean oneEditAway(String first, String second) {
        int len = first.length()-second.length();
        if(Math.abs(len)>1){
            return false;
        }
        int count = 1;
        for(int i = 0,j = 0;i<first.length()&&j<second.length();i++,j++){
            if(first.charAt(i)!=second.charAt(j)){
                if(len==1){
                    j--; //second要不要添加一个字符
                }else if(len==-1){
                    i--;  //second要不要删除一个字符
                }
                count--;
            }
            if(count<0){
                return false;
            }
        }
        return true;
    }
}
//主站 72. 编辑距离
class Solution {
    public boolean oneEditAway(String first, String second) {
        int m = first.length();
        int n = second.length();
        if(Math.abs(m-n)>1) return false;
        int[][] dp = new int[m+1][n+1];
        //dp[i][j]  表示 到first的第i个字母,second的第j个字母,最少修改的次数为dp[i][j]
        for(int i = 0;i<=m;i++){
            dp[i][0] = i;
        }
        for(int i = 0;i<=n;i++){
            dp[0][i] = i;
        }

        for(int i = 1;i<=m;i++){
            for(int j = 1;j<=n;j++){
                if(first.charAt(i-1)==second.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1];
                }else{
                    dp[i][j] = Math.min(Math.min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1;
                }
            }
        }
        return dp[m][n]<2;
    }
}

你知道的越多,你不知道的越多。

你可能感兴趣的:(数据结构与算法)