- public class DeleteEveryTwo {
- public static void main(String[] args) {
- int index = getLastDeletedIndex(8);
- System.out.println("The last index deleted is " + index);
- }
-
-
- public static int getLastDeletedIndex(int len) {
- if (len <= 0) {
- return -1;
- }
-
- int[] arr = new int[len];
- for (int i = 0; i < len; i++) {
- arr[i] = len;
- }
-
- final int DELFLAG = len + 1;
- int currentSize = len;
- final int STEP = 2;
- int count = 0;
- int lastDelIndex = 0;
- int i = 0;
-
- while (currentSize != 0) {
- if (arr[i] != DELFLAG) {
- if (count++ == STEP) {
- arr[i] = DELFLAG;
- lastDelIndex = i;
- currentSize--;
- count = 0;
- System.out.println("Deleted index is " + i % len);
- }
- }
- i = (i + 1) % len;
- }
- return lastDelIndex;
- }
- }
posted on
2016-04-20 22:39
XZhe
阅读(
1366)
评论()
编辑
收藏
举报