[Leetcode] 292. Nim Game

博弈论2:尼姆游戏(取物游戏)(Nim Game) 两个玩家,I & II,每轮交替行动有三堆筹码,筹码个数分别为x1,x2,x3(xi都是正整数)每轮游戏中,选取一个非空的筹码堆i从堆中取走>=1个筹码(拿走一个或更多个,最多把这一堆拿空)拿走最后一个筹码的人获胜。(拿完之后三堆都没有筹码了) 阅读详情

Description:


You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.

Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.

Example:

Input: 4
Output: false 
Explanation: If there are 4 stones in the heap, then you will never win the game;
             No matter 1, 2, or 3 stones you remove, the last stone will always be 
             removed by your friend.

 

class Solution {
    public boolean canWinNim(int n) {
        boolean result = false;
        if (n < 4 && n > 0){
            result = true;
        }
        else if (n % 4 == 0){
            result = false;
        }
        else if (n % 4 != 0){
            result = true;
        }
        return result;
    }
}

MATLAB与ROS联合仿真——控制类功能模块介绍 阅读详情

相关推荐

TYPE-C接口电路设计篇(一)

常见USB接口类型 随着USB Type-C接口的推行,USB Type-C是一种通用串行总线(USB)的硬件接口形式,USB Type-C接口在消费类产品普及程度越来越高,常见的适配器、笔电、手机等产品都兼容USB Type-C设计,接口多元化逐渐趋向归一,从而可以看出USB Type-C的重要性。 在了解USB Type-C接口之前,我们先来回顾下USB Type-A和USB Type-B接口,USB3.0 Type-A和Type-B接口定义分别如下。 USB 3.0...

JWu的博客 3万+

尼姆游戏(Nim Game)入门

水

weixin_62528401的博客 4805

RDMA 出现背景、如何实现?如何测试连通性?

文章目录一、背景:1、传统的 网络通信:2、传统网络通信的问题:二、RDMA :1、出现背景:2、RDMA 简介:3、如何工作:4、RDMA 与 Infiniband 的关系:5、支持 RDMA 的其他协议:6、RDMA 的优势:7、通过 Infiniband 实现 RDMA :8、如何测试 RDMA 连通性:   一、背景: 1、传统的 网络通信:     &n...

郑泽林 2162

LeetCode——292. Nim 游戏(Nim Game)[简单]——分析及代码(Java)

LeetCode——292. Nim 游戏[Nim Game][简单]——分析及代码[Java]一、题目二、分析及代码1. 数学(1)思路(2)代码(3)结果三、其他 一、题目 你和你的朋友,两个人一起玩 Nim 游戏: 桌子上有一堆石头。 你们轮流进行自己的回合,你作为先手。 每一回合,轮到的人拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。 假设你们每一步都是最优解。请编写一个函数,来判断你是否可以在给定石头数量为 n 的情况下赢得游戏。如果可以赢,返回 true;否则,返回 false

江南土豆的博客 839

camera——HAL层知识分享

CameraHAL层是Android相机系统的核心中间层,负责连接Framework层与硬件驱动层。它通过标准化接口解耦软硬件,实现跨平台兼容性,同时为厂商提供硬件定制空间。主要功能包括:设备管理、数据流处理、参数控制以及图像优化。关键子系统涵盖预览、拍照、录像、对焦与曝光等模块,采用HAL3的"请求-响应"模型实现精细控制。性能优化重点关注启动时间、帧率稳定性和延迟控制。通过分层架构设计(接口层-核心层-适配层)和缓冲池机制,确保高效数据传输和硬件资源调度。

2302_77429403的博客 2082

GD32H7 SPI驱动实战:手把手教你用SPI3连接外部Flash(W25Q128)并实现读写

本文详细介绍了基于GD32H7系列MCU的SPI3接口驱动W25Q128外部Flash的全流程开发指南。从硬件设计、引脚配置到SPI3外设深度配置,再到底层通信函数实现和高级功能优化,提供了完整的解决方案。特别针对高速读取、DMA传输和信号完整性等关键环节进行了优化,确保系统在-40℃~85℃范围内稳定工作。

weixin_28729205的博客 384

【数论】博弈论 —— nim游戏

学习博弈论nim游戏(先手后手)问题

gzkeylucky的博客 1828

今天出一个candence 17.4 allego的安装教程

7、安装完成,接下来进行补丁更新,找到Hotfix_SPB17.40.035_wint.iso软件 ,右击鼠标选择装载。6、因为之前选择的定制安装,这一步不知道具体要安装啥,因此都勾选,点击NECK,接下来等待安装完成。后面经过仔细对比,发现是前面更新补丁时选择了F盘,F盘是candence安装包,不是补丁。8、在破戒大师里选择安装更新补丁,并找到刚才装载的安装包里的程序,点击下一步。3、打开破戒大师,找到安装candence进行如下图选择,点击下一步。有耐心的人可以重新卸了再装一边,我就不卸了,直接装。

