当前位置:网站首页>[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);
}
}
边栏推荐
- Games101 notes (III)
- 浏览器渲染原理以及重排与重绘
- [61dctf]fm
- Get ready for the pre-season card game MotoGP ignition champions!
- Etcd 构建高可用Etcd集群
- If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
- Detailed explanation of use scenarios and functions of polar coordinate sector diagram
- 阈值同态加密在隐私计算中的应用:解读
- 帮忙看看是什么问题可以吗?[ERROR] Could not execute SQL stateme
- Jarvis OJ Flag
猜你喜欢

Deep dive kotlin synergy (XXI): flow life cycle function

【 brosser le titre 】 chemise culturelle de l'usine d'oies

【刷题篇】有效的数独

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

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

Learnopongl notes (I)

Domestic API management artifact used by the company

调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%

Global Data Center released DC brain system, enabling intelligent operation and management through science and technology

二叉树相关OJ题
随机推荐
【刷題篇】鹅廠文化衫問題
【机器人坐标系第一讲】
Global Data Center released DC brain system, enabling intelligent operation and management through science and technology
如何安装mysql
What is the difference between EDI license and ICP business license
It is forbidden to copy content JS code on the website page
Domestic API management artifact used by the company
[deep learning] [original] let yolov6-0.1.0 support the txt reading dataset mode of yolov5
DenseNet
Twig数组合并的写法
Jarvis OJ Flag
面对新的挑战,成为更好的自己--进击的技术er
Deep dive kotlin synergy (XXI): flow life cycle function
Etcd 构建高可用Etcd集群
Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
Dare not buy thinking
Jarvis OJ Flag
[es6] add if judgment or ternary operator judgment in the template string
Games101 notes (III)
如何将mysql卸载干净