#832. Flipping an Image

有順利解出

/**
 * @param {number[][]} image
 * @return {number[][]}
 */
var flipAndInvertImage = function(image) {
    let cloumnLen = image[0].length
    let rowLen = image.length;
    let result = []
    for(let i=0; i<rowLen; i++){
        result[i] = reverse(flipArray(image[i])) // [1,1,0]
    }

    return result
};

function flipArray(arr){
    let result = []
    for(let i=arr.length - 1; i>=0; i--){
        result.push(arr[i])
    }
    return result
}

function reverse(arr){
    let result = []
    for(let i=0; i<arr.length; i++){
        if(arr[i] === 0){
            result.push(1)
        } else {
            result.push(0)
        }
    }
    return result
}
/* 1 1 0          0 1 1.       1 0 0
   1 0 1.     ->  1 0 1.  ->   0 1 0
   0 0 0.         0 0 0.       1 1 1
*/

/**
 y: 0, 1, 2 -> 2, 1, 0 
 */

Last updated

Was this helpful?