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;
    }
};
学术规范与论文写作(雨课堂)研究生 全部答案 1:如何造就优秀的学术论文 论文语法判断题 :全部是错的 2:如何撰写学术论文 3: SCI论文写作经验分享 4:论文写作与投稿技巧 5:学术伦理道德 答案仅供参考,研究生生活不易,大家如果有时间的话,还是可以学习学习的,毕竟老师讲的不错。加油,希望读研的每一个人都能顺利毕业! ... 阅读详情

相关推荐

在 NVIDIA Orin (JetPack 6.0) 上配置 PyTorch 2.4 + Torchvision 0.19 的完整避坑指南

本文提供在NVIDIA Jetson Orin设备上,基于JetPack 6.0系统配置PyTorch 2.4与Torchvision 0.19环境的完整指南。通过使用Miniconda环境隔离、安装特定版本PyTorch夜间构建版、编译匹配的Torchvision源码等步骤,解决版本不匹配、CUDA支持等常见问题,确保AI开发环境稳定可用。

weixin_29179311的博客 369

力扣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

2024年全国大学生电子设计竞赛H题_自动行驶小车

自动行驶小车(H题)【本科组/高职高专组】

qq_74395263的博客 1万+

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 名小伙伴顺时针

江南土豆的博客 1425

IROS 2025机器人技术解析:从诡异设计到硬核应用

机器人技术正从传统的结构化环境应用,迈向复杂非结构化场景的深度交互。其核心原理在于通过仿生设计、多模态感知与智能控制算法,赋予机器人类似生物的适应性与交互能力。这一技术演进的价值在于突破传统机器人在极端环境、精密操作和自然交互中的瓶颈。例如,结合深度强化学习的全身动力学控制,使机器人能在废墟、管道等复杂地形中自主导航;而软体机器人技术则通过材料变形实现安全、柔顺的抓取操作。这些技术最终服务于极端环境巡检、精密装配和高端陪护等刚性应用场景,推动机器人从功能实现迈向与环境、人进行深度、智能交互的新阶段。

weixin_34006468的博客 457

深入解析ST MC SDK(WorkBench)工程结构:从零搭建FOC电机控制项目

本文深入解析了ST MC SDK(WorkBench)工程结构,详细介绍了如何从零搭建FOC电机控制项目。通过ST MC SDK和WorkBench工具链,开发者可以快速配置电机类型、控制策略等参数,生成完整的FOC控制工程。文章还拆解了工程结构中的核心控制模块、HAL硬件抽象层、芯片系统初始化和电机控制算法库,并提供了无传感器FOC实战技巧和常见问题排查指南,帮助开发者高效掌握STM32F407ZGT6开发板上的FOC电机控制技术。

weixin_29173777的博客 420

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

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

zik的博客 560

公文派2025:免费社区版重大安装更新!

距离上一次更新已经过去了将近一年的时间,今天我们带来了全新的免费2025社区版,该版本也是目前最新的版本,无需授权即可使用所有的功能。

似最初 1966

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

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

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

ANSYS命令流黑箱揭秘:流体渗透压力载荷的七种收敛优化策略

本文深入解析ANSYS Workbench中流体渗透压力载荷的七种收敛优化策略,重点探讨接触探测点迭代逻辑、PPCN阈值动态调整及FPAT参数优化等关键技术。通过命令流示例和实战技巧,帮助工程师有效解决流体与结构相互作用仿真中的收敛难题,提升计算效率和精度。

weixin_29274337的博客 525

qt下使用grpc编程(2) —— 练手!自己改的一个简单小例子!1

(1)在研究了grpc官方提供的helloworld例程后,自己试着写了个小程序。 (2)编写proto文件。 proto文件用于定义服务和消息,用protobuf的语法编写,protobuf是一种通信协议,详细请百度。下面附上源码: syntax = "proto3"; option java_multiple_files = true; option java_pac

u011099079的博客 3932

(美服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的博客 339

海康视觉脚本开发全流程解析

本文详细解析了海康机器视觉平台(VM)中脚本模块的开发全流程。从创建第一个脚本模块入手,深入讲解了输入参数、输出参数与内部变量的定义与区别,并提供了C#脚本编写、公用函数调用、代码结构组织以及编译调试的实用指南。旨在帮助开发者掌握海康视觉脚本开发的核心技巧,高效实现复杂的自定义视觉逻辑处理。

beer8的博客 612

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

数学建模模型总结

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

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

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

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

weixin_45401129的博客 406

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

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

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

jokerMingge的博客 4596

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Leetcode 1823.找出游戏的获胜者

队列+模拟

cwtnice的博客 298

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

Cpp环境【SDUT1128】【Code[VS]1809】【CQYZOJ1823】河床

【问题描述】   地理学家们经常要对一段河流进行测量分析。他们从上游开始向下游方向等距离地选择n(n<=30000)个点测量水位深度。得到一组数据a1,a2,…,an,回到实验室后数据分析员根据需要对数据进行分析,发掘隐藏在数据背后的规律。  最近,乌龙博士发现某种水文现象与河床地势有关,于是他指示分析员要找出一段河流中最大高低起伏差不超过K(1<=K<=100)的最长的一段。这看似一个简单的问

天地皆逆旅,你我本行人。 671

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

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

acmnickzhang的专栏 1160

【codevs1279】Guard 的无聊

题目: 我是超链接 题解: 就是一个质因数分解(要记忆化)+普通线段树 代码: #include #include #include #define N 2000000 using namespace std; int zh[1229]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97

薇小薇 994

约瑟夫环问题- 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

【Lintcode】1823. Longest Prefix of Array

题目地址: https://www.lintcode.com/problem/longest-prefix-of-array/description 给定一个数组AAA,再给定两个数xxx和yyy,求最长的AAA前缀A[0:i]A[0:i]A[0:i]使得xxx和yyy在其中的出现次数相等,且出现次数都大于000。 用两个变量记录前缀里两个数字出现次数即可。代码如下: public class Solution { /** * @param X: a integer * @

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

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]+

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

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址