LeetCode 1823: Find the Winner of the Circular Game (约瑟夫环问题)

【LeetCode】1823. 找出游戏的获胜者 Find the Winner of the Circular Game 作者: id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 关键词:力扣,LeetCode,题解,清晰讲解,算法,约瑟夫环,Python,Java,C++ 目录 题目描述 题目大意 解题方法 问题描述 解法 数学推导 定义: 整体思路: 1. 定义新序列上函数 2. 求解新函数 附录 1. p ( x ) p(x) p(x) 的推导 附录 2. p ? 1 ( x ) p^{-1}(x) p?1(x) 的推导 附录 3. 消除括号里的模运算 阅读详情
  1. Find the Winner of the Circular Game
    Medium
    There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend brings you to the (i+1)th friend for 1 <= i < n, and moving clockwise from the nth friend brings you to the 1st friend.

The rules of the game are as follows:

Start at the 1st friend.
Count the next k friends in the clockwise direction including the friend you started at. The counting wraps around the circle and may count some friends more than once.
The last friend you counted leaves the circle and loses the game.
If there is still more than one friend in the circle, go back to step 2 starting from the friend immediately clockwise of the friend who just lost and repeat.
Else, the last friend in the circle wins the game.
Given the number of friends, n, and an integer k, return the winner of the game.

Example 1:

Input: n = 5, k = 2
Output: 3
Explanation: Here are the steps of the game:

  1. Start at friend 1.
  2. Count 2 friends clockwise, which are friends 1 and 2.
  3. Friend 2 leaves the circle. Next start is friend 3.
  4. Count 2 friends clockwise, which are friends 3 and 4.
  5. Friend 4 leaves the circle. Next start is friend 5.
  6. Count 2 friends clockwise, which are friends 5 and 1.
  7. Friend 1 leaves the circle. Next start is friend 3.
  8. Count 2 friends clockwise, which are friends 3 and 5.
  9. Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.
    Example 2:

Input: n = 6, k = 5
Output: 1
Explanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.

Constraints:

1 <= k <= n <= 500

Follow up:

Could you solve this problem in linear time with constant space?

解法1:double linked list。不过空间不让过。

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    struct Node *prev, *next;
    int val;
} Node;

int findTheWinner(int n, int k) {
    printf("sizeof(Node *) = %ld\n", sizeof(Node *));
    Node *head = (Node *)malloc(sizeof(Node *));
    head->val = 1; 
    head->prev = NULL; 
    head->next = NULL;
    Node *origHead = head;
    Node *node;
    for (int i = 2; i <= n; i++) {
        node = (Node *)malloc(sizeof(Node *));
        node->val = i;
        head->next = node;
        node->prev = head;
        head = node;
    }
    head->next = origHead;
    origHead->prev = head;
    node = origHead;
    while (n > 0) {
        for (int i = 0; i < k - 1; i++) {
            node = node->next;
        }
        node->prev->next = node->next;
        node->next->prev = node->prev;
        node = node->next;
        n--;
    }
    return node->val;
}

二刷:

struct Node {
    int index;
    Node *prev, *next;
    Node(int id = 0, Node *pr = NULL, Node *ne = NULL) : index(id) {}
};

class Solution {
public:
  int findTheWinner(int n, int k) {
        Node *head = new Node(1);
        Node *node = head;
        for (int i = 2; i <= n; i++) {
            Node *newNode = new Node(i);
            node->next = newNode;
            newNode->prev = node;
            node = newNode;
        }
        node->next = head;
        head->prev = node;
        Node *pos = head;
        int count = 0;
        while (count != n - 1) {
            for (int i = 1; i < k; i++) {
                pos = pos->next;
            }
            Node *prevNode = pos->prev;
            Node *nextNode = pos->next;
            prevNode->next = nextNode;
            nextNode->prev = prevNode;
            pos = nextNode;
            count++;
        }
        return pos->index;
    }
};
搭建适用于lammps中opls-aa力场的文件流程 现在我已经解决了以上问题,并且通过我的方法可以实现从空白文档到搭建完成以上全部内容,后期只要细心检查即可。需要的data文件最关键是获得相应阴阳离子的mmol文件,因此问题转化为构建一份mmol文件。主要用到的软件及程序:gaussian09,makemol程序(tranal),opls-aa的参数文件。二面角个数:成角原子序号及对应的参数(这里PF6的结构没有二面角,所以为0)参考文献个数:文献名(空够行数即可,这些是给自己的备忘))和自身体系对应的参考文献,修改或填入对应的参数即可。 阅读详情

相关推荐

C++总结笔记

/ ++操作 这是因为定义的数组,申请的是连续的内存空间,当ptr++(ptr是int类型的指针变量),地址空间向后偏移是以int类型的大小为单位进行偏移的,相当于int类型数组元素的向后偏移。使用场景:它是最通用的转换形式,用于将一种类型转换为另一种类型,例如基本数据类型的转换(如 float 转 int),指向基类的指针或引用转为指向派生类的指针或引用等。在派生类被调用时会调用派生类中的函数,不会调用基类中的被重写的函数(基类中的函数需要加上virtual才可以,即基类中的函数是虚函数)。

weixin_50890461的博客 5238

力扣236单周赛题目分享

5727.找出游戏的获胜者 https://leetcode-cn.com/problems/find-the-winner-of-the-circular-game/ 难度:中等 题目: 共有 n 名小伙伴一起做游戏。小伙伴们围成一圈,按 顺时针顺序 从 1 到 n 编号。确切地说,从第 i 名小伙伴顺时针移动一位会到达第 (i+1. 名小伙伴的位置,其中 1 <= i < n ...

清风Python 476

【Creator】cocoscreator 2.4 apk还原为工程 (1)

Cocos Creator 2.4 还原图片 meta文件

jokesuser的博客 2260

LeetCode——1823. 找出游戏的获胜者(Find the Winner of the Circular Game)[中等]——分析及代码(Java)

LeetCode——1823. 找出游戏的获胜者[Find the Winner of the Circular Game][中等]——分析及代码[Java]一、题目二、分析及代码1. 模拟(1)思路(2)代码(3)结果2. 递推(1)思路(2)代码(3)结果三、其他 一、题目 共有 n 名小伙伴一起做游戏。小伙伴们围成一圈,按 顺时针顺序 从 1 到 n 编号。确切地说,从第 i 名小伙伴顺时针移动一位会到达第 (i+1) 名小伙伴的位置,其中 1 <= i < n ,从第 n 名小伙伴顺时针

江南土豆的博客 1423

安装Quartus-II 13 和Modelsim

目录一、Quartus-II安装1.Quartus-II 13下载2.Quartus-II 13安装3.Quartus-II 13注册二、Modelsim安装 实验目的:安装Quartus-II 13 和Modelsim 实验环境:Windows10 参考资料:Quartus-II安装及使用 一、Quartus-II安装 1.Quartus-II 13下载 正点原子官方资料下载 百度网盘下载链接: https://pan.baidu.com/s/1a9d-bq9RZmWrRV542X4IEA 提取码:

qqq080的博客 8644

嵌入式工程师成长之路(10)——项目举例

送给大学毕业后找不到奋斗方向的你(每周不定时更新)

云轩阁 5万+

LeetCode-1823. 找出游戏的获胜者【队列,递归,数组,数学,模拟】

题目描述: 共有 n 名小伙伴一起做游戏。小伙伴们围成一圈,按 顺时针顺序 从 1 到 n 编号。确切地说,从第 i 名小伙伴顺时针移动一位会到达第 (i+1) 名小伙伴的位置,其中 1 <= i < n ,从第 n 名小伙伴顺时针移动一位会回到第 1 名小伙伴的位置。 游戏遵循如下规则: 从第 1 名小伙伴所在位置 开始 。 沿着顺时针方向数 k 名小伙伴,计数时需要 包含 起始时的那位小伙伴。逐个绕圈进行计数,一些小伙伴可能会被数过不止一次。 你数到的最后一名小伙伴需要离开圈子,并视作输掉

zik的博客 557

Qt插件之Qt Designer插件实现

