@@ -80,98 +80,102 @@ This function return the sum result at C(row, column).
80
80
*/
81
81
public class _631 {
82
82
83
- /**Credit: https://leetcode.com/articles/design-excel-sum-formula/#approach-1-using-topological-sortaccepted*/
84
- public static class Excel {
85
-
86
- Formula [][] formulas ;
83
+ public static class Solution1 {
84
+ /**
85
+ * Credit: https://leetcode.com/articles/design-excel-sum-formula/#approach-1-using-topological-sortaccepted
86
+ */
87
+ public static class Excel {
88
+
89
+ Formula [][] formulas ;
90
+
91
+ class Formula {
92
+ Formula (HashMap <String , Integer > c , int v ) {
93
+ val = v ;
94
+ cells = c ;
95
+ }
87
96
88
- class Formula {
89
- Formula (HashMap <String , Integer > c , int v ) {
90
- val = v ;
91
- cells = c ;
97
+ HashMap <String , Integer > cells ;
98
+ int val ;
92
99
}
93
100
94
- HashMap <String , Integer > cells ;
95
- int val ;
96
- }
101
+ Stack <int []> stack = new Stack <>();
97
102
98
- Stack <int []> stack = new Stack <>();
99
-
100
- public Excel (int H , char W ) {
101
- formulas = new Formula [H ][(W - 'A' ) + 1 ];
102
- }
103
+ public Excel (int H , char W ) {
104
+ formulas = new Formula [H ][(W - 'A' ) + 1 ];
105
+ }
103
106
104
- public int get (int r , char c ) {
105
- if (formulas [r - 1 ][c - 'A' ] == null ) {
106
- return 0 ;
107
+ public int get (int r , char c ) {
108
+ if (formulas [r - 1 ][c - 'A' ] == null ) {
109
+ return 0 ;
110
+ }
111
+ return formulas [r - 1 ][c - 'A' ].val ;
107
112
}
108
- return formulas [r - 1 ][c - 'A' ].val ;
109
- }
110
113
111
- public void set (int r , char c , int v ) {
112
- formulas [r - 1 ][c - 'A' ] = new Formula (new HashMap <String , Integer >(), v );
113
- topologicalSort (r - 1 , c - 'A' );
114
- execute_stack ();
115
- }
114
+ public void set (int r , char c , int v ) {
115
+ formulas [r - 1 ][c - 'A' ] = new Formula (new HashMap <String , Integer >(), v );
116
+ topologicalSort (r - 1 , c - 'A' );
117
+ execute_stack ();
118
+ }
116
119
117
- public int sum (int r , char c , String [] strs ) {
118
- HashMap <String , Integer > cells = convert (strs );
119
- int summ = calculate_sum (r - 1 , c - 'A' , cells );
120
- set (r , c , summ );
121
- formulas [r - 1 ][c - 'A' ] = new Formula (cells , summ );
122
- return summ ;
123
- }
120
+ public int sum (int r , char c , String [] strs ) {
121
+ HashMap <String , Integer > cells = convert (strs );
122
+ int summ = calculate_sum (r - 1 , c - 'A' , cells );
123
+ set (r , c , summ );
124
+ formulas [r - 1 ][c - 'A' ] = new Formula (cells , summ );
125
+ return summ ;
126
+ }
124
127
125
- public void topologicalSort (int r , int c ) {
126
- for (int i = 0 ; i < formulas .length ; i ++) {
127
- for (int j = 0 ; j < formulas [0 ].length ; j ++) {
128
- if (formulas [i ][j ] != null && formulas [i ][j ].cells .containsKey ("" + (char ) ('A' + c ) + (r + 1 ))) {
129
- topologicalSort (i , j );
128
+ public void topologicalSort (int r , int c ) {
129
+ for (int i = 0 ; i < formulas .length ; i ++) {
130
+ for (int j = 0 ; j < formulas [0 ].length ; j ++) {
131
+ if (formulas [i ][j ] != null && formulas [i ][j ].cells .containsKey ("" + (char ) ('A' + c ) + (r + 1 ))) {
132
+ topologicalSort (i , j );
133
+ }
130
134
}
131
135
}
136
+ stack .push (new int []{r , c });
132
137
}
133
- stack .push (new int []{r , c });
134
- }
135
138
136
- public void execute_stack () {
137
- while (!stack .isEmpty ()) {
138
- int [] top = stack .pop ();
139
- if (formulas [top [0 ]][top [1 ]].cells .size () > 0 ) {
140
- calculate_sum (top [0 ], top [1 ], formulas [top [0 ]][top [1 ]].cells );
139
+ public void execute_stack () {
140
+ while (!stack .isEmpty ()) {
141
+ int [] top = stack .pop ();
142
+ if (formulas [top [0 ]][top [1 ]].cells .size () > 0 ) {
143
+ calculate_sum (top [0 ], top [1 ], formulas [top [0 ]][top [1 ]].cells );
144
+ }
141
145
}
142
146
}
143
- }
144
147
145
- public HashMap <String , Integer > convert (String [] strs ) {
146
- HashMap <String , Integer > res = new HashMap <>();
147
- for (String st : strs ) {
148
- if (st .indexOf (":" ) < 0 ) {
149
- res .put (st , res .getOrDefault (st , 0 ) + 1 );
150
- } else {
151
- String [] cells = st .split (":" );
152
- int si = Integer .parseInt (cells [0 ].substring (1 ));
153
- int ei = Integer .parseInt (cells [1 ].substring (1 ));
154
- char sj = cells [0 ].charAt (0 );
155
- char ej = cells [1 ].charAt (0 );
156
- for (int i = si ; i <= ei ; i ++) {
157
- for (char j = sj ; j <= ej ; j ++) {
158
- res .put ("" + j + i , res .getOrDefault ("" + j + i , 0 ) + 1 );
148
+ public HashMap <String , Integer > convert (String [] strs ) {
149
+ HashMap <String , Integer > res = new HashMap <>();
150
+ for (String st : strs ) {
151
+ if (st .indexOf (":" ) < 0 ) {
152
+ res .put (st , res .getOrDefault (st , 0 ) + 1 );
153
+ } else {
154
+ String [] cells = st .split (":" );
155
+ int si = Integer .parseInt (cells [0 ].substring (1 ));
156
+ int ei = Integer .parseInt (cells [1 ].substring (1 ));
157
+ char sj = cells [0 ].charAt (0 );
158
+ char ej = cells [1 ].charAt (0 );
159
+ for (int i = si ; i <= ei ; i ++) {
160
+ for (char j = sj ; j <= ej ; j ++) {
161
+ res .put ("" + j + i , res .getOrDefault ("" + j + i , 0 ) + 1 );
162
+ }
159
163
}
160
164
}
161
165
}
166
+ return res ;
162
167
}
163
- return res ;
164
- }
165
168
166
- public int calculate_sum (int r , int c , HashMap <String , Integer > cells ) {
167
- int sum = 0 ;
168
- for (String s : cells .keySet ()) {
169
- int x = Integer .parseInt (s .substring (1 )) - 1 ;
170
- int y = s .charAt (0 ) - 'A' ;
171
- sum += (formulas [x ][y ] != null ? formulas [x ][y ].val : 0 ) * cells .get (s );
169
+ public int calculate_sum (int r , int c , HashMap <String , Integer > cells ) {
170
+ int sum = 0 ;
171
+ for (String s : cells .keySet ()) {
172
+ int x = Integer .parseInt (s .substring (1 )) - 1 ;
173
+ int y = s .charAt (0 ) - 'A' ;
174
+ sum += (formulas [x ][y ] != null ? formulas [x ][y ].val : 0 ) * cells .get (s );
175
+ }
176
+ formulas [r ][c ] = new Formula (cells , sum );
177
+ return sum ;
172
178
}
173
- formulas [r ][c ] = new Formula (cells , sum );
174
- return sum ;
175
179
}
176
180
}
177
181
@@ -183,9 +187,4 @@ public int calculate_sum(int r, int c, HashMap<String, Integer> cells) {
183
187
* int param_3 = obj.sum(r,c,strs);
184
188
*/
185
189
186
- public static void main (String ... args ) {
187
- System .out .println ('A' - 64 );
188
- System .out .println ((int ) 'C' );
189
- System .out .println ('Z' - 64 );
190
- }
191
190
}
0 commit comments