LeetCode高频面试60天打卡日记Day30

Day30(圆圈中最后剩下的数字–ArrayList)

在这里插入图片描述

class Solution {
    
    
    public int lastRemaining(int n, int m) {
    
    
        List<Integer> list = new ArrayList<>();
        for(int i=0;i<n;i++){
    
    
            list.add(i);
        }
        int start = 0;
        while(list.size()>1){
    
    
            int size = list.size();
            int delIndex = (start+m-1)%size;
            list.remove(delIndex);
            start = delIndex;
        }
        return list.get(0);
    }
}

猜你喜欢

转载自blog.csdn.net/YoungNUAA/article/details/105235640