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, flipping [1, 1, 0] horizontally 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].

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,1],[1,0,1],[0,0,0]].
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]

Example 2:

Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]

Notes:

  • 1 <= A.length = A[0].length <= 20
  • 0 <= A[i][j] <= 1
 1 class Solution {
2 public:
3 vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
4 vector<vector<int>> res;
5 for(int i = 0; i < A.size(); i++){
6 vector<int> t; //暂存行
7 for(int j = A[0].size() - 1; j>=0 ; j--){
8
9 if(A[i][j]){ //取反
10 t.push_back(0);
11 }else{
12 t.push_back(1);
13 }
14
15 }
16 res.push_back(t);
17 }
18 return res;
19 }
20 };

最新文章

  1. PHP处理海量样本相似度聚类算法
  2. loadrunner关联——对服务器返回的数据选择性提交
  3. 一般处理程序中使用session
  4. 【C#进阶系列】15 枚举类型和位标志
  5. python-数据类型补充及文件处理操作
  6. 【BZOJ 2733】【HNOI 2012】永无乡 Splay启发式合并
  7. How To Tune or Test PLSQL Code Performance in Oracle D2k Forms
  8. hdfs[命令] fsck
  9. Apple移动设备处理器指令集 armv6、armv7、armv7s及arm64-b
  10. 【Lucene3.6.2入门系列】第14节_SolrJ操作索引和搜索文档以及整合中文分词
  11. Robots协议
  12. 2、jQuery的Ajax简单示例
  13. Java实训课
  14. redis复制
  15. python--私有属性--私有方法
  16. requests库入门04-http基本认证
  17. ArcGIS案例学习笔记2_1
  18. DFT
  19. APP三种开发模式
  20. R中的一些基础1106

热门文章

  1. VUE第一个项目怎么读懂
  2. 038 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 05 案例演示switch结构-星期的表示案例以及总结
  3. centos配置WordPress(Apache+mysql+php)
  4. OpenCV计算机视觉学习(2)——图像算术运算 &amp; 掩膜mask操作(数值计算,图像融合,边界填充)
  5. MATLAB中exist函数的用法
  6. PJzhang:鸟哥的linux私房菜-shell脚本-上
  7. 2020年在项目中使用MVVM正确姿势,你用对了吗?
  8. 写给前端同学的C++入门教程(一):概述和环境搭建
  9. Kibana详细入门教程
  10. centos8安装java jdk 13