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

Commit48b128a

Browse files
refactor 653
1 parente9a3f0f commit48b128a

File tree

2 files changed

+9
-51
lines changed

2 files changed

+9
-51
lines changed

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

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
packagecom.fishercoder.solutions;
22

33
importcom.fishercoder.common.classes.TreeNode;
4-
importcom.fishercoder.common.utils.Notes;
54

65
importjava.util.ArrayList;
7-
importjava.util.HashMap;
86
importjava.util.List;
9-
importjava.util.Map;
107

118
/**
129
* 653. Two Sum IV - Input is a BST
@@ -39,7 +36,7 @@
3936
*/
4037
publicclass_653 {
4138

42-
publicstaticclassListSolution {
39+
publicstaticclassSolution1 {
4340
publicbooleanfindTarget(TreeNoderoot,intk) {
4441
if (root ==null) {
4542
returnfalse;
@@ -66,36 +63,4 @@ private void dfs(TreeNode root, List<Integer> list) {
6663
}
6764
}
6865
}
69-
70-
@Notes(todo ="This solution fails by _653Test.test6(), need to fix it.")
71-
publicstaticclassMapSolution {
72-
publicbooleanfindTarget(TreeNoderoot,intk) {
73-
if (root ==null) {
74-
returnfalse;
75-
}
76-
Map<Integer,Integer>map =newHashMap();//value is index
77-
intindex =0;
78-
preorder(root,map,index);
79-
for (Map.Entry<Integer,Integer>entry :map.entrySet()) {
80-
if (map.containsKey(k -entry.getKey()) &&map.get(k -entry.getKey()) !=entry.getValue()) {
81-
returntrue;
82-
}
83-
}
84-
returnfalse;
85-
}
86-
87-
privatevoidpreorder(TreeNoderoot,Map<Integer,Integer>map,intindex) {
88-
if (root ==null) {
89-
return;
90-
}
91-
map.put(root.val,index++);
92-
if (root.left !=null) {
93-
preorder(root.left,map, ++index);
94-
}
95-
if (root.right !=null) {
96-
preorder(root.right,map, ++index);
97-
}
98-
}
99-
100-
}
10166
}

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
importstaticjunit.framework.Assert.assertEquals;
1414

1515
publicclass_653Test {
16-
privatestatic_653.ListSolutionlistSolution;
17-
privatestatic_653.MapSolutionmapSolution;
16+
privatestatic_653.Solution1solution1;
1817
privatestaticbooleanexpected;
1918
privatestaticTreeNoderoot;
2019

2120
@BeforeClass
2221
publicstaticvoidsetup() {
23-
listSolution =new_653.ListSolution();
24-
mapSolution =new_653.MapSolution();
22+
solution1 =new_653.Solution1();
2523
}
2624

2725
@Before
@@ -33,24 +31,21 @@ public void setupForEachTest() {
3331
publicvoidtest1() {
3432
root =TreeUtils.constructBinaryTree(newArrayList<>(Arrays.asList(5,3,2,4,6,7)));
3533
expected =true;
36-
assertEquals(expected,listSolution.findTarget(root,9));
37-
assertEquals(expected,mapSolution.findTarget(root,9));
34+
assertEquals(expected,solution1.findTarget(root,9));
3835
}
3936

4037
@Test
4138
publicvoidtest2() {
4239
root =TreeUtils.constructBinaryTree(newArrayList<>(Arrays.asList(2,1,3)));
4340
expected =true;
44-
assertEquals(expected,listSolution.findTarget(root,4));
45-
assertEquals(expected,mapSolution.findTarget(root,4));
41+
assertEquals(expected,solution1.findTarget(root,4));
4642
}
4743

4844
@Test
4945
publicvoidtest3() {
5046
root =newTreeNode(1);
5147
expected =false;
52-
assertEquals(expected,listSolution.findTarget(root,2));
53-
assertEquals(expected,mapSolution.findTarget(root,2));
48+
assertEquals(expected,solution1.findTarget(root,2));
5449
}
5550

5651
@Test
@@ -68,16 +63,14 @@ public void test4() {
6863

6964
root =TreeUtils.constructBinaryTree(newArrayList<>(Arrays.asList(2,0, -4,1,3)));
7065
expected =true;
71-
assertEquals(expected,listSolution.findTarget(root, -1));
72-
assertEquals(expected,mapSolution.findTarget(root, -1));
66+
assertEquals(expected,solution1.findTarget(root, -1));
7367
}
7468

7569
@Test
7670
publicvoidtest5() {
7771
root =TreeUtils.constructBinaryTree(newArrayList<>(Arrays.asList(2,1,3, -4,0)));
7872
expected =true;
79-
assertEquals(expected,listSolution.findTarget(root,2));
80-
assertEquals(expected,mapSolution.findTarget(root,2));
73+
assertEquals(expected,solution1.findTarget(root,2));
8174
}
8275

8376
@Test
@@ -93,7 +86,7 @@ public void test6() {
9386
null,null,170,376,1421,1613,null,null,2534,null,
9487
null,null,96,null,null,null,1303)));
9588
expected =true;
96-
assertEquals(expected,listSolution.findTarget(root,5831));
89+
assertEquals(expected,solution1.findTarget(root,5831));
9790
// TreeUtils.printBinaryTree(root);
9891
// assertEquals(expected, mapSolution.findTarget(root, 5831));
9992
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp