当前位置:网站首页>剑指 Offer 62. 圆圈中最后剩下的数字
剑指 Offer 62. 圆圈中最后剩下的数字
2022-07-02 01:54:00 【Yake1965】
剑指 Offer 62. 圆圈中最后剩下的数字
lastRemaining(n - 1, m) 是 n - 1 个数,即 0, 1, …, n - 2,从数字 0 开始,每次从这个圆圈里删除第 m 个数字(删除后从下一个数字开始计数)。和真实的序列 m, m + 1, …, n - 2, 0, …, m - 2 做一个影射。x -> (m + x) % n。
方法一:递归
class Solution:
# Python 默认的递归深度不够,需要手动设置
sys.setrecursionlimit(100000)
def lastRemaining(self, n: int, m: int) -> int:
if n == 0: return 0
x = self.lastRemaining(n - 1, m)
return (m + x) % n
class Solution {
public int lastRemaining(int n, int m) {
if(n == 1) return 0;
int index = lastRemaining(n - 1, m); // x 是按 0, 1, 2 ... n - 2 计算的结果
return (index + m) % n; // 和实际的作一个影射
}
}
方法二:迭代
规律:每一轮删掉第 m 个数都是将前 m - 1 个数移到数组末尾以供循环,每次 i 相当于左移了 m 个位置。
追本溯源:从最后一次 index 为 0 开始溯源,依次让 index 处的 i 右移 m 个位置,即加上 m。
倒序每一次 index + m 就能找到上一轮那个 i 所在的 index,同时防止越界要取余该轮数组 size
class Solution {
public int lastRemaining(int n, int m) {
int index = 0; // 从最后剩下的一个数反推,最后得到 0, 1, ... , n - 1 中的索引。
for (int i = 2; i <= n; ++i) {
index = (m + index) % i;
}
return index;
}
}
方法三:模拟法,用 ArrayList 模拟:时间复杂度 O(n^2)
class Solution {
public int lastRemaining(int n, int m) {
List<Integer> list = new ArrayList<>(); // LinkedList 超时
for (int i = 0; i < n; i++) {
list.add(i);}
int index = 0;
while (n > 1) {
index = (index + m - 1) % n;
list.remove(index);
n--;
}
return list.get(0);
}
}
1823. 找出游戏的获胜者
方法一:递归
class Solution {
public int findTheWinner(int n, int k) {
if(n == 1) return 1;
int x = findTheWinner(n - 1, k); // x 转换成索引
return (k + x - 1) % n + 1;
}
}
方法二:迭代
class Solution {
public int findTheWinner(int n, int k) {
int winner = 1;
for (int i = 2; i <= n; i++) {
winner = (k + winner - 1) % i + 1;
}
return winner;
}
}
方法三:队列模拟
class Solution {
public int findTheWinner(int n, int k) {
Deque<Integer> q = new ArrayDeque<>();
for(int i = 1; i <= n; i++) q.add(i);
while(q.size() > 1){
for(int i = 0; i < k - 1; i++){
q.offer(q.pop());
}
q.pop();
}
return q.peek();
}
}
边栏推荐
- 2022 Q2 - Summary of skills to improve skills
- Experimental reproduction of variable image compression with a scale hyperprior
- It's already 30. Can you learn programming from scratch?
- What is AQS and its principle
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- Parted command
- Data analysis on the disaster of Titanic
- leetcode2312. 卖木头块(困难,周赛)
- 城市选择器组件实现原理
- Electronic Society C language level 1 32, calculate the power of 2
猜你喜欢
Basic concepts of machine learning
With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
1069. Division of convex polygons (thinking, interval DP)
【LeetCode 43】236. The nearest common ancestor of binary tree
leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
医药管理系统(大一下C语言课设)
机器学习基本概念
Data analysis on the disaster of Titanic
Convolutional neural network (including code and corresponding diagram)
随机推荐
Openssl3.0 learning XXI provider encoder
479. Additive binary tree (interval DP on the tree)
ES6 new method of string
np.where 和 torch.where 用法
遊戲思考15:全區全服和分區分服的思考
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
Redis有序集合如何使用
1222. Password dropping (interval DP, bracket matching)
* and & symbols in C language
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
Learn basic K-line diagram knowledge in three minutes
PR second training
[Floyd] post disaster reconstruction
Failed to transform file 'xxx' to match attributes
C language 3-7 daffodils (enhanced version)
new和malloc的区别
现货黄金分析的技巧有什么呢?
电子协会 C语言 1级 32、计算2的幂
城市选择器组件实现原理
Introduction to ffmpeg Lib