hcvinh的博客 2727

ACM 博弈 只是总结 ( ZZ自奋斗哥 )

以下是我从网上收集的关于组合博弈的资料汇总:有一种很有意思的游戏,就是有物体若干堆,可以是火柴棍或是围棋子等等均可。两个人轮流从堆中取物体若干,规定最后取光物体者取胜。这是我国民间很古老的一个游戏,别看这游戏极其简单,却蕴含着深刻的数学原理。下面我们来分析一下要如何才能够取胜。(一)巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取m个。最后取...

weixin_30807677的博客 1127

三极管特性曲线看不懂?手把手教你用Multisim仿真分析放大电路频率响应

本文针对模拟电路初学者,详细讲解如何利用Multisim软件进行电路仿真,将抽象的三极管特性曲线与放大电路频率响应分析变得直观可视。通过手把手教学,指导读者从导入三极管SPICE模型、仿真输出特性曲线,到分析动态工作点,最终使用波特图仪深入分析频率响应并进行补偿设计,有效帮助理解电路原理并指导实践调试。

weixin_29230059的博客 125

876-基于STM32的毫米波雷达智能睡眠监测系统

基于STM32的毫米波雷达智能睡眠监测系统通过非接触式毫米波雷达检测人体呼吸频率和体动次数,结合WIFI数据传输和OLED显示,实现睡眠质量分析与远程监控。系统核心功能包括生理参数采集、阈值设置、数据可视化及远程交互。

m0_50083169的博客 1545

LeetCode 292: Nim Game 题解

别看题这么长,其实很简单。LeetCode 292: Nim Game Problem: You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones

请叫我AXin 1278

Android SELinux——neverallow问题处理(十六)

遇到 neverallow 规则问题,千万别急着去注释/剔除里面原有的规则(原生的尽量别动)。增加 allow 规则是常见的解决办法,但是随着 android 版本的升级,系统对 SELinux 的管控越来越严,增加了大量的 neverallow。一般情况下,向默认标签授予权限的做法是错误的,其中许多权限都是 neverallow 规则所不允许的。按照上面方法去添加 SELinux 可能会违反了 neverallow 规则,编译时候会报 neverallow 相关的错误。

小旭的专栏 2694

Leetcode 292 Nim Game

Leetcode 292 Nim Game 题目描述 You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes...

qq_43271202的博客 293

深度学习工具包 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")

Camera2Demo

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

[LeetCode 292] Nim Game

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the

coder 进阶的专栏 3630

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

算法 - leetcode 292 Nim Game

算法 - leetcode 292 Nim Game 一丶题目 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你作为先手。 你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。 输入: 4 输出: false 解释: 如果堆中...

weixin_30345055的博客 871

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

LeetCode 292 Nim Game(Nim游戏)

翻译你正在和你的朋友们玩下面这个Nim游戏:桌子上有一堆石头,每次你从中去掉1-3个。谁消除掉最后一个石头即为赢家。你在取出石头的第一轮。你们中的每一个人都有着聪明的头脑和绝佳的策略。写一个函数来确定对于给定的数字是否你可以赢得这场比赛。例如,如果堆中有4个石头,那么你永远也无法赢得比赛:无论你移除了1、2或3个石头,最后一个石头都会被你的朋友所移除。原文You are playing the fo

nomasp 7634

LeetCode 292. Nim Game

LeetCode 292. Nim Game Say there are s = 4n + i (i = 1, 2, 3) stones on the table. Initial state Consider the case where n = 0, there are at most 3 stones on the table. You take the first

Stephen Wong的专栏 608

Leetcode ☞ 292. Nim Game

NIM游戏 292. Nim Game My Submissions Question Total Accepted: 47343 Total Submissions: 91979 Difficulty: Easy You are playing th

Dr_Unknown的博客 634

Leetcode #292 Nim Game

题目描述: You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone

小昕成长史 246

LeetCode 292 Nim Game

Problem: You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last s...

dilian4850的博客 172

[leetcode 292]Nim Game

Question: You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last st...

andun6858的博客 194

[LeetCode]292. Nim Game

[LeetCode]292. Nim Game题目描述思路 数字n 结果 1 2 3 1 4 0 5 6 7 1 8 0 … … 4n+1 4n+2 4n+3 1 4n 0 代码class Solution { public: bool canWinNim(int n) { if(n % 4) {

Lcharon的博客 3258

Leetcode:292. Nim Game

Leetcode题解292.Nim Game-Easy题目

Littlecuter的博客 531

leetcode292 Nim Game

292. Nim Game Total Accepted: 50767 Total Submissions: 98218 Difficulty: Easy You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac

葡萄架的程序猿 352
上一篇: [Leetcode] 762. Prime Number of Set Bits in Binary Representation
下一篇: [Leetcode] 824. Goat Latin
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值