[Swift]LeetCode293. 翻转游戏 $ Flip Game
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10692426.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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 []
.
您正在和您的朋友玩以下翻转游戏:给定一个仅包含这两个字符的字符串:+和-,您和您的朋友轮流将两个插入“++”翻转为“--”。游戏结束时,一个人不能再做一个动作,因此另一个人将是赢家。
写一个函数来计算一次有效移动后字符串的所有可能状态。
例如,给定s=“+++++”,移动一次后,它可能成为以下状态之一:
[ "--++", "+--+", "++--"]
如果没有有效的移动,请返回空列表[]。
Solution:
1 class Solution { 2 func generatePossibleNextMoves(_ s:String) ->[String] { 3 var res:[String] = [String]() 4 var arrS:[Character] = Array(s) 5 for i in 1..<s.count 6 { 7 if arrS[i] == "+" && arrS[i - 1] == "+" 8 { 9 res.append(s.subString(0, i - 1) + "--" + s.subString(i + 1)) 10 } 11 } 12 return res 13 } 14 } 15 16 extension String { 17 // 截取字符串:从index到结束处 18 // - Parameter index: 开始索引 19 // - Returns: 子字符串 20 func subString(_ index: Int) -> String { 21 let theIndex = self.index(self.endIndex, offsetBy: index - self.count) 22 return String(self[theIndex..<endIndex]) 23 } 24 25 // 截取字符串:指定索引和字符数 26 // - begin: 开始截取处索引 27 // - count: 截取的字符数量 28 func subString(_ begin:Int,_ count:Int) -> String { 29 let start = self.index(self.startIndex, offsetBy: max(0, begin)) 30 let end = self.index(self.startIndex, offsetBy: min(self.count, begin + count)) 31 return String(self[start..<end]) 32 } 33 }
转载于:https://www.cnblogs.com/strengthen/p/10692426.html
[Swift]LeetCode293. 翻转游戏 $ Flip Game相关推荐
- Flip Game翻转游戏
4*4的棋盘,棋盘上的每个位置都是黑或白,每个位置的颜色可以翻转,并且翻转一个位置的同时其周围的棋子的颜色也同时翻转.给一个目标状态,问多少步可以到达目标状态. 广度优先搜索. #include &l ...
- NC106350 POJ1753 Flip Game 翻转游戏
网上上课写的题目 POJ传送们http://poj.org/problem?id=1753 先贴代码,感觉里面重点部分说的已经比较清楚了 #include <iostream> #inc ...
- 【OpenCV3】图像翻转——cv::flip()详解
在opencv2和opencv中,cv::flip()支持图像的翻转(上下翻转.左右翻转,以及同时均可). 具体调用形式如下: void cv::flip(cv::InputArray src, // ...
- OpenCV图像翻转:flip()函数的使用
功能描述 flip()函数翻转一个二维的矩阵,包含垂直翻转,水平翻转,以及垂直水平翻转, 函数 cv::flip 以以下三种方式之一翻转矩阵(行和列索引从0开始): 函数使用示例方案如下: flipC ...
- [Swift]LeetCode832. 翻转图像 | Flipping an Image
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- LeetCode 293. 翻转游戏
文章目录 1. 题目 2. 解题 1. 题目 你和朋友玩一个叫做「翻转游戏」的游戏,游戏规则:给定一个只有 + 和 - 的字符串. 你和朋友轮流将 连续 的两个 "++" 反转成 ...
- 51nod-1337:翻转游戏
1337 翻转游戏 题目来源: TopCoder 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 这个游戏与灯有关,有M只灯排成一行,编号为0,1, ...
- [Swift]LeetCode493. 翻转对 | Reverse Pairs
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- [Swift]LeetCode289. 生命游戏 | Game of Life
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
最新文章
- R语言dplyr包数据列重排(reorder)实战:把特定数据列移动到第一列、把特定数据列移动到最后一列、数据列多列重排、按照字母顺序重排数据列、把数据列反序
- Java设计模式之五大创建型模式(附实例和详解)
- 公司--页面调用日期控件 WdatePicker日历控件使用方法
- HDLBits 系列(20)移位寄存器(逻辑移位、算术移位、循环移位)
- 【Flutter】Flutter 布局组件 ( PhysicalModel 组件 )
- DIP第三章习题解答
- spring boot第七讲
- [云炬创业基础笔记]第二章创业者测试8
- vue项目封装axios请求
- FTP的主动模式与被动模式
- java大数据组件Kafka
- mysql 自定义排序函数_MySQL自定义排序函数FIELD()
- oozie JAVA Client 编程提交作业
- 深入理解Java中四种创建对象的方式
- MATLAB 2018a Mac版安装激活教程
- 加班奖励 python_“加班做了2天的方案,同事用Python半个小时就搞定了?”
- 互联网产品运营必备工具大全
- springBoot打包瘦身
- 仿iPhone开关按钮switch
- 开一间水果店投资多少钱,开家水果店投资大不大