1.题目描述

给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果。

水平翻转图片就是将图片的每一行都进行翻转,即逆序。例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]

反转图片的意思是图片中的 0 全部被 1 替换, 1 全部被 0 替换。例如,反转 [0, 1, 1] 的结果是 [1, 0, 0]

示例 1:

输入: [[,,],[,,],[,,]]
输出: [[,,],[,,],[,,]]
解释: 首先翻转每一行: [[,,],[,,],[,,]];
然后反转图片: [[,,],[,,],[,,]]

示例 2:

输入: [[,,,],[,,,],[,,,],[,,,]]
输出: [[,,,],[,,,],[,,,],[,,,]]
解释: 首先翻转每一行: [[,,,],[,,,],[,,,],[,,,]];
然后反转图片: [[,,,],[,,,],[,,,],[,,,]]

说明:

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

2.我的代码

//卸去对C语言的兼容,加速执行
/*static const auto KSpeedUp = [](){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();*/ class Solution {
public:
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
//翻转每一行,反转每一行
for(auto& a : A){
reverse(a);
overturn(a);
}
return A;
} //反转
vector<int> overturn(vector<int>& nums){
for(auto& c : nums){
c = (c==)? : ;
}
return nums;
} //翻转(逆序)
vector<int> reverse(vector<int>& nums){
int sz = nums.size();
for(int i=; i<=sz/-; ++i){
swap(nums[i],nums[sz-i-]);
}
return nums;
}
};

3.局部优化代码

  • reverse()函数支持对向量的翻转,无需自己造轮子;
  • 1变0,0变1的技巧:c = 1 - c;
class Solution {
public:
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
//翻转每一行,反转每一行
for(auto& a : A){
reverse(a.begin(),a.end());//注意:使用迭代器调用
overturn(a);
}
return A;
} //反转
vector<int> overturn(vector<int>& nums){
for(auto& c : nums){
//c = (c==0)? 1 : 0;
c = -c;
}
return nums;
}
};

4.Leetcode的置顶范例

class Solution {
public:
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
if (A.empty()) return A; for (vector<int> &row : A)
{
reverse(row.begin(), row.end());//逆序每一行
for_each(row.begin(), row.end(), [](int& x){x = ^x; });//反转每一行 for_each遍历+lambda表达式
}
return A;
}
};

参考资料:

1.论C++11 中vector的N种遍历方法 (for_each遍历+Lambda函数)

Leetcode 832.翻转图像的更多相关文章

  1. Java实现 LeetCode 832 翻转图像(位运算)

    832. 翻转图像 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, ...

  2. 力扣(LeetCode)832. 翻转图像

    给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. 反转图片的 ...

  3. 力扣832. 翻转图像-C语言实现-简单题

    题目 传送门 文本 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, ...

  4. Leetcode#832. Flipping an Image(翻转图像)

    题目描述 给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果. 水平翻转图片就是将图片的每一行都进行翻转,即逆序.例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]. ...

  5. [LeetCode] Flipping an Image 翻转图像

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

  6. C#版(击败100.00%的提交) - Leetcode 151. 翻转字符串里的单词 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  7. LeetCode:翻转二叉树【226】

    LeetCode:翻转二叉树[226] 题目描述 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 题目 ...

  8. leetcode-832翻转图像

    翻转图像 思路: 先对图像进行水平翻转,然后反转图片(对每个像素进行异或操作) 代码: class Solution: def flipAndInvertImage(self, A: List[Lis ...

  9. leetcode python翻转字符串里的单词

    # Leetcode 151 翻转字符串里的单词### 题目描述给定一个字符串,逐个翻转字符串中的每个单词. **示例1:** 输入: "the sky is blue" 输出: ...

随机推荐

  1. Python常用模块之Pygame(手册篇:首页)

    Pygame手册官方网址:http://www.pygame.org/docs/ Pygame首页 说明文档: 自述 关于Pygame的基本信息,它是什么,谁参与了以及在哪里找到它. 安装 在几个平台 ...

  2. os模块大全详情

    python常用模块目录 一:os模块分类: python os.walk详解 二:os模块大全表 序号 方法 方法 1 os.access(path, mode) 检验权限模式 2 os.chdir ...

  3. Play on Words(欧拉回路)

    Description Some of the secret doors contain a very interesting word puzzle. The team of archaeologi ...

  4. 软件工程-东北师大站-第五次作业(PSP)

    1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 4.本周PSP饼状图

  5. c# HttpListener拒绝访问

    直接记录解决步骤: 程序代码: HttpListener httpListener = new HttpListener(); httpListener.Prefixes.Add("http ...

  6. HDU 5642 King's Order dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 King's Order  Accepts: 381  Submissions: 1361   ...

  7. ubuntu下配置ss并设置PAC模式

    一.安装ss 1. sudo apt-get update(更新源) 2. sudo apt-get install python-pip(安装pip) 3. sudo pip install sha ...

  8. 【第二周】【作业五】Scrum 每日站会

    1.首先来看一下什么是Scrum: Scrum是一种敏捷软件开发的方法学,用于迭代式增量软件开发过程.Scrum在英语是橄榄球运动中争球的意思. 虽然Scrum是为管理软件开发项目而开发的,它同样可以 ...

  9. Ubuntu 16.04 LTS安装sogou输入法详解

    http://blog.csdn.net/qq_21792169/article/details/53152700 最近开始学习linux 在安装输入法中遇到的一些问题,最终成功安装,也得益于网络上的 ...

  10. c 用指针操作结构体数组

    重点:指针自加,指向下一个结构体数组单元 #include <stdio.h> #include <stdlib.h> #include <string.h> #d ...