File tree Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ tags : [算法]
31
31
32
32
* 排除1和2的情况
33
33
34
- 下面先提供两个工具函数
34
+ 下面先提供三个工具函数
35
35
36
36
``` js
37
37
@@ -47,6 +47,18 @@ function print(arg){
47
47
}
48
48
}
49
49
50
+ // 检查是否是正反的走法
51
+ function hasRepeat (src , dist ){
52
+ for (var i = 0 , len = src .length ; i < len ; i++ ){
53
+ if (dist .length == src[i].length ){
54
+ if (dist[0 ] == src[i][1 ]){
55
+ return true ;
56
+ }
57
+ }
58
+ }
59
+ return false ;
60
+ }
61
+
50
62
```
51
63
52
64
下面贴出算法的实现
@@ -85,8 +97,10 @@ function countSteps(n){
85
97
if (i == 0 || i == 1 ){
86
98
linearg .push (calculate (arg .slice (0 ,2 )));
87
99
Array .prototype .push .apply (linearg, arg .slice (2 , arg .length ));
88
- result .push (linearg);
89
- combine (linearg .slice (0 ));
100
+ if (! hasRepeat (result, linearg)){
101
+ result .push (linearg);
102
+ combine (linearg .slice (0 ));
103
+ }
90
104
return ;
91
105
}
92
106
}
You can’t perform that action at this time.
0 commit comments