832. Flipping an Image - LeetCode
Question
Solution
题目大意:将1列与最后n列对换,2列与n-1列对换…然后再将每个元素取反
思路:遍历二维数组的左半边,对每个元素先做对换再取反
Java实现:
public int[][] flipAndInvertImage(int[][] A) {
// flip and reverse
for (int row=0; row<A.length; row++) {
for (int col=0; col<=A[row].length/2; col++) {
if (col == A[row].length/2 && A[row].length%2 == 0) break;
int end = A[row].length - 1 - col;
int tmp = A[row][col];
A[row][col] = invert(A[row][end]);
A[row][end] = invert(tmp);
System.out.print(A[row][col] + ", ");
}
System.out.println();
}
return A;
}
int invert(int x) {
return x == 1 ? 0 : 1;
}
832. Flipping an Image - LeetCode的更多相关文章
- Leetcode#832. Flipping an Image(翻转图像)
题目描述 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. ...
- 【Leetcode_easy】832. Flipping an Image
problem 832. Flipping an Image solution1: class Solution { public: vector<vector<int>> f ...
- 【LeetCode】832. Flipping an Image 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 翻转 + 异或 直接计算 日期 题目地址:https ...
- LeetCode 832. Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- LeetCode 832 Flipping an Image 解题报告
题目要求 Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the ...
- [LeetCode] 832. Flipping an Image_Easy
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- [LeetCode&Python] Problem 832. Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- 832. Flipping an Image —— weekly contest 84
Flipping an Image Given a binary matrix A, we want to flip the image horizontally, then invert it, a ...
- 832. Flipping an Image
class Solution { public: vector<vector<int>> flipAndInvertImage(vector<vector<int& ...
随机推荐
- cpu内部组成
计算机系统的硬件结构主要由四部分组成:控制器.运算器.内存和输入输出设备 其中,控制器和运算器统称为中央处理器.简称CPU.它是计算机硬件系统的指挥中心. 它包括控制器.运算器.寄存器三个部分,其中, ...
- (stm32f103学习总结)—RTC独立定时器—实时时钟实验
一.STM32F1 RTC介绍 1.1 RTC简介 STM32 的实时时钟( RTC)是一个独立的定时器. STM32 的 RTC 模 块拥有一组连续计数的计数器,在相应软件配置下,可提供时钟日历的 ...
- python去除txt文件空白行
代码: def delblankline(infile, outfile): infopen = open(infile, 'r', encoding="utf-8") outfo ...
- mapreduce统计单词
源代码: WordCountMapper.java: package cn.idcast.mapreduce; import org.apache.hadoop.io.LongWritable; im ...
- 【每日日报】第三十二天---DataOutputStream写文件
1 今天继续看书 DataOutputStream写文件 1 package File; 2 import java.io.IOException; 3 import java.io.FileOutp ...
- 用 JS(JavaScript )实现多选、全选、反选
JS小例题 学习内容: 需求 总结: 学习内容: 需求 用 JavaScript 实现全选.反选.多选 实现代码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- java基础-字符流
字符流 * 字符流是可以直接读写字符的IO流 * 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写出. FileReader * FileRea ...
- oracle查询出现科学计数法问题
- 小程序滚动事件之头部渐隐渐现demo
效果图: ==> 代码: //test1.wxml <view class='header' style="opacity:{{opacityStyle}}" hid ...
- vue获取验证码倒计时
<template> <div> <el-button :disabled="disabled" @click="sendcode" ...