(Easy) Flipping an Image LeetCode

Description:

Given a binary matrix A, 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 each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].

Example 1:

Input: [[1,1,0],[1,0,1],[0,0,0]]
Output: [[1,0,0],[0,1,0],[1,1,1]]
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]

Example 2:

Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]

Notes:

  • 1 <= A.length = A[0].length <= 20
  • 0 <= A[i][j] <= 1

Solution

class Solution {
    public int[][] flipAndInvertImage(int[][] A) {

        if(A==null || A.length ==0 ||A[0].length==0){
            return null;
        }

        int [][] Res = new int[A[0].length][];

        for(int i =0; i< A[0].length; i++){

            Res[i] = revert(A[i]);
        }

        return flip(Res);
    }

    public int[] revert (int[] A){

        int [] B = new int[A.length];

        for(int i=A.length-1;i>=0; i-- ){

            B[A.length-i-1] = A[i];
        }

        return B;
    }

    public int[][] flip(int[][] A){

        for (int i =0; i<A.length; i++){
            for(int j = 0; j<A[0].length; j++){
                if(A[i][j]==0){
                    A[i][j]=1;
                }
                else{
                    A[i][j]=0;
                }
            }
        }

        return A;
    }
}

原文地址:https://www.cnblogs.com/codingyangmao/p/11306279.html

时间: 2024-11-06 09:44:57

(Easy) Flipping an Image LeetCode的相关文章

(Easy) Diet Plan Performance LeetCode Contest

Description: 5174. Diet Plan Performance My SubmissionsBack to Contest User Accepted:0 User Tried:0 Total Accepted:0 Total Submissions:0 Difficulty:Easy A dieter consumes calories[i] calories on the i-th day.  For every consecutive sequence of k days

(Easy) To Lower Case LeetCode

Description Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here" Example 3: In

(Easy) Last Stone Weight LeetCode

class Solution { public int lastStoneWeight(int[] stones) { int len = stones.length; int i = len -1; int minus = 0; int remain = len; if(stones.length ==1){ return stones[0]; } else { Arrays.sort(stones); do{ if(stones[i]==stones[i-1]){ stones[i] = 0

(Easy) Occurences After Bigram LeetCode

Description: Given words first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after second. For each such occurrence, add "third"

(Easy) Find Common Characters LeetCode

Description Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates).  For example, if a character occurs 3 times in all strings but not 4 times,

(Easy) Self Dividing Numbers LeetCode

Description: A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Also, a self-dividing number is not allowed to contain the

(Easy) Long Pressed Name LeetCode

Description: Your friend is typing his name into a keyboard.  Sometimes, when typing a character c, the key might get long pressed, and the character will be typed 1 or more times. You examine the typed characters of the keyboard.  Return True if it

(Easy) Reverse linked list LeetCode

Description: Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? Accepted 661,364

(Easy) Can Make Palindrome - LeetCode Contest (in progress)

Description: Given a string s, we make queries on substrings of s. For each query queries[i] = [left, right, k], we may rearrange the substring s[left], ..., s[right], and then choose up to k of them to replace with any lowercase English letter. If t