用数组模拟,边界条件做吐了,还是用库中的集合吧
public int LastRemaining_Solution(int n, int m) { if(n==0||m==0)return -1; int[] arr =new int[n]; int count = n; int cur = 0; while(count!=1){ int i = 0; while(i<m-1){ if(arr[cur%n]==1) cur=(cur+1)%n; else{ cur=(cur+1)%n; i++; } } while(arr[cur]==1){ cur=(cur+1)%n; } arr[cur]=1; count--; } int i; for(i=0;i<n;i++){ if(arr[i]!=1) break; } return i; }