返回介绍

solution / 0800-0899 / 0832.Flipping an Image / README_EN

发布于 2024-06-17 01:03:34 字数 3999 浏览 0 评论 0 收藏 0

832. Flipping an Image

中文文档

Description

Given an n x n binary matrix image, 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: image = [[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: image = [[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]]

 

Constraints:

  • n == image.length
  • n == image[i].length
  • 1 <= n <= 20
  • images[i][j] is either 0 or 1.

Solutions

Solution 1

class Solution:
  def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
    n = len(image)
    for row in image:
      i, j = 0, n - 1
      while i < j:
        if row[i] == row[j]:
          row[i] ^= 1
          row[j] ^= 1
        i, j = i + 1, j - 1
      if i == j:
        row[i] ^= 1
    return image
class Solution {
  public int[][] flipAndInvertImage(int[][] image) {
    for (var row : image) {
      int i = 0, j = row.length - 1;
      for (; i < j; ++i, --j) {
        if (row[i] == row[j]) {
          row[i] ^= 1;
          row[j] ^= 1;
        }
      }
      if (i == j) {
        row[i] ^= 1;
      }
    }
    return image;
  }
}
class Solution {
public:
  vector<vector<int>> flipAndInvertImage(vector<vector<int>>& image) {
    for (auto& row : image) {
      int i = 0, j = row.size() - 1;
      for (; i < j; ++i, --j) {
        if (row[i] == row[j]) {
          row[i] ^= 1;
          row[j] ^= 1;
        }
      }
      if (i == j) {
        row[i] ^= 1;
      }
    }
    return image;
  }
};
func flipAndInvertImage(image [][]int) [][]int {
  for _, row := range image {
    i, j := 0, len(row)-1
    for ; i < j; i, j = i+1, j-1 {
      if row[i] == row[j] {
        row[i] ^= 1
        row[j] ^= 1
      }
    }
    if i == j {
      row[i] ^= 1
    }
  }
  return image
}
/**
 * @param {number[][]} image
 * @return {number[][]}
 */
var flipAndInvertImage = function (image) {
  for (const row of image) {
    let i = 0;
    let j = row.length - 1;
    for (; i < j; ++i, --j) {
      if (row[i] == row[j]) {
        row[i] ^= 1;
        row[j] ^= 1;
      }
    }
    if (i == j) {
      row[i] ^= 1;
    }
  }
  return image;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文