[LeetCode294] Flip Game II

【Gurobi学习笔记】翻转游戏(Flip Game):数学模型与Python调用Gurobi实现 本文介绍了翻转游戏(Flip Game)的数学规划模型,并给出了Python调用Gurobi求解其数学规划模型的完整代码。可以做Mathematical Modelling的学习和训练。 阅读详情

You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip twoconsecutive "++" into "--". The game ends when a person can no longer make a move and therefore the other person will be the winner.

Write a function to determine if the starting player can guarantee a win.

For example, given s = "++++", return true. The starting player can guarantee a win by flipping the middle "++" to become "+--+".

Follow up:
Derive your algorithm's runtime complexity.

solution:

recursive and backtracking, after you move the game, it's left a non-movable result, you will win. Also, here use array to make change and restore. Look at code, there is one line

boolean win = !canWin(cs);
For example"+++", after first move, --+ is non-movable


public void test() {
        //win ++, +++, ++++, + ++ ++ +++
        //failed,+++++,++--++--
        String[] testcase = {"+","-","++","--","+++","++++","+--+","-+-+","+++++"};
        for(int i=0;i<testcase.length;i++){
            System.out.println(firstPlayWinning(testcase[i]));
        }
    }
    public boolean firstPlayWinning(String s1) {
        return canWin(s1.toCharArray());
    }
    public boolean canWin(char[] cs) {
        for(int i=0;i<cs.length-1;i++) {
            if(cs[i] == cs[i+1] && cs[i]=='+') {
                cs[i] = '-';
                cs[i+1] = '-';
                boolean win = !canWin(cs);
                cs[i] = '+';
                cs[i+1] = '+';
                if(win) return true;
            }
        }
        return false;
    }


LeetCode 294. Flip Game II (你给翻译翻译,什么叫做guarantee?) LeetCode 294. Flip Game II: 你给翻译翻译,什么叫做guarantee? 阅读详情

相关推荐

Copula函数

Copula是处理统计中随机变量相关性问题的一种方法。本文是对Copula函数的各种定义,Copula函数的思想,Copula函数的性质,Copula函数的类型等理论资料总结

Explore 5万+

Flip Game (不太暴力的解法)

Flip Game1. 大概题意2. 不太暴力的思路3. 代码3.1 C++3.2 Java 前一篇暴力解法 (链接) 1. 大概题意 翻转游戏是在一个 4x4 的方形场地上进行的,在其 16 个方格中的每一个方格上都放置有两个面的棋子。 每个棋子的一面是白色的,另一面是黑色的,每个都是黑色或白色的一面朝上。 每轮你翻转 3 到 5 块,从而将它们上侧的颜色从黑色变为白色,反之亦然。 每轮都根据以下规则选择要翻转的棋子: 1.选择 16 个中的任何一个。 2.将选定的棋子以及所有相邻的棋子翻转到所选棋

m0_52313753的博客 1397

初学树莓派——(九)DHT11数据传入ONENET

目录 前言 DHT11实现本地温湿度监测 DHT11.py 接线图 效果图 温湿度数据上传至ONENET 新建产品 新建设备 ONENET.py 测试 Shell脚本配置 效果图 前言 本文分部分实现温湿度上传至OneNet云平台,本地数据监测,数据上传 新建一个文件夹,文件夹内包含DHT11.py、ONENET.py、tmp_data.txt、hum_data.txt DHT11实现本地温湿度监测 DHT11.py import RPi.GPIO as GPIO

会点灯的大力水手的博客 2617

翻转游戏(Flip_Game)

翻转游戏——Flip Game问题描述样例代码 问题描述 翻转游戏:给定一个只包含两种字符的字符串:+和-。你可以将两个连续的“++”翻转成"–",你需要翻转一次,并找到翻转后所有可能得到的结果。编写一个程序,找到字符串在一次有效翻转后的所有可能状态。 样例 样例1 输入: s = “++++” 输出: [ “–++”, “±-+”, “+±-” ] 样例2 输入: s = “—++±++±+” 输出: [ “—++±±–+”, “—++±–±+”, “—±–++±+”, “-----±++±+” ]