相比于通过控件提升来使用自定义控件,在Qt Designer中直接使用自定义控件更加方便和直观。这里就介绍一下如编写和注册Qt Designer插件。Qt Designer插件本质上来说就是一个动态库,和普通的动态库的区别是插件中导出了Qt Designer能识别的接口。...

yang1fei2的博客 3104

【Leetcode】1823. Find the Winner of the Circular Game(配数学证明)

个人的情况下剩下的人的编号,第一轮之后剩下的人为。问最后剩下的人编号是多少。

数学、算法爱好者的博客 458

VTK GPU 加速渲染技术

/ 创建颜色工具// 创建立方体数据// 创建OpenGL映射器(注意使用vtkOpenGLPolyDataMapper而不是常规的vtkPolyDataMapper)// 启用VBO(顶点缓冲对象)// 声明几何体是静态的以获得最佳性能// 创建actor// 创建渲染器、渲染窗口和交互器renderWindow->SetWindowName("VTK自定义着色器示例");// 添加actor到渲染器。

深之JohnChen的专栏 1242

【车联网】Automotive 软件过程能力成熟度模型——ASPICE标准入门介绍

ASPICE标准详细定义了与汽车电子软件开发相关的各种术语和概念,包括软件组件、系统集成、测试和验证等。这些定义确保了所有开发团队在使用相同术语时具有一致的理解。

奶爸的编程之路,也就一周冷个三天 2680

(美服leetcode)1823. Find the Winner of the Circular Game

题目 There arenfriends that are playing a game. The friends are sitting in a circle and are numbered from1toninclockwise order. More formally, moving clockwise from theithfriend brings you to the(i+1)thfriend for1 <= i < n, and moving cloc...

Stan_lu的博客 336

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

1823. Find the Winner of the Circular Game

The description of the problem There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend brings you to the (i+1)th friend for 1 <

SUNNY的博客 533

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

LeetCode-Day103(C++) 1249. 移除无效的括号

找出游戏的获胜者 共有 n 名小伙伴一起做游戏。小伙伴们围成一圈,按 顺时针顺序 从 1 到 n 编号。确切地说,从第 i 名小伙伴顺时针移动一位会到达第 (i+1) 名小伙伴的位置,其中 1 <= i < n ,从第 n 名小伙伴顺时针移动一位会回到第 1 名小伙伴的位置。 游戏遵循如下规则: 从第 1 名小伙伴所在位置 开始 。 沿着顺时针方向数 k 名小伙伴,计数时需要 包含 起始时的那位小伙伴。逐个绕圈进行计数,一些小伙伴可能会被数过不止一次。 你数到的最后一名小伙伴需要离开圈子,并.

weixin_45401129的博客 406

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

约瑟夫环——递推公式详解(leetcode 1823. 找出游戏的获胜者)

约瑟夫环——递推公式详解(leetcode 1823. 找出游戏的获胜者) 约瑟夫环问题 约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知 n 个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。从第一个人开始报数,数到 k 的那个人出圈;他的下一个人又从 1 开始报数,数到 k 的那个人又出圈;依此规律重复下去,直到剩余最后一个胜利者。 题目:1823. 找出游戏的获胜者 详解 解法有两种,模拟和递推;模拟往往是人们最容易想到的,可用多种数据结构来解决,代码放在了文末,本文着重讲解递推解法。 问题 举

jokerMingge的博客 4593

Leetcode 1823.找出游戏的获胜者

队列+模拟

cwtnice的博客 297

Leetcode 1535. Find the Winner of an Array Game(分别用List和数组实现)

Leetcode 1535. Find the Winner of an Array Game 题目链接: Find the Winner of an Array Game 难度:easy 题目大意: 给出一个数组,将数组第一个元素和第二个元素相比较,胜者排到第一个,败者排到数组最后。输入整数k,求按照这个规则第一个连续获胜k次的数。 思路: 思路1: 用List来模拟这个游戏规则,寻找符合题意的数。注意k如果非常大的话会超时,如果k大于等于数组长度减一,即要寻找比除自己外的其它k-1个都大的数,那就是数

xue1236的博客 239

【约瑟夫环】Leetcode 1823 找出游戏的获胜者|围圈报数出局 |猴子选王|约瑟夫环|剑指 Offer 62. 圆圈中最后剩下的数字

