题目地址:https://leetcode-cn.com/problems/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 two consecutive "++" 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.

Example:

Input: s = "++++"
Output:
[
"--++",
"+--+",
"++--"
]

Note: If there is no valid move, return an empty list [].

题目大意

把给定字符串中的++换成--,返回所有可能的结果。

解题方法

遍历

题目表述不清,其实只要把所有++换成--即可,那就很简单了,直接遍历一次就行了。

C++代码如下:

class Solution {
public:
vector<string> generatePossibleNextMoves(string s) {
vector<string> res;
if (s.empty() || s.size() == 1) return res;
for (int i = 1; i < s.size(); ++i) {
if (s[i] == '+' && s[i - 1] == '+') {
string move = s;
move[i] = move[i - 1] = '-';
res.push_back(move);
}
}
return res;
}
};

日期

2019 年 9 月 19 日 —— 举杯邀明月,对影成三人

【LeetCode】293. Flip Game 解题报告(C++)的更多相关文章

  1. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  2. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  3. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  4. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  5. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  6. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  7. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. PCA方差解释比例求解与绘图?

    目录 主成分方差解释率计算 绘图示例 主成分方差解释率计算 通常,求得了PCA降维后的特征值,我们就可以绘图,但各个维度的方差解释率没有得到,就无法获得PC坐标的百分比. 有些工具的结果是提供了维度标 ...

  2. Nginx 编译 echo 模块

    Nginx  编译 echo 模块 echo模块下载地址:https://github.com/openresty/echo-nginx-module 查看nginx已经编译的模块, nginx -V ...

  3. WSL + MobaXterm 取代虚拟机

    windows子系统安装默认C盘,这里我们直接下载安装文件安装在我们指定的目录. 1.先去官方网站下载安装文件:https://docs.microsoft.com/zh-cn/windows/wsl ...

  4. 【讲座】朱正江——基于LC-MS的非靶向代谢组学

    本次课程主题为<基于LC-MS的非靶向代谢组学>,主要分为代谢组学简介.代谢组学技术简介.非靶向代谢组学方法和数据采集.非靶向代谢组学数据分析和代谢物结构鉴定几个方面. 一.代谢组简介 基 ...

  5. Linux搭建yum仓库

    1.安装nginx 2.为nginx搭建共享目录 3.安装createrepo,创建存储库 4.客户端测试 1.安装nginx yum list |grep nginx #查看是否有可用的nginx包 ...

  6. DOM给表格添加新一行和删除整个行的内容

    DOM用appendChild()给表格添加新一行时,要注意,在HTML中没特别设置<thead>,<tbody>时,会自动添加上,所以要选择表格第一个元素在添加tr. // ...

  7. abundant

    In ecology [生态学], local abundance is the relative representation of a species in a particular ecosys ...

  8. day01 MySQL发展史

    day01 MySQL发展史 今日内容概要 数据库演变史 软件开发架构 数据库本质 数据库中的重要概念 MySQL下载与安装 基本SQL语句 今日内容详细 数据库演变史 # 1.文件操作阶段 jaso ...

  9. day10设置文件权限

    day10设置文件权限 yum复习 1.修改IP [root@localhost ~]# sed -i 's#.200#.50#g' /etc/sysconfig/network-scripts/if ...

  10. Spring Boot中使用Servlet与Filter

    在Spring Boot中使用Servlet,根据Servlet注册方式的不同,有两种使用方式.若使用的是Servlet3.0+版本,则两种方式均可使用:若使用的是Servlet2.5版本,则只能使用 ...