Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# [2639.Find the Width of Columns of a Grid][title]

## 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.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/find-the-width-of-columns-of-a-grid
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
package Solution

func Solution(x bool) bool {
return x
func nBits(n int) int {
if n == 0 {
return 1
}
bits := 0
if n < 0 {
bits++
n = -n
}
for n > 0 {
n /= 10
bits++
}
return bits
}

func Solution(grid [][]int) []int {
rows, cols := len(grid), len(grid[0])
ret := make([]int, cols)
for r := 0; r < rows; r++ {
for c := 0; c < cols; c++ {
ret[c] = max(ret[c], nBits(grid[r][c]))
}
}
return ret
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{1}, {22}, {333}}, []int{3}},
{"TestCase2", [][]int{{-15, 1, 3}, {15, 7, 12}, {5, 6, -2}}, []int{3, 1, 2}},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

//压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

//使用案列
// 使用案列
func ExampleSolution() {
}
Loading