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 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.

 1 class Solution {
 2 public:
 3     bool canWin(string s) {
 4         if (s.size() < 2) return false;
 5
 6         for (int i = 1; i < s.size(); i++) {
 7             if (s[i] == ‘+‘ && s[i - 1] == ‘+‘) {
 8                 s[i] = s[i - 1] = ‘-‘;
 9                 if (!canWin(s)) return true;
10                 s[i] = s[i - 1] = ‘+‘;
11             }
12         }
13         return false;
14     }
15 };
时间: 2024-12-28 11:08:59

Flip Game II的相关文章

294. Flip Game II

/* * 294. Flip Game II * 2016-7-2 by Mingyang * 这个题目我开始做的时候,还加了turn=1.-1来表示是哪个人走,还用了一个函数来表示什么时候不能走 * 其实都不用,这里如果没有可以走的了dfs的末尾会自动return false,另外,本轮是我走,我只要保证 * 下一轮return false就行了,我就return true,就是我走了以后,你下一轮无论怎么走,都不行 * 再次注意:题目的要求是determine if the starting

LeetCode Flip Game II

原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 类似Flip Game. 若是有一段"++", 剩下的段和"--"组合 can not win, 那么返回true. 从头到尾试遍了没找到这么一段"++", 返回false. Time Complexity: exponential. AC Java: 1 public class Solution { 2 public boolean can

Flip Game II -- LeetCode

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

第四篇 Flip Game II

1 public class Solution { 2 public boolean canWin(String s) { 3 if (s == null || s.length() <= 1) { 4 return false; 5 } 6 ArrayList<String> validMoves = getValidMoves(s); 7 if (validMoves.isEmpty()) { 8 return false; 9 } 10 for (String str : vali

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482