weixin_43894655的博客 1375

SAP各模块表清单及逻辑关系介绍

分享韩国同行总结的各模块的表清单及逻辑关系,这对ABAP顾问和各模块业务顾问应该非常有用的 物料主数据 SD与MM PS模块 PP模块 QM模块 CO模块 FI模块

weixin_40672823的博客 9616

晶圆测试厂wafer map管理建议

按照Recipe设定bin定义 应有版本管理概念,一般使用最新版本做为活动版本即可 Bin明细中应包含 BinID、BinCode、BinName、BinType、BinColor、Description BinID可使用数字类型 BinCode可参照实际机台的Bin,用于匹配 有些公司也将BinID、BinCode理解为SBin和HBin 即 Software Bin & Hardware Bin 一般也将BinID对应到客户的Bin BinType

凯神做开发 1万+

Flip Game

总时间限制:1000ms内存限制:65536kB描述(没有翻译)bwbwwwwwbbwbbwwbbwbwbwwwwwwbwwwb输入输出样例输入bwwbbbwbbwwbbwww样例输出4来源代码(自认为很短):mp[sx][sy];i<4;i++){mp[x][y]=!mp[x][y];j>=0;j--){flip(0,j);cnt++;a/=2;i<4;i++){j<4;j++){

xuxizhe0104的博客 508

碳纳米管NRAM:突破5纳米制程的下一代存储技术

在半导体存储领域,非易失性存储器和随机存取存储器是两大基础技术方向。传统DRAM速度快但断电易失,NAND Flash非易失但写入慢、寿命有限,两者在先进制程下都面临物理极限挑战。碳纳米管作为一种新型纳米材料,凭借其优异的导电性、机械强度和尺寸微缩潜力,为存储技术革新提供了物理基础。基于碳纳米管的NRAM技术,通过控制纳米管间的可逆接触实现电阻状态切换,兼具DRAM级速度和Flash的非易失性,同时具备近乎无限的擦写寿命和出色的CMOS工艺兼容性。这项技术不仅能满足5纳米以下制程的存储需求,在数据中心缓存、

weixin_42576804的博客 434

leetcode:294.翻转游戏

这道题不是问 “1p是否会怎么选都会赢”,而是 “如果1p每次都选特别的两个+,最终他会不会赢”。所以 canWin 这个函数的意思是 “在当前这种状态下,至少有一种选法,能够让他赢”。canWin) 的意思就变成了 “在当前这种状态下,无论怎么选,都不能赢”。所以 1p 要看的是,是否存在这样一种情况,无论 2p 怎么选,都不会赢。canWin),1p 就可以确定他会赢。题目要求先手必胜的方案,于是思路在于遍历所有的先手可能,对于每一个先手,一路玩下去,看当前先手策略是不是能让先手赢。...

OceanStar的博客 400

STM32 实现 4*4 矩阵键盘扫描(HAL库、标准库 都适用)

本文实现的代码是基于STM32HAL库的基础上的,不过标准库也可以用,只是调用的库函数不同,逻辑跟配置是一样的,按我这里的逻辑来配置即可。 1、键盘原理图: 原理举例:先把 F0-F7 内部拉高,这样这个8个引脚都是高电平,然后就进行列扫描。例如:假如按下3按钮,Y3 列扫描,把F4先拉低,然后读取F0-F3的状态,就会读出为1110,这就可 以知道是F3行拉低了,同时这时候是程序控制F4拉低的,这样就可以知道是F4列导致它转态变化了的,这样就可以定位出是F4列F3行的按键按下了;其他的列也是这样子扫描

lhh_qrsly的博客 7062

医学图像的对抗攻击:模型安全性与鲁棒性分析

