avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

2639. Query the width of each column in the grid diagram

  2024-01-01        
字数统计: 205字   |   阅读时长: 1min

topic:

2639. Query the width of each column in the grid diagram.md

Thought:

The first thing I think is to usemapTo solve,[list(map(lambda x: len(str(x)), row)) for row in grid] Build such an expression,但是topic要求找到列的最大值,If it is done by yourself so manually, you need itO(n^2)Time complexity,SonumpyYou can easily find the iterative value of each column。

Code:

1
2
3
4
5
6
7
8
9
10
11
import numpy as np

class Solution:
def findColumnWidth(self, grid: List[List[int]]) -> List[int]:
# let's convert the grid to a numpy array
np_grid = np.array(grid, dtype=str)
# calculate the length of each element in the grid
lengths = np.vectorize(len)(np_grid)
# find the maximum length of each column
max_lengths = lengths.max(axis=0)
return max_lengths.tolist()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
impl Solution {
pub fn find_column_width(grid: Vec<Vec<i32>>) -> Vec<i32> {
let col_n = grid[0].len();
let mut ans = vec![0; col_n];

for row in grid.iter() {
for (i, &num) in row.iter().enumerate() {
let length = num.to_string().len() as i32;
if length > ans[i] {
ans[i] = length;
}
}
}
ans
}
}
  • Python
  • answer

扫一扫,分享到微信

微信分享二维码
24.Two or two exchanges linked watches
2679.In the matrix and the harmony
目录
  1. 1. topic:
  2. 2. Thought:
  3. 3. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号