题目描述: n个人围成一圈,报到第k个数的人出局,重复报数,最后剩下的一个人是获胜者。 数据结构:队列。 解答1(繁琐):参考了百科“约瑟夫问题”中c++的思路。 class Solution { public int findTheWinner(int n, int k) { List<Integer> queue = new ArrayList<Integer>(); if(n == 1)return 1; .

朕不知如何疼你 755

LeetCode 1823. 找出游戏的获胜者

LeetCode 1823. 找出游戏的获胜者 文章目录LeetCode 1823. 找出游戏的获胜者题目描述一、解题关键词二、解题报告1.思路分析2.时间复杂度3.代码示例2.知识点总结 题目描述 共有 n 名小伙伴一起做游戏。小伙伴们围成一圈,按 顺时针顺序 从 1 到 n 编号。确切地说,从第 i 名小伙伴顺时针移动一位会到达第 (i+1) 名小伙伴的位置,其中 1 <= i < n ,从第 n 名小伙伴顺时针移动一位会回到第 1 名小伙伴的位置。 游戏遵循如下规则: 从第 1

Datao_csdn的博客 4111

StarUML启动时候出现"System Error. Code:1722. RPC服务器不可用."错误的解

 StarUML是用得很顺手的UML工具,但是启动时候每次都会出现"System Error. Code:1722. RPC服务器不可用."错误.一般来说这个应该是某个Windows服务没有打开所导致的问题.后来才发现:原来是Print Spooler这个服务没有启动,只要启动这个服务就可以了,启动StarUML的时候就不会报错了.

pengchengwanlirwx的专栏 1594

golang力扣leetcode 1823.找出游戏的获胜者

1823.找出游戏的获胜者1823.找出游戏的获胜者题解代码 1823.找出游戏的获胜者 1823.找出游戏的获胜者 题解 题目:n个位围成一个环,每次删除第k个人,求最后一个人是谁 思路:约瑟夫环 1.在最后一轮中,只剩一个人,下标pos1=(0+k)%1 2.该人既然是最后一个人,说明他在每一轮都是安全的 3.倒数第二轮:pos2=(pos1+k)%2 4.倒数第三轮:pos3=(pos2+k)%3 5.倒数第n轮:posN=(pos[N-1]+k)%n 6.得到递推公式,posN=(pos[N-1]+

清风 1020

leetcode.1823. 找出游戏的获胜者

解题思路 简单模拟即可 代码 class Solution { public int findTheWinner(int n, int k) { int[] people = new int[n]; int index=0,sum=n; while(sum!=1){//存活人数大于1 for(int i=1;i<k;){ for(int j=index+1;j<=n;j++){

weixin_45221477的博客 328

Leetcode 1823 找出游戏的获胜者 (约瑟夫环问题)

Leetcode 1823 找出游戏的获胜者 (约瑟夫环问题)

acmnickzhang的专栏 1159

约瑟夫环问题- leetcode 1823. 找出游戏的获胜者

1823. 找出游戏的获胜者 使用链表模拟 class Solution { // 模拟 public int findTheWinner(int n, int k) { List<Integer> list = new ArrayList<Integer>(); // 模拟环 for (int i = 1; i <= n; i++) { list.add(i); } int start = 0; // 0 开始 int

weixin_56915683的博客 1199

[leetCode1823]约瑟夫环

采用逆向思考的方法,倒退出最后这个元素在最开始所在的位置 假设:size代表当前有几个元素,pos代表最后剩下的元素在当前环里面所在的下标,k代表每k个元素删除一个,n代表最开始有几个元素 当最后只剩一个元素的时候,它的下标是0,即size=1,pos=0,我们称这个元素为r; 当剩两个元素的时候,r的下标应该为:(0+k)% 2,size为2; 0是上一轮r的位置下标 所以依次递推,当剩n-1个元素的时候,r的下标为pos,那么最开始有n个元素的时候,r的下标应该为:(pos+k)%size cla

weixin_43810348的博客 677
上一篇: LintCode1300: Bash Game (DP和数学题)
下一篇: 谈谈C/C++的指针,数组和可变长数组
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值