当前位置:网站首页>[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);
}
}
边栏推荐
- 面对新的挑战,成为更好的自己--进击的技术er
- Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)
- Deep learning plus
- Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
- Twig数组合并的写法
- Combined use of vant popup+ other components and pit avoidance Guide
- Binary tree related OJ problems
- It is forbidden to copy content JS code on the website page
- 怎样在电脑上设置路由器的WiFi密码
- 如何安装mysql
猜你喜欢
Etcd 构建高可用Etcd集群
用键盘输入一条命令
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
Summary of methods for finding intersection of ordered linked list sets
Solution of vant tabbar blocking content
Spring Festival Limited "forget trouble in the year of the ox" gift bag waiting for you to pick it up~
How was the middle table destroyed?
BS-XX-042 基于SSM实现人事管理系统
阈值同态加密在隐私计算中的应用:解读
中间表是如何被消灭的?
随机推荐
Copy mode DMA
Global Data Center released DC brain system, enabling intelligent operation and management through science and technology
麻烦问下,DMS中使用Redis语法是以云数据库Redis社区版的命令为参考的嘛
Timestamp strtotime the day before or after the date
Facing new challenges and becoming a better self -- attacking technology er
Do sqlserver have any requirements for database performance when doing CDC
How does win11 change icons for applications? Win11 method of changing icons for applications
Jarvis OJ Webshell分析
Hiengine: comparable to the local cloud native memory database engine
Solution of vant tabbar blocking content
Benji Banas membership pass holders' second quarter reward activities update list
PHP人才招聘系统开发 源代码 招聘网站源码二次开发
How to set the WiFi password of the router on the computer
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
tf. sequence_ Mask function explanation case
阈值同态加密在隐私计算中的应用:解读
Wechat official account web page authorization login is so simple
Summary of methods for finding intersection of ordered linked list sets
有序链表集合求交集 方法 总结
The difference between searching forward index and inverted index