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. Testing Harbor REST API on Swagger

    先贴官方地址,我的做法差不多 https://github.com/goharbor/harbor/blob/master/docs/configure_swagger.md 1.下载对应资源 wge ...

  2. Tensorflow - Implement for a Softmax Regression Model on MNIST.

    Coding according to TensorFlow 官方文档中文版 import tensorflow as tf from tensorflow.examples.tutorials.mn ...

  3. mysql把一字段拆分为多行

    sql语句 select a.house_no as '房子',substring_index(substring_index(a.name,',',b.help_topic_id+1),',',-1 ...

  4. python 标准日志模块loging 及日志系统实例

    本文出处:https://www.cnblogs.com/goodhacker/p/3355660.html#undefined python的标准库里的日志系统从Python2.3开始支持.只要im ...

  5. php的大小写敏感问题整理

    php的大小写敏感问题整理 今天在开发php的过程中,因为命名大小写的问题导致代码错误,所以从网上整理了php的大小写敏感的一些资料,需要的朋友可以参考下.   PHP对大小写敏感问题的处理比较乱,写 ...

  6. 第16次Scrum会议(10/28)【欢迎来怼】

    一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华小组照片 二.开会信息 时间:2017/10/28 17:20~17:32,总计12min.地点:东北师范 ...

  7. “Hello World!”团队召开的第六次会议

    团队“Hello World!”团队召开的第六次会议. 博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时间 2017年1 ...

  8. 团队项目成员与题目(本地地铁查询app)

    团队名称:Daydreaming团队成员及其特点:张运涛:能快速与团队成员中的每一位进行合作,能全面考虑遇到的问题,善于总结积累.能较好的理解老师与其他人员的想法要求.刘瑞欣:做事果断,善于领导,有想 ...

  9. windows+ubuntu时间修改问题

    只需要在ubuntu系统数输入一行指令即可 timedatectl set-local-rtc 1 --adjust-system-clock

  10. 第11章 认识和学习bash

    认识bash这个shell 硬件.内核和shell 用户操作计算机流程如下: 用户——>用户界面(shell,KDE,application)——>核心(kernel)——>硬件(h ...