From e08428524dc326e2616125d80a1481e31228ed77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Mon, 11 Apr 2022 17:12:56 +0800 Subject: [PATCH] =?UTF-8?q?293:=E7=BF=BB=E8=BD=AC=E6=B8=B8=E6=88=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/leetcode/editor/cn/FlipGame.java | 61 +++++++++++++++++++ .../editor/cn/doc/content/FlipGame.md | 31 ++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/FlipGame.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/FlipGame.md diff --git a/src/main/java/leetcode/editor/cn/FlipGame.java b/src/main/java/leetcode/editor/cn/FlipGame.java new file mode 100644 index 0000000..7f70f65 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/FlipGame.java @@ -0,0 +1,61 @@ +//你和朋友玩一个叫做「翻转游戏」的游戏。游戏规则如下: +// +// 给你一个字符串 currentState ,其中只含 '+' 和 '-' 。你和朋友轮流将 连续 的两个 "++" 反转成 "--" 。当一方无法进行有效 +//的翻转时便意味着游戏结束,则另一方获胜。 +// +// 计算并返回 一次有效操作 后,字符串 currentState 所有的可能状态,返回结果可以按 任意顺序 排列。如果不存在可能的有效操作,请返回一个空列表 +// [] 。 +// +// +// +// 示例 1: +// +// +//输入:currentState = "++++" +//输出:["--++","+--+","++--"] +// +// +// 示例 2: +// +// +//输入:currentState = "+" +//输出:[] +// +// +// +// +// 提示: +// +// +// 1 <= currentState.length <= 500 +// currentState[i] 不是 '+' 就是 '-' +// +// Related Topics 字符串 👍 33 👎 0 + +package leetcode.editor.cn; + +import java.util.ArrayList; +import java.util.List; + +//293:翻转游戏 +public class FlipGame { + public static void main(String[] args) { + Solution solution = new FlipGame().new Solution(); + // TO TEST + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public List generatePossibleNextMoves(String currentState) { + List list = new ArrayList<>(); + for (int i = 1; i < currentState.length(); i++) { + if(currentState.charAt(i-1)=='+'&¤tState.charAt(i)=='+'){ + list.add(currentState.substring(0,i-1)+"--"+currentState.substring(i+1)); + } + } + return list; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/main/java/leetcode/editor/cn/doc/content/FlipGame.md b/src/main/java/leetcode/editor/cn/doc/content/FlipGame.md new file mode 100644 index 0000000..e5a2ec4 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/FlipGame.md @@ -0,0 +1,31 @@ +

你和朋友玩一个叫做「翻转游戏」的游戏。游戏规则如下:

+ +

给你一个字符串 currentState ,其中只含 '+''-' 。你和朋友轮流将 连续 的两个 "++" 反转成 "--" 。当一方无法进行有效的翻转时便意味着游戏结束,则另一方获胜。

+ +

计算并返回 一次有效操作 后,字符串 currentState 所有的可能状态,返回结果可以按 任意顺序 排列。如果不存在可能的有效操作,请返回一个空列表 []

+ +

 

+ +

示例 1:

+ +
+输入:currentState = "++++"
+输出:["--++","+--+","++--"]
+
+ +

示例 2:

+ +
+输入:currentState = "+"
+输出:[]
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= currentState.length <= 500
  • +
  • currentState[i] 不是 '+' 就是 '-'
  • +
+
Related Topics
  • 字符串

  • 👍 33
  • 👎 0
  • \ No newline at end of file