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 player can guarantee a win!!!!
      * 不是能不能赢,是能不能保证必须赢!!!!这就是我刚开始搞错的地方
      * 这里和其他传统的backtracking一点点不一样,没有刚开始的条件判断,因为只要走过一遍没发的都return false
      */

     public boolean canWin(String s) {
            int n = s.length();
            if(n<=1) return false;
            return dfs(s);
        }
        private boolean dfs(String s){
            StringBuffer buffer = new StringBuffer(s);
            for(int i=0;i<s.length()-1;i++){
                if(s.charAt(i)==s.charAt(i+1)&&s.charAt(i+1)==‘+‘){
                    buffer.setCharAt(i,‘-‘);
                    buffer.setCharAt(i+1,‘-‘);
                    if(!dfs(buffer.toString()))
                        return true;
                    buffer.setCharAt(i,‘+‘);
                    buffer.setCharAt(i+1,‘+‘);
                }
            }
            return false;
        }
时间: 2024-10-13 00:10:58

294. Flip Game II的相关文章

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

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