第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

【LEETCODE】模擬面試-294.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 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 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.

input: a string with only + and - two kinds of characters
output: for a given input, based on the game rule, return whether there is one strategy that can make the first player win, if yes, return true, if there is none, return false
corner: when the string if null, return false

We will first change the string to a character array.

And apply Backtracking algorithm to solve it.

The starting point of the first player will iterate from 0 to arr.length - 2, when he finds two consecutive ++, he will soon change it into --.

Then the second player will do the same thing, so we pass current changed array to the second player, and he will receive a result: boolean otherWin = helper(arr);

If the second player will win, means he will return a true to upper level, the first player should recover his current strategy by change the -- to original ++, and keep moving i to next step, so that he can find other strategy that will make himself finally win the game, as long as there is just one strategy make it happen, the final result will be true, otherwise, it will be false.

public class Solution{    public boolean canWin(String s){        //corner
        if ( s == null || s.length() == 0 ){            return false;
        }        
        return helper(s.toCharArray());
    }    
    public boolean helper(char[] arr){        for ( int i = 0; i < arr.length; i++ ){            if ( arr[i] == '+' && arr[i + 1] == '+' ){
                arr[i] = '-';
                arr[i + 1] = '-';                
                boolean otherWin = helper(arr);         //2人轮流
                
                arr[i] = '+';               //返回到upper level后恢复+号
                arr[i + 1] = '+';                
                if ( !otherWin ){           //另一人false时走这一步,另一人true时,要继续遍历++对
                    return true;            //直到找到某一种走法可以让另一人false,最终整体返回的值为true,此时第一人赢
                }
            }
        }        
        return false;           //如果遍历到头没有++对了,而且几种走法都一直找不到赢的走法了,第一人就最终false
    }
}


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消