1
+ // AC:
2
+ // Runtime: 2056 ms, faster than 50.00% of Java online submissions for Rotating the Box.
3
+ // Memory Usage: 64.7 MB, less than 100.00% of Java online submissions for Rotating the Box.
4
+ // my thought: using string split(obstacle), and combine every part to make stone fall down.
5
+ // complexity: T:O(m * n), S:O(m * n)
6
+ //
7
+ class Solution {
8
+ public char [][] rotateTheBox (char [][] box ) {
9
+ int row = box .length , col = box [0 ].length ;
10
+ char [][] ret = new char [col ][row ];
11
+ List <List <Character >> record = new LinkedList <>();
12
+ for (int i = 0 ; i < row ; i ++) {
13
+ // 逐行下落
14
+ List <Character > temp = new LinkedList <>();
15
+ StringBuilder tempStr = new StringBuilder ();
16
+ for (int j = 0 ; j <= col - 1 ; j ++) {
17
+ tempStr .append (box [i ][j ]);
18
+ }
19
+ tempStr .append ("#" );
20
+ String [] tempArr = tempStr .toString ().split ("\\ *" );
21
+ if (tempArr .length == 0 ) {
22
+ for (int t = 0 ; t < tempStr .length (); t ++) {
23
+ temp .add ('*' );
24
+ }
25
+ } else {
26
+ for (String part : tempArr ) {
27
+ if (part .isEmpty ()) {
28
+ temp .add ('*' );
29
+ continue ;
30
+ }
31
+ int countEmpty = 0 , countStone = 0 ;
32
+ for (char item : part .toCharArray ()) {
33
+ if (item == '#' ) {
34
+ countStone ++;
35
+ }
36
+ if (item == '.' ) {
37
+ countEmpty ++;
38
+ }
39
+ }
40
+ for (int k = 0 ; k < countEmpty ; k ++) {
41
+ temp .add ('.' );
42
+ }
43
+ for (int k = 0 ; k < countStone ; k ++) {
44
+ temp .add ('#' );
45
+ }
46
+
47
+ temp .add ('*' );
48
+ }
49
+ // 去除末尾 *
50
+ if (temp .get (temp .size () - 1 ) == '*' ) {
51
+ temp .remove (temp .size () - 1 );
52
+ }
53
+ }
54
+ temp .remove (temp .size () - 1 );
55
+ record .add (temp );
56
+ }
57
+ // rotate
58
+ for (int i = 0 ; i < col ; i ++) {
59
+ for (int j = 0 ; j < row ; j ++) {
60
+ ret [i ][j ] = record .get (row - 1 - j ).get (i );
61
+ }
62
+ }
63
+
64
+ return ret ;
65
+ }
66
+ }
0 commit comments