LeetCode算法题-Flipping an Image(Java实现)

leetcode伪代码-flip-an-image:翻转图像 leetcode伪代码flipping-an-image 题目来源: 原文: Given a binary matrix A, we want to flip the image horizo​​ntally, then invert it, and return the resulting image. To flip an image horizo​​ntally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizo​​ntally 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]. 解读: 给定二元矩阵A, 目标是要把矩阵做两个操作 第一个是flip 就是让原本每个A[i][j] = A[i][len 立即下载

这是悦乐书的第324次更新,第347篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第194题(顺位题号是832)。给定二进制矩阵A,我们想要水平翻转图像,然后反转它,并返回结果图像。


水平翻转图像意味着图像的每一行都是相反的。 例如,水平翻转[1,1,0]会产生[0,1,1]。

反转图像意味着每个0替换为1,每个1替换为0。例如,反转[0,1,1]会产生[1,0,0]。


例如:

输入:[[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]]。


输入:[[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]]。


注意:

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

  • 0 <= A [i] [j] <= 1

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

从题目的描述和示例来看,我们需要对二维数组进行两次操作,一是反转,二是换值。

反转操作,首先需要拿到二维数组A中每一行数据,也就是一个一维数组,然后对该数组进行元素互换,利用双指针思路。换值操作,将0换成1,1换成0即可。

public int[][] flipAndInvertImage(int[][] A) {
    int row = A.length;
    for (int i=0; i<row; i++) {
        reverse(A[i]);
        invert(A[i]);
    }
    return A;
}

public void reverse(int[] rows){
    int start = 0, end = rows.length-1;
    while (start < end) {
        int temp = rows[start];
        rows[start] = rows[end];
        rows[end] = temp;
        start++;
        end--;
    }
}

public void invert(int[] rows){
    int n = rows.length;
    for (int i=0; i<n; i++) {
        if (rows[i] == 0) {
            rows[i] = 1;
        } else {
            rows[i] = 0;
        }
    }
}

03 第二种解法

我们还可以简化下,将反转和换值放在一个循环中处理。反转依旧是使用双指针进行替换,换值则利用位运算异或,异或运算的规则是对应位不同就取1,1对1进行异或运算的结果是0,1对0进行异或运算的结果是1。直接将数组元素与1进行异或运算,就能达到换值的效果。

public int[][] flipAndInvertImage2(int[][] A) {
    for (int[] rows : A) {
        int start = 0, end = rows.length-1;
        while (start <= end) {
            int temp = rows[start]^1;
            rows[start] = rows[end]^1;
            rows[end] = temp;
            start++;
            end--;
        }
    }
    return A;
}

04 小结

算法专题目前已日更超过五个月,算法题文章194+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

sentaurus浪涌仿真sdevice代码解释(4) 9. `Plot(FilePrefix="Surge_n@node@" time=(Range=(0 @<0+10E-3>@) Intervals=10) NoOverWrite)`:设置了一个绘图过程,用于在瞬态分析过程中绘制结果,文件名前缀为`Surge_n@node@`,时间范围从0到`10E-3`秒,每隔10个时间点绘制一次,且不覆盖之前的结果。同样设置了最大迭代次数和线搜索阻尼系数。这个过程将模拟从`0`到`10E-3`秒内器件的行为,并且会在不同的时间点记录和绘制电流和电势的数据。 阅读详情

相关推荐

Claude Code + 自定义模型体验

AI 编程工具的发展突飞猛进:Cursor 横空出世,推出了“Agent 模式”;新竞争者如 Windsurf 也纷纷加入战局。几乎所有产品都开始转向“代理式(agentic)”开发模式——不再依赖单次 LLM 响应进行补全,而是让大模型在一个循环中调用各种工具,来完成更复杂的任务:分析代码库上下文、读取网页与文档、编译程序、运行测试、修复失败构建或测试、不断迭代等。

西京刀客 5805

CAD矩形绘图全攻略:从REC参数到坐标控制与工程实践

在CAD制图中,矩形命令(RECTANG)看似简单,实则是构建精确图形的基础。许多绘图者常因忽略命令参数、混淆坐标输入方式或缺少验证流程而导致图纸偏差。理解绝对坐标、相对坐标与极坐标的区别,掌握对象捕捉与正交模式的使用,是高效准确绘图的核心前提。矩形本质上是闭合多段线,这一特性使其便于整体偏移与编辑,但也需注意修改单边时需要分解。从图框绘制到圆角面板设计,再到尺寸标注与面积验证,矩形命令贯穿工程制图的完整流程。本文以工程实践为导向,系统讲解矩形绘制的四种方式与参数设置,并结合综合练习帮助读者建立稳定的操作习

weixin_34080951的博客 462

ESP32连接OneNet云平台实现多设备联动控制完整示例

通过esp32连接onenet云平台,实现远程设备间的智能联动控制,利用MQTT协议完成数据通信,esp32连接onenet云平台后可高效稳定地上传与下发指令,提升物联网系统响应速度与自动化水平。

weixin_42360733的博客 549

给定一个二进制矩阵a JAVA,空空间二进制矩阵:Java

Here is my question: How to calcul the Kernel of a Binary Matrix ??To calcul the Kernel (or Null Space if you prefer) in Java, it's pretty simple in the Real Space, there is already a lot of classes, ...

weixin_30249465的博客 151

概率数据关联(PDA):多目标跟踪的基石算法

1. 为什么需要概率数据关联? 2. 核心思想与假设条件 3. 算法核心:概率分配与状态更新 4. 典型应用场景与优势 5. 挑战与改进方向 6. 下期预告

qq_44965657的博客 1468

Silvaco二极管、三极管、CMOS的制备

Silvaco二极管和三极管的制备

yytu231的博客 5753

科大讯飞笔试题8.15(java)

1.给定一个无符号32位整数N,求N的二进制表示法中含1的个数 此题是整数,不考虑负数情况,以下四种方法均可,由于是无符号32位,所以int不符合,要使用long类型 法一:取余,但对于负数不适用 输入:-1 输出:0 输入:8 输出:1 import java.util.Scanner; public class Member { public static void main(String[] args) { Scanner sc=new Scanner(Syste

菜鸟的自我救赎的博客 1450

Vue3 视频播放器完整指南 – @videojs-player/vue 从入门到精通

全面解析 Vue3 生态中最强大的视频播放解决方案 @videojs-player/vue。从基础安装到高级功能,涵盖自动播放、直播流、字幕支持等核心特性。提供完整的代码示例和最佳实践,助你快速构建专业的视频播放应用。

前端开发-前端技术分享 1428

给定一个填充有0和1的二维二进制矩阵,找到仅包含1的最大矩形并返回其面积。

public class MaximalRectangle { public MaximalRectangle(){} public int maximalRectangle(char[][] matrix) { if(matrix.length == 0 || matrix[0].length == 0) {return 0;} int maxarea= 0; i...

gz9876543210的博客 8609

TikTok滑块验证码verifyV2逆向实战:从fp生成到captchabody加密的完整流程解析

本文详细解析了TikTok滑块验证码verifyV2的逆向工程全流程,从fp参数生成到captchabody加密机制,涵盖了验证码触发、WASM解密、轨迹模拟等关键技术点。通过实战案例和代码示例,帮助开发者深入理解反爬机制,提升逆向工程能力,应对复杂的验证挑战。

weixin_28568819的博客 484

【数据集】CMIP6气候模式数据下载

简介CMIP6及其气候模式数据下载

WW、forever的博客 9360

LeetCode 832 Flipping an Image

LeetCode 832 Flipping an Image 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 t...

个人博客 444

46、聚类算法在智能交通数据分析中的应用

本文探讨了聚类算法在智能交通数据分析中的应用,介绍了K-均值算法、K-中心点算法和基于模型的聚类算法的基本原理及特点,并结合实际交通数据讨论了其应用场景与挑战。通过聚类分析,能够从海量交通数据中发现隐藏模式,为交通管理和规划提供科学依据。文章还展示了具体的案例分析和实现流程,为未来智能交通系统的发展提供了参考。

zz67890的博客 134

[Leetcode学习-java]Flipping an Image

问题: 难度:easy 说明: 输入一个 int[][] 然后把数组所有数字镜面翻转,然后 0 1 反转,输入元素只有 0 1。 题目连接:https://leetcode.com/problems/flipping-an-image/ 输入案例: 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,.

qq_28033719的博客 167

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Leetcode 832. Flipping an Image

题目原文: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 ex...

mori644的博客 815

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

[leetcode] 832. Flipping an Image

题目: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 exam...

jing16337305的博客 739

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Leetcode 832. Flipping an Image, Java解法

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 examp...

YuanTheCoder的博客 251

【Leetcode】832. Flipping an Image

题目地址: https://leetcode.com/problems/flipping-an-image/ 给定一个二维0−10-10−1矩阵,要求将其每行水平翻转,并把000处变为111,111处变为000。代码如下: public class Solution { public int[][] flipAndInvertImage(int[][] A) { for (...

数学、算法爱好者的博客 157

[LeetCode] 832. Flipping an Image

题目描述 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...

carbon06的博客 647

832.Flipping an Image

LeetCode解题思路随记LeetCode第832题 解题思路镜像反转01替换小结分析最优解总结 LeetCode第832题 解题思路 Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image h...

weixin_43408733的博客 147

LeetCode——832. 翻转图像(Flipping an Image)[简单]——分析及代码(Java)

LeetCode——832. 翻转图像[Flipping an Image][简单]——分析及代码[Java]一、题目二、分析及代码1. 双指针模拟(1)思路(2)代码(3)结果三、其他 一、题目 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果。 水平翻转图片就是将图片的每一行都进行翻转,即逆序。例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]。 反转图片的意思是图片中的 0 全部被 1 替

江南土豆的博客 256

【LeetCode】832. Flipping an Image 翻转图像(Easy)(JAVA)

【LeetCode】832. Flipping an Image 翻转图像(Easy)(JAVA) 题目地址: https://leetcode.com/problems/perfect-squares/ 题目描述: 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 mean

qq_16927853的博客 227

LeetCode 832. Flipping an Image

LeetCode 832. Flipping an Image (Easy) 主要知识点:array;优先级:1

温迪今天刷题了吗 389

leetcode - 832.Flipping an Image -Java

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].

少年时。 158

【leetcode】Leetcode:832. Flipping an Image

问题描述: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 ...

Felaim的博客 1190

Leetcode——832. Flipping an Image

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...

伍六七的博客 477
上一篇: LeetCode算法题-Positions of Large Groups(Java实现)
下一篇: LeetCode算法题-Rectangle Overlap(Java实现)
程序员小川
博客等级 码龄10年 47粉丝 305原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值