diff --git a/leetcode/1801-1900/1861.Rotating-the-Box/1.png b/leetcode/1801-1900/1861.Rotating-the-Box/1.png new file mode 100644 index 000000000..aa477bd79 Binary files /dev/null and b/leetcode/1801-1900/1861.Rotating-the-Box/1.png differ diff --git a/leetcode/1801-1900/1861.Rotating-the-Box/2.png b/leetcode/1801-1900/1861.Rotating-the-Box/2.png new file mode 100644 index 000000000..590f92cc1 Binary files /dev/null and b/leetcode/1801-1900/1861.Rotating-the-Box/2.png differ diff --git a/leetcode/1801-1900/1861.Rotating-the-Box/3.png b/leetcode/1801-1900/1861.Rotating-the-Box/3.png new file mode 100644 index 000000000..35f3faa6a Binary files /dev/null and b/leetcode/1801-1900/1861.Rotating-the-Box/3.png differ diff --git a/leetcode/1801-1900/1861.Rotating-the-Box/README.md b/leetcode/1801-1900/1861.Rotating-the-Box/README.md index ea7cee165..dffe3590b 100755 --- a/leetcode/1801-1900/1861.Rotating-the-Box/README.md +++ b/leetcode/1801-1900/1861.Rotating-the-Box/README.md @@ -1,28 +1,57 @@ # [1861.Rotating the Box][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given an `m x n` matrix of characters `box` representing a side-view of a box. Each cell of the box is one of the following: + +- A stone `'#'` +- A stationary obstacle `'*'` +- Empty `'.'` + +The box is rotated **90 degrees clockwise**, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity **does not** affect the obstacles' positions, and the inertia from the box's rotation **does not** affect the stones' horizontal positions. + +It is **guaranteed** that each stone in `box` rests on an obstacle, another stone, or the bottom of the box. + +Return an `n x m` matrix representing the box after the rotation described above. + +**Example 1:** -**Example 1:** +![1](./1.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: box = [["#",".","#"]] +Output: [["."], + ["#"], + ["#"]] ``` -## 题意 -> ... +**Example 2:** -## 题解 +![2](./2.png) -### 思路1 -> ... -Rotating the Box -```go ``` +Input: box = [["#",".","*","."], + ["#","#","*","."]] +Output: [["#","."], + ["#","#"], + ["*","*"], + [".","."]] +``` + +**Example 3:** + +![3](./3.png) +``` +Input: box = [["#","#","*",".","*","."], + ["#","#","#","*",".","."], + ["#","#","#",".","#","."]] +Output: [[".","#","#"], + [".","#","#"], + ["#","#","*"], + ["#","*","."], + ["#",".","*"], + ["#",".","."]] +``` ## 结语 diff --git a/leetcode/1801-1900/1861.Rotating-the-Box/Solution.go b/leetcode/1801-1900/1861.Rotating-the-Box/Solution.go index d115ccf5e..6241d4fc6 100644 --- a/leetcode/1801-1900/1861.Rotating-the-Box/Solution.go +++ b/leetcode/1801-1900/1861.Rotating-the-Box/Solution.go @@ -1,5 +1,29 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(box [][]byte) [][]byte { + rows, cols := len(box), len(box[0]) + for i := 0; i < rows; i++ { + for j := cols - 2; j >= 0; j-- { + if box[i][j] == '.' || box[i][j] == '*' { + continue + } + next := j + 1 + for ; next < cols; next++ { + if box[i][next] == '#' || box[i][next] == '*' { + break + } + } + box[i][j] = '.' + box[i][next-1] = '#' + } + } + res := make([][]byte, cols) + for i := range cols { + res[i] = make([]byte, rows) + for j := 0; j < rows; j++ { + res[i][j] = box[rows-1-j][i] + } + } + + return res } diff --git a/leetcode/1801-1900/1861.Rotating-the-Box/Solution_test.go b/leetcode/1801-1900/1861.Rotating-the-Box/Solution_test.go index 14ff50eb4..7fc36b8b9 100644 --- a/leetcode/1801-1900/1861.Rotating-the-Box/Solution_test.go +++ b/leetcode/1801-1900/1861.Rotating-the-Box/Solution_test.go @@ -10,12 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs [][]byte + expect [][]byte }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]byte{{'#', '.', '#'}}, [][]byte{{'.'}, {'#'}, {'#'}}}, + {"TestCase2", [][]byte{ + {'#', '.', '*', '.'}, + {'#', '#', '*', '.'}, + }, [][]byte{ + {'#', '.'}, + {'#', '#'}, + {'*', '*'}, + {'.', '.'}, + }}, + {"TestCase3", [][]byte{ + {'#', '#', '*', '.', '*', '.'}, + {'#', '#', '#', '*', '.', '.'}, + {'#', '#', '#', '.', '#', '.'}, + }, [][]byte{ + {'.', '#', '#'}, + {'.', '#', '#'}, + {'#', '#', '*'}, + {'#', '*', '.'}, + {'#', '.', '*'}, + {'#', '.', '.'}, + }}, } // 开始测试 @@ -30,10 +49,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }