我正在做一些面试准备,并正在研究我遇到的这个 leetcode 问题。问题说明Given two integers m and n, loop repeatedly through an array of m and remove each nth element. Return the last element left. (If m = 7 and n = 4, then begin with the array 1 2 3 4 5 6 7 and remove, in order, 4 1 6 5 2 7 and return 3.)。从我的工作来看,7 将是最后一个数字,而不是 3。
我的思考过程是将所有元素添加到一个ArrayListor 中LinkedList,然后使用该remove()函数摆脱传递的位置。我想知道的是如何从我删除的元素开始,然后添加那么多索引并删除下一个数字?我的代码在下面。
static int arraryReturn (int [] a, int b, int c) {
LinkedList<Integer> myList = new LinkedList<>();
for(int i =0; i< a.length;i++) {
myList.add(i);
}
while(myList.size() > 0) {
myList.remove(c);
}
return -1;
}
慕雪6442864
相关分类