当前位置:网站首页>[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 to uninstall MySQL cleanly
- 时间戳strtotime前一天或后一天的日期
- 【 brosser le titre 】 chemise culturelle de l'usine d'oies
- How to install MySQL
- ECU简介
- "21 days proficient in typescript-3" - install and build a typescript development environment md
- 美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
- C how TCP restricts the access traffic of a single client
- [deep learning] [original] let yolov6-0.1.0 support the txt reading dataset mode of yolov5
- 【剑指 Offer】66. 构建乘积数组
猜你喜欢
Etcd build a highly available etcd cluster
Jarvis OJ 简单网管协议
composer安装报错:No composer.lock file present.
DenseNet
Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)
中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户
机器学习编译第2讲:张量程序抽象
普洛斯数据中心发布DC Brain系统,科技赋能智慧化运营管理
WSL2.0安装
ECU introduction
随机推荐
ECU简介
Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
數據訪問 - EntityFramework集成
Oneforall installation and use
Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation
如何安装mysql
关于new Map( )还有哪些是你不知道的
Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
飞桨EasyDL实操范例:工业零件划痕自动识别
Application of threshold homomorphic encryption in privacy Computing: Interpretation
深耕5G,芯讯通持续推动5G应用百花齐放
普洛斯数据中心发布DC Brain系统,科技赋能智慧化运营管理
Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
Games101 notes (III)
DenseNet
清晰还原31年前现场,火山引擎超清修复Beyond经典演唱会
Raspberry pie 4B installation pytorch1.11
PHP人才招聘系统开发 源代码 招聘网站源码二次开发
BS-XX-042 基于SSM实现人事管理系统