[LeetCode] 293. Flip Game 翻转游戏
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 compute all possible states of the string after one valid move.
For example, given s = "++++", after one move, it may become one of the following states:
[
"--++",
"+--+",
"++--"
]
If there is no valid move, return an empty list [].
给一个只含有'+', '-'的字符串,每次可翻动两个连续的'+',求有多少种翻法。
解法:
java:
public class Solution {
public List<String> generatePossibleNextMoves(String s) {
List<String> res = new ArrayList<>();
char[] arr = s.toCharArray();
for(int i = 1; i < s.length(); i++) {
if(arr[i] == '+' && arr[i - 1] == '+') {
arr[i] = '-';
arr[i - 1] = '-';
res.add(String.valueOf(arr));
arr[i] = '+';
arr[i - 1] = '+';
}
}
return res;
}
}
Python:
class Solution(object):
def generatePossibleNextMoves(self, s):
"""
:type s: str
:rtype: List[str]
"""
res = []
i, n = 0, len(s) - 1
while i < n: # O(n) time
if s[i] == '+':
while i < n and s[i+1] == '+': # O(c) time
res.append(s[:i] + '--' + s[i+2:]) # O(n) time and space
i += 1
i += 1
return res
Python:
# Time: O(c * m * n + n) = O(c * n + n), where m = 2 in this question
# Space: O(n)
# This solution compares O(m) = O(2) times for two consecutive "+", where m is length of the pattern
class Solution2(object):
def generatePossibleNextMoves(self, s):
"""
:type s: str
:rtype: List[str]
"""
return [s[:i] + "--" + s[i+2:] for i in xrange(len(s) - 1) if s[i:i+2] == "++"]
C++:
// Time: O(c * n + n) = O(n * (c+1)), n is length of string, c is count of "++"
// Space: O(1), no extra space excluding the result which requires at most O(n^2) space
class Solution {
public:
vector<string> generatePossibleNextMoves(string s) {
vector<string> res;
int n = s.length();
for (int i = 0; i < n - 1; ++i) { // O(n) time
if (s[i] == '+') {
for (;i < n - 1 && s[i + 1] == '+'; ++i) { // O(c) time
s[i] = s[i + 1] = '-';
res.emplace_back(s); // O(n) to copy a string
s[i] = s[i + 1] = '+';
}
}
}
return res;
}
};
C++:
class Solution {
public:
vector<string> generatePossibleNextMoves(string s) {
vector<string> res;
for (int i = 1; i < s.size(); ++i) {
if (s[i] == '+' && s[i - 1] == '+') {
res.push_back(s.substr(0, i - 1) + "--" + s.substr(i + 1));
}
}
return res;
}
};
类似题目:
[LeetCode] 294. Flip Game II 翻转游戏 II
All LeetCode Questions List 题目汇总
[LeetCode] 293. Flip Game 翻转游戏的更多相关文章
- [LeetCode] Flip Game 翻转游戏
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- LeetCode 293. Flip Game
原题链接在这里:https://leetcode.com/problems/flip-game/description/ 题目: You are playing the following Flip ...
- leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)
914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...
- [LeetCode] 294. Flip Game II 翻转游戏 II
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [Swift]LeetCode293. 翻转游戏 $ Flip Game
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- 【2018寒假集训 Day1】【位运算】翻转游戏
翻转游戏(flip) [问题描述] 翻转游戏是在一个 4 格×4 格的长方形上进行的,在长方形的 16 个格上每 个格子都放着一个双面的物件.每个物件的两个面,一面是白色,另一面是黑色, 每个物件要么 ...
- Luogu 1764 翻转游戏 - 枚举 + 搜索
题目描述 kkke在一个n*n的棋盘上进行一个翻转游戏.棋盘的每个格子上都放有一个棋子,每个棋子有2个面,一面是黑色的,另一面是白色的.初始的时候,棋盘上的棋子有的黑色向上,有的白色向上.现在kkke ...
- [LeetCode] 190. Reverse Bits 翻转二进制位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 294. 翻转游戏 II
题目: 链接:https://leetcode-cn.com/problems/flip-game-ii/ 你和朋友玩一个叫做「翻转游戏」的游戏,游戏规则:给定一个只有 + 和 - 的字符串.你和朋友 ...
随机推荐
- 微信小程序~跳页传参
[1]需求: 点击商品,跳到相应商品详情页面 [2]代码: (1)商品列表页 <view class="goodsList"> <view wx:for=&quo ...
- 更改DHCP服务器默认日志存储位置
DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一种有效的IP 地址分配手段,已经广泛地应用于各种局域网管理.它能动态地向网络中每台计算机分配唯一 ...
- 基于Asp.net core + EF + Sqlite 5分钟快速上手一个小项目
虽然该方法不会用在实际开发中,但该过程对于初学者还是非常友好的,真应了麻雀虽小,五脏俱全这句话了.好了不多废话了,直接开始!! 1.建立一个名为test的Asp.net core web应用程序 这一 ...
- finall语句是在return语句执行前被执行还是执行后被执行?
finally对try...catch的影响和执行时间 一.finally语句不被执行的两种情况 我们在学习抛出异常的时候学习到了finally语句块,这个语句块一般会被执行,但是有两种情况下不被执行 ...
- django-登录后得个人信息
Web请求中的认证:https://yiyibooks.cn/xx/django_182/topics/auth/default.html Django使用会话和中间件来拦截request 对象到认证 ...
- /etc/rc.local
/etc/rc.local是/etc/rc.d/rc.local的软连接 应用于指定开机启动的进程 开机启动不生效,则首先需要检查下/etc/rc.d/rc.local是否具有可执行权限 在配置文件中 ...
- P4899 【[IOI2018] werewolf 狼人】
感觉已经几次碰到这种类型的题目了,写篇\(Blog\)总结一下 题意: 是否存在一条\((s_i, t_i)\)的路径,满足先只走编号不超过\(L_i\)的点,再走编号不超过\(R_i\)的点 \(S ...
- Evaluation of Sampling and Cross-Validation Tuning Strategies for Regional-Scale Machine Learning Classification
比较了不同抽样方法(随机,分层等比随机,分层不等比随机,人为),不同交叉验证方法(k折,留一法,蒙特卡洛),不同样本范围大小的效果,最后都是用SVM分类 结果是k折验证最好,人为选择样本最差.小范围小 ...
- Eclipse各个版本及其对应代号、下载地址列表【转】
Eclipse各个版本及其对应代号.下载地址列表 Eclipse各个版本及其对应代号.下载地址列表版本号 代码 日期 下载地址Eclipse 3.1 IO[木卫一,伊奥] 2005 http://ar ...
- 腾讯云手动搭建nginx+php-fpm并自启动
自己一点小爱好,搭建了一个小网站植物大战僵尸百科, 使用的是腾讯云,市场里的镜像不好用,所以自己手动搭建一波. centos 7 编译安装 php-7.2.11的步骤 在官网下载php-7.2.11的 ...