回答編集履歴

2

微修正

2020/04/30 07:03

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -66,13 +66,7 @@
66
66
 
67
67
 
68
68
 
69
- if(!flag) {
70
-
71
- ans = new int[1]; ans[0] = -1;
69
+ return flag ? ans : new int[]{-1};
72
-
73
- }
74
-
75
- return ans;
76
70
 
77
71
  }
78
72
 

1

追記

2020/04/30 07:03

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -11,3 +11,101 @@
11
11
 
12
12
 
13
13
  return ans.toArray()
14
+
15
+
16
+
17
+ [追記] バグみっけ。
18
+
19
+ ```Java
20
+
21
+ import java.util.*;
22
+
23
+
24
+
25
+ public class Trial {
26
+
27
+
28
+
29
+ static int[] absolutePermutation(int n, int k) {
30
+
31
+ Set<Integer> used = new HashSet<Integer>();
32
+
33
+ boolean flag = true;
34
+
35
+ int[] ans = new int[n];
36
+
37
+
38
+
39
+ for(int i=1; i<=n; i++) {
40
+
41
+ if ( i-k >= 1 && !used.contains(i-k) ){
42
+
43
+ used.add(i-k);
44
+
45
+ ans[i-1]= i-k; // ココと
46
+
47
+ continue;
48
+
49
+ }
50
+
51
+ if( i+k <= n && !used.contains(i+k) ){
52
+
53
+ used.add(i+k);
54
+
55
+ ans[i-1]= i+k; // ココ
56
+
57
+ continue;
58
+
59
+ }
60
+
61
+ flag = false;
62
+
63
+ break;
64
+
65
+ }
66
+
67
+
68
+
69
+ if(!flag) {
70
+
71
+ ans = new int[1]; ans[0] = -1;
72
+
73
+ }
74
+
75
+ return ans;
76
+
77
+ }
78
+
79
+
80
+
81
+ public static void main(String[] args) {
82
+
83
+ for ( int item : absolutePermutation(2, 1) ) {
84
+
85
+ System.out.printf("%d ", item);
86
+
87
+ }
88
+
89
+ System.out.println();
90
+
91
+ for ( int item : absolutePermutation(3, 0) ) {
92
+
93
+ System.out.printf("%d ", item);
94
+
95
+ }
96
+
97
+ System.out.println();
98
+
99
+ for ( int item : absolutePermutation(3, 2) ) {
100
+
101
+ System.out.printf("%d ", item);
102
+
103
+ }
104
+
105
+ System.out.println();
106
+
107
+ }
108
+
109
+ }
110
+
111
+ ```