Given a binary matrixA, we want to flip the image horizontally, then invert it, and return the resulting image.

To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].

To invert an image means that each0is replaced by1, and each1is replaced by0. For example, inverting [0, 1, 1] results in [1, 0, 0].

依然没啥意思,新加的easy题都不太可能面到,不知道为啥还要加

class Solution {
    public int[][] flipAndInvertImage(int[][] A) {
        if(A == null || A.length == 0 || A[0] == null || A[0].length == 0)
            return A;

        int m = A.length;
        int n = A[0].length;

        for(int i = 0; i < m; i++) {
            int start = 0;
            int end = n - 1;
            while(start < end)
                swap(A[i], start++, end--);
        }

        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                A[i][j] = 1 ^ A[i][j];
            }
        }

        return A;
    }

    public void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

results matching ""

    No results matching ""