医学图像的对抗攻击:模型安全性与鲁棒性分析 ,人工智能,计算机视觉,大模型,AI,本文围绕医学图像的对抗攻击展开,分析了其对AI模型的威胁。介绍了对抗样本定义、攻击原理,以及FGSM、BIM、PGD等常见攻击方法,还给出了相关代码示例。阐述了攻击对模型诊断准确性和可信度的影响,提出对抗训练、输入预处理、模型集成等提升模型安全性与鲁棒性的方法,探讨了评估指标、数据集和流程,分析了实际应用中的挑战与应对策略,最后展望了未来发展趋势。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 18万+

LeetCode 294. Flip Game II(反转游戏II)

原题网址:https://leetcode.com/problems/flip-game-ii/ You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend t

James Pan 1134

[GXYCTF2019]Ping Ping Ping - RCE(空格、关键字绕过[3种方式])

/?ip= |\'|\"|\\|\(|\)|\[|\]|\{|\}/", $ip, $match)){ echo preg_match("/\&|\/|\?|\*|\|\'|\"|\\|\(|\)|\[|\]|\{|\}/", $ip, $match); die("fxck your symbol!"); } else if(preg_match("/ /", $ip)){ die("fxck your space!"); } else

oZuoShen123的博客 1351

[leetcode294]Flip Game II

Question: You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive "++" 

清空 1185

假人有限元模型

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

深度学习工具包 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】294. Flip Game II

思路是记忆化搜索,如果某个局面是乙的必败态,则当前局面是甲的必胜态。直接枚举第一步操作即可。可以用记忆化加速,避免重复计算。,谁无法操作了谁就输。,甲和乙轮流操作,每个人可以将。

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

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

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

[Leetcode] 294. Flip Game II 解题报告

题目: You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive "++" into "

魔豆(Magicbean)的博客 810

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Leetcode 294 Flip Game II

思路: 根本没有思路,看了答案发现是backtracking方法。哎,根本想不到啊,难受。直接上代码吧。这边需要注意的是此题目意思是只要先手能赢就返回true,而不是先手不管怎么出牌都能赢才返回true。 class Solution { int len; char[] ss ; public boolean canWin(String s) { len = s.length(); ss = s.toCharArray(); retu.

GoodJobJasper的博客 219

[LeetCode294]Flip Game II

You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive "++" into "--". The gam

MoM 643

LeetCode294 Flip Game II

...

李歇特冯-兹拜因巴哈的博客 231

[leetcode] 294. Flip Game II 解题报告

题目链接:https://leetcode.com/problems/flip-game-ii/ You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend t

小榕流光的专栏 2835

【LeetCode】294. Flip Game II 解题报告 (C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法记忆化搜索日期 题目地址:https://leetcode-cn.com/problems/flip-game-ii/ 题目描述 You are playing the following Flip Game with your frien...

负雪明烛 1140

[Leetcode] 294. Flip Game II

这一题不太适合深究,目前来看适用的做法就只有backtrack,外加一个memorization作为performance optimization。这一题返回true的条件是你走了某一步,对方完全没法赢,反过来说,你走了某一步,对方只要还有一种赢法你就得返回false。然而这对你和对方来说都是一样的。所以,canWin返回true的条件就是我们尝试了这一个flip,然后对方的canWin就返...

chaochen1407的专栏 514

LeetCode #294. Flip Game II

题目描述: You are playing the following Flip Game with your friend: Given a string that contains only these two characters:+and-, you and your friend take turns to flip twoconsecutive"++"into"--"....

LawFile的博客 195

LeetCode 294. Flip Game II

Problem Description:You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive “++

xinqrs01的博客 984

[Swift]LeetCode294. 翻转游戏之 II $ Flip Game II

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/streng...

weixin_30847865的博客 178
上一篇: [LeetCode 293] Flip Game
下一篇: [LeetCode 295] Find Median from Data Stream
Mavs
博客等级 码龄17年 166粉丝 296原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值