返回介绍

solution / 2600-2699 / 2639.Find the Width of Columns of a Grid / README_EN

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

2639. Find the Width of Columns of a Grid

中文文档

Description

You are given a 0-indexed m x n integer matrix grid. The width of a column is the maximum length of its integers.

  • For example, if grid = [[-10], [3], [12]], the width of the only column is 3 since -10 is of length 3.

Return _an integer array_ ans _of size_ n _where_ ans[i] _is the width of the_ ith _column_.

The length of an integer x with len digits is equal to len if x is non-negative, and len + 1 otherwise.

 

Example 1:

Input: grid = [[1],[22],[333]]
Output: [3]
Explanation: In the 0th column, 333 is of length 3.

Example 2:

Input: grid = [[-15,1,3],[15,7,12],[5,6,-2]]
Output: [3,1,2]
Explanation: 
In the 0th column, only -15 is of length 3.
In the 1st column, all integers are of length 1. 
In the 2nd column, both 12 and -2 are of length 2.

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 100
  • -109 <= grid[r][c] <= 109

Solutions

Solution 1

class Solution:
  def findColumnWidth(self, grid: List[List[int]]) -> List[int]:
    ans = [0] * len(grid[0])
    for row in grid:
      for j, x in enumerate(row):
        w = len(str(x))
        ans[j] = max(ans[j], w)
    return ans
class Solution {
  public int[] findColumnWidth(int[][] grid) {
    int n = grid[0].length;
    int[] ans = new int[n];
    for (var row : grid) {
      for (int j = 0; j < n; ++j) {
        int w = String.valueOf(row[j]).length();
        ans[j] = Math.max(ans[j], w);
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> findColumnWidth(vector<vector<int>>& grid) {
    int n = grid[0].size();
    vector<int> ans(n);
    for (auto& row : grid) {
      for (int j = 0; j < n; ++j) {
        int w = to_string(row[j]).size();
        ans[j] = max(ans[j], w);
      }
    }
    return ans;
  }
};
func findColumnWidth(grid [][]int) []int {
  ans := make([]int, len(grid[0]))
  for _, row := range grid {
    for j, x := range row {
      w := len(strconv.Itoa(x))
      ans[j] = max(ans[j], w)
    }
  }
  return ans
}
function findColumnWidth(grid: number[][]): number[] {
  const n = grid[0].length;
  const ans: number[] = new Array(n).fill(0);
  for (const row of grid) {
    for (let j = 0; j < n; ++j) {
      const w: number = String(row[j]).length;
      ans[j] = Math.max(ans[j], w);
    }
  }
  return ans;
}
impl Solution {
  pub fn find_column_width(grid: Vec<Vec<i32>>) -> Vec<i32> {
    let mut ans = vec![0; grid[0].len()];

    for row in grid.iter() {
      for (j, num) in row.iter().enumerate() {
        let width = num.to_string().len() as i32;
        ans[j] = std::cmp::max(ans[j], width);
      }
    }

    ans
  }
}

Solution 2

class Solution:
  def findColumnWidth(self, grid: List[List[int]]) -> List[int]:
    return [max(len(str(x)) for x in col) for col in zip(*grid)]

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

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

发布评论

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