forked from Wang-Jun-Chao/coding-interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test45.java
79 lines (65 loc) · 2.17 KB
/
Test45.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.LinkedList;
import java.util.List;
/**
* Author: 王俊超
* Date: 2015-06-14
* Time: 14:57
* Declaration: All Rights Reserved !!!
*/
public class Test45 {
public static int lastRemaining(int n, int m) {
if (n < 1 || m < 1) {
return -1;
}
List<Integer> list = new LinkedList<>();
for (int i = 0; i < n; i++) {
list.add(i);
}
// 要删除元素的位置
int idx = 0;
// 开始计数的位置
int start = 0;
while (list.size() > 1) {
// 只要移动m-1次就可以移动到下一个要删除的元素上
for (int i = 1; i < m; i++) {
idx = (idx + 1) % list.size(); // 【A】
}
list.remove(idx);
// 确保idx指向每一轮的第一个位置
// 下面的可以不用,【A】已经可以保证其正确性了,可以分析n=6,m=6的第一次删除情况
// if (idx == list.size()) {
// idx = 0;
// }
}
return list.get(0);
}
public static int lastRemaining2(int n, int m) {
if (n < 1 || m < 1) {
return -1;
}
int last = 0;
for (int i = 2; i <=n ; i++) {
last = (last + m)%i;
}
return last;
}
public static void main(String[] args) {
test01();
System.out.println();
test02();
}
private static void test01() {
System.out.println(lastRemaining(5, 3)); // 最后余下3
System.out.println(lastRemaining(5, 2)); // 最后余下2
System.out.println(lastRemaining(6, 7)); // 最后余下4
System.out.println(lastRemaining(6, 6)); // 最后余下3
System.out.println(lastRemaining(0, 0)); // 最后余下-1
}
private static void test02() {
System.out.println(lastRemaining2(5, 3)); // 最后余下3
System.out.println(lastRemaining2(5, 2)); // 最后余下2
System.out.println(lastRemaining2(6, 7)); // 最后余下4
System.out.println(lastRemaining2(6, 6)); // 最后余下3
System.out.println(lastRemaining2(0, 0)); // 最后余下-1
}
}