• leetcode 832 翻转图像


    package com.example.lettcode.dailyexercises;
    
    /**
     * @Class FlipAndInvertImage
     * @Description 832 翻转图像
     * 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果。
     * 水平翻转图片就是将图片的每一行都进行翻转,即逆序。例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]。
     * 反转图片的意思是图片中的 0 全部被 1 替换, 1 全部被 0 替换。例如,反转 [0, 1, 1] 的结果是 [1, 0, 0]。
     * <p>
     * 示例 1:
     * 输入:[[1,1,0],[1,0,1],[0,0,0]]
     * 输出:[[1,0,0],[0,1,0],[1,1,1]]
     * 解释:首先翻转每一行: [[0,1,1],[1,0,1],[0,0,0]];
     * 然后反转图片: [[1,0,0],[0,1,0],[1,1,1]]
     * <p>
     * 示例 2:
     * 输入:[[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
     * 输出:[[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
     * 解释:首先翻转每一行: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]];
     * 然后反转图片: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
     * <p>
     * 提示:
     * 1 <= A.length = A[0].length <= 20
     * 0 <= A[i][j] <= 1
     * @Author
     * @Date 2021/2/24
     **/
    public class FlipAndInvertImage {
        public static int[][] flipAndInvertImage(int[][] A) {
            if (A == null || A.length == 0 || A[0].length == 0) return A;
            int[][] cloneArray = A.clone();
            int row = cloneArray.length;
            int col = cloneArray[0].length;
            for (int j = 0; j < col / 2; j++) {
                for (int i = 0; i < row; i++) {
                    if (cloneArray[i][j] == 0) cloneArray[i][j] = 1;
                    else cloneArray[i][j] = 0;
                    if (cloneArray[i][col - j - 1] == 0) cloneArray[i][col - j - 1] = 1;
                    else cloneArray[i][col - j - 1] = 0;
    
                    cloneArray[i][j] = cloneArray[i][j] + cloneArray[i][col - j - 1];
                    cloneArray[i][col - j - 1] = cloneArray[i][j] - cloneArray[i][col - j - 1];
                    cloneArray[i][j] = cloneArray[i][j] - cloneArray[i][col - j - 1];
                }
            }
            if (col % 2 != 0) {
                for (int i = 0; i < row; i++) {
                    if (cloneArray[i][col / 2] == 0) cloneArray[i][col / 2] = 1;
                    else cloneArray[i][col / 2] = 0;
                }
            }
    
            return cloneArray;
        }
    }
    
    // 测试用例
    public static void main(String[] args) {
    	int[][] A = new int[][]{{1, 1, 0}, {1, 0, 1}, {0, 0, 0}};
    	int[][] ans = FlipAndInvertImage.flipAndInvertImage(A);
    	System.out.println("FlipAndInvertImage demo01:");
    	for (int i = 0; i < ans.length; i++) {
    		System.out.println("");
    		for (int j = 0; j < ans[0].length; j++) {
    			System.out.print("," + ans[i][j]);
    		}
    	}
    	System.out.println("");
    
    	A = new int[][]{{1, 1, 0, 0}, {1, 0, 0, 1}, {0, 1, 1, 1}, {1, 0, 1, 0}};
    	ans = FlipAndInvertImage.flipAndInvertImage(A);
    	System.out.println("FlipAndInvertImage demo02:");
    	for (int i = 0; i < ans.length; i++) {
    		System.out.println("");
    		for (int j = 0; j < ans[0].length; j++) {
    			System.out.print("," + ans[i][j]);
    		}
    	}
    	System.out.println("");
    }
    
  • 相关阅读:
    .net的委托和事件的直接理解[转载]
    虚函数 多态 函数重载[转载]
    自动化 object <> xml
    用System.Web.HttpRequest模拟一个form的post
    【WP7】动画的使用
    【WP7】关于ListBox控件的数据绑定
    【WP7】坐标变换
    【WP7】页面之间数据交互
    【WP7】调用系统LED灯
    【WP7】MediaPlayer与关闭音乐的方法
  • 原文地址:https://www.cnblogs.com/fyusac/p/14441827.html
Copyright © 2020-2023  润新知