Skip to content

Commit 36079e6

Browse files
committed
Add solution and test-cases for problem 2639
1 parent f03226b commit 36079e6

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [2639.Find the Width of Columns of a Grid][title]
2+
3+
## Description
4+
You are given a **0-indexed** `m x n` integer matrix `grid`. The width of a column is the maximum **length** of its integers.
5+
6+
- For example, if `grid = [[-10], [3], [12]]`, the width of the only column is `3` since `-10` is of length `3`.
7+
8+
Return an integer array `ans` of size n where `ans[i]` is the width of the `ith` column.
9+
10+
The **length** of an integer `x` with `len` digits is equal to `len` if `x` is non-negative, and `len + 1` otherwise.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: grid = [[1],[22],[333]]
16+
Output: [3]
17+
Explanation: In the 0th column, 333 is of length 3.
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: grid = [[-15,1,3],[15,7,12],[5,6,-2]]
24+
Output: [3,1,2]
25+
Explanation:
26+
In the 0th column, only -15 is of length 3.
27+
In the 1st column, all integers are of length 1.
28+
In the 2nd column, both 12 and -2 are of length 2.
29+
```
30+
31+
## 结语
32+
33+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
34+
35+
[title]: https://leetcode.com/problems/find-the-width-of-columns-of-a-grid
36+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func nBits(n int) int {
4+
if n == 0 {
5+
return 1
6+
}
7+
bits := 0
8+
if n < 0 {
9+
bits++
10+
n = -n
11+
}
12+
for n > 0 {
13+
n /= 10
14+
bits++
15+
}
16+
return bits
17+
}
18+
19+
func Solution(grid [][]int) []int {
20+
rows, cols := len(grid), len(grid[0])
21+
ret := make([]int, cols)
22+
for r := 0; r < rows; r++ {
23+
for c := 0; c < cols; c++ {
24+
ret[c] = max(ret[c], nBits(grid[r][c]))
25+
}
26+
}
27+
return ret
528
}

leetcode/2601-2700/2639.Find-the-Width-of-Columns-of-a-Grid/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1}, {22}, {333}}, []int{3}},
17+
{"TestCase2", [][]int{{-15, 1, 3}, {15, 7, 12}, {5, 6, -2}}, []int{3, 1, 2}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)