当前位置:网站首页>[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);
}
}
边栏推荐
- 【剑指 Offer】66. 构建乘积数组
- Learnopongl notes (II) - Lighting
- Google Earth engine (GEE) -- a brief introduction to kernel kernel functions and gray level co-occurrence matrix
- 什么是ROM
- Get ready for the pre-season card game MotoGP ignition champions!
- tf. sequence_ Mask function explanation case
- [js] 技巧 简化if 判空
- Sentinel-流量防卫兵
- Jarvis OJ shell流量分析
- [team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
猜你喜欢

Jarvis OJ Flag

美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名

清晰还原31年前现场,火山引擎超清修复Beyond经典演唱会

数据访问 - EntityFramework集成

Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely

Summary of methods for finding intersection of ordered linked list sets

How to uninstall MySQL cleanly

Cs231n notes (bottom) - applicable to 0 Foundation

中间表是如何被消灭的?
![[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
随机推荐
Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation
【组队 PK 赛】本周任务已开启 | 答题挑战,夯实商品详情知识
树莓派4b安装Pytorch1.11
【剑指 Offer】66. 构建乘积数组
Games101 notes (I)
【729. 我的日程安排錶 I】
搜索 正排索引 和 倒排索引 区别
What is ROM
How to uninstall MySQL cleanly
Timestamp strtotime the day before or after the date
China Radio and television officially launched 5g services, and China Mobile quickly launched free services to retain users
Yarn common commands
Hiengine: comparable to the local cloud native memory database engine
Learnopongl notes (II) - Lighting
Twig数组合并的写法
数据访问 - EntityFramework集成
How does the outer disk futures platform distinguish formal security?
[echart] resize lodash to realize chart adaptation when window is zoomed
Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022