当前位置:网站首页>[Jianzhi offer] 62 The last remaining number in the circle
[Jianzhi offer] 62 The last remaining number in the circle
2022-07-05 16:58:00 【LuZhouShiLi】
The finger of the sword Offer 62. The last number in the circle
subject
0,1,···,n-1 this n Number in a circle , From numbers 0 Start , Delete the... From this circle every time m A digital ( Delete and count from the next number ). Find the last number left in the circle .
for example ,0、1、2、3、4 this 5 Numbers make a circle , From numbers 0 Start deleting the 3 A digital , Before deleting 4 The numbers in turn are 2、0、4、1, So the last remaining number is 3.
Ideas
- Create a list Simulate circular linked list
- Add all elements to list in
- Every time m Step . Remove elements , Pay attention to the residual operation
Code
class Solution {
public int lastRemaining(int n, int m) {
// Create a circular linked list to simulate a circle
ArrayList<Integer> list = new ArrayList<>(n);
for(int i = 0; i < n; i++)
{
list.add(i);
}
int idx = 0;
while(n > 1)
{
idx = (idx + m - 1) % n;// Every time I go M Step Remainder Because it's a ring
list.remove(idx);
n--;
}
return list.get(0);
}
}
边栏推荐
- How can C TCP set heartbeat packets to be elegant?
- 齐宣王典故
- [brush questions] effective Sudoku
- [team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
- 飞桨EasyDL实操范例:工业零件划痕自动识别
- BS-XX-042 基于SSM实现人事管理系统
- 中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户
- sqlserver 做cdc 要对数据库性能有什么要求么
- 【刷题篇】鹅厂文化衫问题
- Do sqlserver have any requirements for database performance when doing CDC
猜你喜欢
【刷题篇】有效的数独
如何安装mysql
深耕5G,芯讯通持续推动5G应用百花齐放
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
Learnopongl notes (I)
Benji Bananas 会员通行证持有人第二季奖励活动更新一览
中间表是如何被消灭的?
调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
Games101 notes (I)
随机推荐
中间表是如何被消灭的?
二叉树相关OJ题
How to install MySQL
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
拷贝方式之DMA
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
Yarn common commands
[61dctf]fm
阈值同态加密在隐私计算中的应用:解读
Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely
PHP 严格模式
How was the middle table destroyed?
Do sqlserver have any requirements for database performance when doing CDC
Sentinel-流量防卫兵
Etcd 构建高可用Etcd集群
2020-2022 two-year anniversary of creation
采用药丸屏的iPhone14或引发中国消费者的热烈抢购
[deep learning] how does deep learning affect operations research?
精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
[深度学习][原创]让yolov6-0.1.0支持yolov5的txt读取数据集模式