Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5eba67c

Browse files
refactor 631
1 parentd94d9f6 commit5eba67c

File tree

2 files changed

+77
-78
lines changed

2 files changed

+77
-78
lines changed

‎src/main/java/com/fishercoder/solutions/_631.java

Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -80,98 +80,102 @@ This function return the sum result at C(row, column).
8080
*/
8181
publicclass_631 {
8282

83-
/**Credit: https://leetcode.com/articles/design-excel-sum-formula/#approach-1-using-topological-sortaccepted*/
84-
publicstaticclassExcel {
85-
86-
Formula[][]formulas;
83+
publicstaticclassSolution1 {
84+
/**
85+
* Credit: https://leetcode.com/articles/design-excel-sum-formula/#approach-1-using-topological-sortaccepted
86+
*/
87+
publicstaticclassExcel {
88+
89+
Formula[][]formulas;
90+
91+
classFormula {
92+
Formula(HashMap<String,Integer>c,intv) {
93+
val =v;
94+
cells =c;
95+
}
8796

88-
classFormula {
89-
Formula(HashMap<String,Integer>c,intv) {
90-
val =v;
91-
cells =c;
97+
HashMap<String,Integer>cells;
98+
intval;
9299
}
93100

94-
HashMap<String,Integer>cells;
95-
intval;
96-
}
101+
Stack<int[]>stack =newStack<>();
97102

98-
Stack<int[]>stack =newStack<>();
99-
100-
publicExcel(intH,charW) {
101-
formulas =newFormula[H][(W -'A') +1];
102-
}
103+
publicExcel(intH,charW) {
104+
formulas =newFormula[H][(W -'A') +1];
105+
}
103106

104-
publicintget(intr,charc) {
105-
if (formulas[r -1][c -'A'] ==null) {
106-
return0;
107+
publicintget(intr,charc) {
108+
if (formulas[r -1][c -'A'] ==null) {
109+
return0;
110+
}
111+
returnformulas[r -1][c -'A'].val;
107112
}
108-
returnformulas[r -1][c -'A'].val;
109-
}
110113

111-
publicvoidset(intr,charc,intv) {
112-
formulas[r -1][c -'A'] =newFormula(newHashMap<String,Integer>(),v);
113-
topologicalSort(r -1,c -'A');
114-
execute_stack();
115-
}
114+
publicvoidset(intr,charc,intv) {
115+
formulas[r -1][c -'A'] =newFormula(newHashMap<String,Integer>(),v);
116+
topologicalSort(r -1,c -'A');
117+
execute_stack();
118+
}
116119

117-
publicintsum(intr,charc,String[]strs) {
118-
HashMap<String,Integer>cells =convert(strs);
119-
intsumm =calculate_sum(r -1,c -'A',cells);
120-
set(r,c,summ);
121-
formulas[r -1][c -'A'] =newFormula(cells,summ);
122-
returnsumm;
123-
}
120+
publicintsum(intr,charc,String[]strs) {
121+
HashMap<String,Integer>cells =convert(strs);
122+
intsumm =calculate_sum(r -1,c -'A',cells);
123+
set(r,c,summ);
124+
formulas[r -1][c -'A'] =newFormula(cells,summ);
125+
returnsumm;
126+
}
124127

125-
publicvoidtopologicalSort(intr,intc) {
126-
for (inti =0;i <formulas.length;i++) {
127-
for (intj =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+
publicvoidtopologicalSort(intr,intc) {
129+
for (inti =0;i <formulas.length;i++) {
130+
for (intj =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+
}
130134
}
131135
}
136+
stack.push(newint[]{r,c});
132137
}
133-
stack.push(newint[]{r,c});
134-
}
135138

136-
publicvoidexecute_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+
publicvoidexecute_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+
}
141145
}
142146
}
143-
}
144147

145-
publicHashMap<String,Integer>convert(String[]strs) {
146-
HashMap<String,Integer>res =newHashMap<>();
147-
for (Stringst :strs) {
148-
if (st.indexOf(":") <0) {
149-
res.put(st,res.getOrDefault(st,0) +1);
150-
}else {
151-
String[]cells =st.split(":");
152-
intsi =Integer.parseInt(cells[0].substring(1));
153-
intei =Integer.parseInt(cells[1].substring(1));
154-
charsj =cells[0].charAt(0);
155-
charej =cells[1].charAt(0);
156-
for (inti =si;i <=ei;i++) {
157-
for (charj =sj;j <=ej;j++) {
158-
res.put("" +j +i,res.getOrDefault("" +j +i,0) +1);
148+
publicHashMap<String,Integer>convert(String[]strs) {
149+
HashMap<String,Integer>res =newHashMap<>();
150+
for (Stringst :strs) {
151+
if (st.indexOf(":") <0) {
152+
res.put(st,res.getOrDefault(st,0) +1);
153+
}else {
154+
String[]cells =st.split(":");
155+
intsi =Integer.parseInt(cells[0].substring(1));
156+
intei =Integer.parseInt(cells[1].substring(1));
157+
charsj =cells[0].charAt(0);
158+
charej =cells[1].charAt(0);
159+
for (inti =si;i <=ei;i++) {
160+
for (charj =sj;j <=ej;j++) {
161+
res.put("" +j +i,res.getOrDefault("" +j +i,0) +1);
162+
}
159163
}
160164
}
161165
}
166+
returnres;
162167
}
163-
returnres;
164-
}
165168

166-
publicintcalculate_sum(intr,intc,HashMap<String,Integer>cells) {
167-
intsum =0;
168-
for (Strings :cells.keySet()) {
169-
intx =Integer.parseInt(s.substring(1)) -1;
170-
inty =s.charAt(0) -'A';
171-
sum += (formulas[x][y] !=null ?formulas[x][y].val :0) *cells.get(s);
169+
publicintcalculate_sum(intr,intc,HashMap<String,Integer>cells) {
170+
intsum =0;
171+
for (Strings :cells.keySet()) {
172+
intx =Integer.parseInt(s.substring(1)) -1;
173+
inty =s.charAt(0) -'A';
174+
sum += (formulas[x][y] !=null ?formulas[x][y].val :0) *cells.get(s);
175+
}
176+
formulas[r][c] =newFormula(cells,sum);
177+
returnsum;
172178
}
173-
formulas[r][c] =newFormula(cells,sum);
174-
returnsum;
175179
}
176180
}
177181

@@ -183,9 +187,4 @@ public int calculate_sum(int r, int c, HashMap<String, Integer> cells) {
183187
* int param_3 = obj.sum(r,c,strs);
184188
*/
185189

186-
publicstaticvoidmain(String...args) {
187-
System.out.println('A' -64);
188-
System.out.println((int)'C');
189-
System.out.println('Z' -64);
190-
}
191190
}

‎src/test/java/com/fishercoder/_631Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
importstaticjunit.framework.TestCase.assertEquals;
77

88
publicclass_631Test {
9-
privatestatic_631.Excelexcel;
9+
privatestatic_631.Solution1.Excelexcel;
1010

1111
@Test
1212
publicvoidtest1() {
13-
excel =new_631.Excel(3,'C');
13+
excel =new_631.Solution1.Excel(3,'C');
1414
assertEquals(0,excel.get(1,'A'));
1515
excel.set(1,'A',1);
1616
assertEquals(1,excel.get(1,'A'));
1717
}
1818

1919
@Test
2020
publicvoidtest2() {
21-
excel =new_631.Excel(3,'C');
21+
excel =new_631.Solution1.Excel(3,'C');
2222
assertEquals(0,excel.sum(1,'A',newString[]{"A2"}));
2323
excel.set(2,'A',1);
2424
assertEquals(1,excel.get(1,'A'));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp