回答編集履歴

6

コード修正

2019/12/24 05:13

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -1,10 +1,12 @@
1
- 要素数を一つ減らして処理。
1
+ コード修正版
2
2
 
3
3
 
4
4
 
5
5
  ```Java
6
6
 
7
7
  import java.util.ArrayList;
8
+
9
+ import java.util.Arrays;
8
10
 
9
11
  import java.util.List;
10
12
 
@@ -18,9 +20,15 @@
18
20
 
19
21
  public static void main(String[] args) {
20
22
 
21
- List<Integer> num = Arrays.asList(2, 1, 4);
23
+ List<Integer> num = Arrays.asList(2,3,5,1,15,30,60);
22
24
 
23
- List<String> fizz = Arrays.asList("ab", "cd");
25
+ List<String> fizz = Arrays.asList("bon","kabe","don","ban","don","gon");
26
+
27
+
28
+
29
+ System.out.println("num: " + num);
30
+
31
+ System.out.println("fizz: " + fizz);
24
32
 
25
33
 
26
34
 
@@ -40,7 +48,7 @@
40
48
 
41
49
  for (var i = 0; i < index.size(); i++) {
42
50
 
43
- num.set(index.get(i), oldNum.get(i));
51
+ num.set(i, oldNum.get(index.get(i)));
44
52
 
45
53
  }
46
54
 
@@ -48,15 +56,15 @@
48
56
 
49
57
  for (var i = 0; i < index.size(); i++) {
50
58
 
51
- fizz.set(index.get(i), oldFizz.get(i));
59
+ fizz.set(i, oldFizz.get(index.get(i)));
52
60
 
53
61
  }
54
62
 
55
63
 
56
64
 
65
+ System.out.println("index: " + index);
66
+
57
67
  System.out.println("num: " + num);
58
-
59
- System.out.println("index: " + index);
60
68
 
61
69
  System.out.println("fizz: " + fizz);
62
70
 

5

修正漏れ対応

2019/12/24 05:13

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
  List<Integer> index = new ArrayList(num.size() - 1);
28
28
 
29
- for (var i = 0; i < fizz.size(); i++) {
29
+ for (var i = 0; i < num.size() - 1; i++) {
30
30
 
31
31
  index.add(i);
32
32
 

4

要素数をひとつ減らした処理コードだけ掲載

2019/12/24 04:45

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -1,64 +1,4 @@
1
- ```java
2
-
3
- import java.util.Arrays;
4
-
5
- import java.util.ArrayList;
6
-
7
- import java.util.List;
8
-
9
- import java.util.Collections;
10
-
11
- import java.util.Comparator;
12
-
13
-
14
-
15
- public class Main {
16
-
17
- public static void main(String[] args) {
18
-
19
- List<Integer> num = Arrays.asList(2, 1, 4);
20
-
21
- List<String> fizz = Arrays.asList("ab", "cd");
22
-
23
- List<Integer> index = new ArrayList(num.size());
24
-
25
- for (var i = 0; i < num.size(); i++) {
26
-
27
- index.add(i);
28
-
29
- }
30
-
31
-
32
-
33
- Collections.sort(index, (i1, i2) -> num.get(i1) - num.get(i2));
34
-
35
- Collections.sort(num);
36
-
37
- List<String> oldFizz = new ArrayList<>(fizz);
38
-
39
- for (var i = 0; i < fizz.size(); i++) {
40
-
41
- fizz.set(index.get(i), oldFizz.get(i));
42
-
43
- }
44
-
45
-
46
-
47
- System.out.println("num: " + num);
48
-
49
- System.out.println("index: " + index);
50
-
51
- System.out.println("fizz: " + fizz);
52
-
53
- }
54
-
55
- }
56
-
57
- ```
58
-
59
-
60
-
61
- fizzの個だけ処理。
1
+ 要素を一つ減らして処理。
62
2
 
63
3
 
64
4
 
@@ -84,7 +24,7 @@
84
24
 
85
25
 
86
26
 
87
- List<Integer> index = new ArrayList(fizz.size());
27
+ List<Integer> index = new ArrayList(num.size() - 1);
88
28
 
89
29
  for (var i = 0; i < fizz.size(); i++) {
90
30
 

3

fizzのサイズ分だけ処理するコードを追加

2019/12/24 04:44

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -58,13 +58,11 @@
58
58
 
59
59
 
60
60
 
61
- fizzに同じ要素がないという前提なら、こんな手も・・・
61
+ fizzの個数だけ処理。
62
62
 
63
63
 
64
64
 
65
65
  ```Java
66
-
67
- import java.util.Arrays;
68
66
 
69
67
  import java.util.ArrayList;
70
68
 
@@ -84,21 +82,35 @@
84
82
 
85
83
  List<String> fizz = Arrays.asList("ab", "cd");
86
84
 
87
- List<Integer> index = new ArrayList(num.size());
88
85
 
86
+
87
+ List<Integer> index = new ArrayList(fizz.size());
88
+
89
- for (int i = 0; i < num.size(); i++) {
89
+ for (var i = 0; i < fizz.size(); i++) {
90
90
 
91
91
  index.add(i);
92
92
 
93
93
  }
94
94
 
95
+ Collections.sort(index, (i1, i2) -> num.get(i1) - num.get(i2));
95
96
 
96
97
 
97
- Collections.sort(index, (i1, i2) -> num.get(i1) - num.get(i2));
98
98
 
99
- Collections.sort(fizz, (s1, s2) -> num.get(fizz.indexOf(s1)) - num.get(fizz.indexOf(s2)));
99
+ List<Integer> oldNum = new ArrayList<>(num);
100
100
 
101
+ for (var i = 0; i < index.size(); i++) {
102
+
101
- Collections.sort(num);
103
+ num.set(index.get(i), oldNum.get(i));
104
+
105
+ }
106
+
107
+ List<String> oldFizz = new ArrayList<>(fizz);
108
+
109
+ for (var i = 0; i < index.size(); i++) {
110
+
111
+ fizz.set(index.get(i), oldFizz.get(i));
112
+
113
+ }
102
114
 
103
115
 
104
116
 
@@ -113,19 +125,3 @@
113
125
  }
114
126
 
115
127
  ```
116
-
117
-
118
-
119
- 実行結果
120
-
121
-
122
-
123
- ```
124
-
125
- num: [1, 2, 4]
126
-
127
- index: [1, 0, 2]
128
-
129
- fizz: [cd, ab]
130
-
131
- ```

2

要素が重複してもいいコードを追加

2019/12/24 02:33

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -1,3 +1,63 @@
1
+ ```java
2
+
3
+ import java.util.Arrays;
4
+
5
+ import java.util.ArrayList;
6
+
7
+ import java.util.List;
8
+
9
+ import java.util.Collections;
10
+
11
+ import java.util.Comparator;
12
+
13
+
14
+
15
+ public class Main {
16
+
17
+ public static void main(String[] args) {
18
+
19
+ List<Integer> num = Arrays.asList(2, 1, 4);
20
+
21
+ List<String> fizz = Arrays.asList("ab", "cd");
22
+
23
+ List<Integer> index = new ArrayList(num.size());
24
+
25
+ for (var i = 0; i < num.size(); i++) {
26
+
27
+ index.add(i);
28
+
29
+ }
30
+
31
+
32
+
33
+ Collections.sort(index, (i1, i2) -> num.get(i1) - num.get(i2));
34
+
35
+ Collections.sort(num);
36
+
37
+ List<String> oldFizz = new ArrayList<>(fizz);
38
+
39
+ for (var i = 0; i < fizz.size(); i++) {
40
+
41
+ fizz.set(index.get(i), oldFizz.get(i));
42
+
43
+ }
44
+
45
+
46
+
47
+ System.out.println("num: " + num);
48
+
49
+ System.out.println("index: " + index);
50
+
51
+ System.out.println("fizz: " + fizz);
52
+
53
+ }
54
+
55
+ }
56
+
57
+ ```
58
+
59
+
60
+
1
61
  fizzに同じ要素がないという前提なら、こんな手も・・・
2
62
 
3
63
 

1

index追加

2019/12/23 11:23

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -5,6 +5,8 @@
5
5
  ```Java
6
6
 
7
7
  import java.util.Arrays;
8
+
9
+ import java.util.ArrayList;
8
10
 
9
11
  import java.util.List;
10
12
 
@@ -22,13 +24,29 @@
22
24
 
23
25
  List<String> fizz = Arrays.asList("ab", "cd");
24
26
 
27
+ List<Integer> index = new ArrayList(num.size());
28
+
29
+ for (int i = 0; i < num.size(); i++) {
30
+
31
+ index.add(i);
32
+
33
+ }
34
+
35
+
36
+
37
+ Collections.sort(index, (i1, i2) -> num.get(i1) - num.get(i2));
38
+
25
39
  Collections.sort(fizz, (s1, s2) -> num.get(fizz.indexOf(s1)) - num.get(fizz.indexOf(s2)));
26
40
 
27
41
  Collections.sort(num);
28
42
 
29
- System.out.println(num);
30
43
 
44
+
45
+ System.out.println("num: " + num);
46
+
47
+ System.out.println("index: " + index);
48
+
31
- System.out.println(fizz);
49
+ System.out.println("fizz: " + fizz);
32
50
 
33
51
  }
34
52
 
@@ -44,8 +62,10 @@
44
62
 
45
63
  ```
46
64
 
47
- [1, 2, 4]
65
+ num: [1, 2, 4]
48
66
 
67
+ index: [1, 0, 2]
68
+
49
- [cd, ab]
69
+ fizz: [cd, ab]
50
70
 
51
71
  ```