当前位置:网站首页>[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);
}
}
边栏推荐
- Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)
- 中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户
- WSL2.0安装
- [brush title] goose factory shirt problem
- How can C TCP set heartbeat packets to be elegant?
- Accès aux données - intégration du cadre d'entité
- Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
- Jarvis OJ Flag
- 【剑指 Offer】63. 股票的最大利润
- 数据访问 - EntityFramework集成
猜你喜欢

【机器人坐标系第一讲】

Cs231n notes (bottom) - applicable to 0 Foundation

Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)

SQL injection of cisp-pte (Application of secondary injection)

2020-2022 two-year anniversary of creation

Desci: is decentralized science the new trend of Web3.0?

Data access - entityframework integration

机器学习编译第2讲:张量程序抽象

调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
![[729. My schedule I]](/img/e3/32914227d00cf7595ee850e60f2b72.png)
[729. My schedule I]
随机推荐
How can C TCP set heartbeat packets to be elegant?
2020-2022 two-year anniversary of creation
How to set the WiFi password of the router on the computer
调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
Etcd 构建高可用Etcd集群
[js] skill simplification if empty judgment
[es6] 模板字符串内添加if判断或添加三元运算符判断
PHP talent recruitment system development source code recruitment website source code secondary development
【剑指 Offer】66. 构建乘积数组
飞桨EasyDL实操范例:工业零件划痕自动识别
Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
【剑指 Offer】63. 股票的最大利润
npm安装
[js] 技巧 简化if 判空
Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
WSL2.0安装
Spring Festival Limited "forget trouble in the year of the ox" gift bag waiting for you to pick it up~
二叉树相关OJ题
Raspberry pie 4B installation pytorch1.11
Wechat official account web page authorization login is so simple