成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

Flip Game II

nanfeiyan / 3178人閱讀

摘要:題目鏈接的題,可以直接暴力,優(yōu)化可以加,只有當(dāng)所有可能的都是的時(shí)候,當(dāng)前結(jié)果才是

Flip Game II

題目鏈接:https://leetcode.com/problems...

dp的題,可以直接暴力dfs,優(yōu)化可以加memory,只有當(dāng)所有可能的subproblem都是false的時(shí)候,當(dāng)前結(jié)果才是false:

public class Solution {
    public boolean canWin(String s) {
        // if already in map
        if(memo.containsKey(s)) return memo.get(s);
        for(int i = 0; i < s.length() - 1; i++) {
            if(s.startsWith("++", i)) {
                String next = s.substring(0, i) + "--" + s.substring(i + 2);
                if(!canWin(next)) {
                    memo.put(s, true);
                    return true;
                }
            }
        }
        memo.put(s, false);
        return false;
    }
    
    Map memo = new HashMap();
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/66631.html

相關(guān)文章

  • LeetCode[45] Jump Game II

    摘要:復(fù)雜度思路每次設(shè)置一個(gè)窗口,觀察在這一步下能到達(dá)的最遠(yuǎn)距離,不斷的移動(dòng)這個(gè)窗口。計(jì)數(shù),需要移動(dòng)幾次,才能覆蓋到末尾的值。 LeetCode[45] Jump Game II Given an array of non-negative integers, you are initially positioned at the first index of the array. Eac...

    keelii 評(píng)論0 收藏0
  • [LintCode/LeetCode] Jump Game I & II

    摘要:建立動(dòng)規(guī)數(shù)組,表示從起點(diǎn)處到達(dá)該點(diǎn)的可能性。循環(huán)結(jié)束后,數(shù)組對(duì)所有點(diǎn)作為終點(diǎn)的可能性都進(jìn)行了賦值。和的不同在于找到最少的步數(shù)。此時(shí)的一定是滿足條件的最小的,所以一定是最優(yōu)解。 Jump Game Problem Given an array of non-negative integers, you are initially positioned at the first index...

    rose 評(píng)論0 收藏0
  • leetcode45. Jump Game II

    摘要:轉(zhuǎn)換為廣度優(yōu)先算法即為我們只需要找到每一步的開(kāi)始節(jié)點(diǎn)和結(jié)束下標(biāo),找到下一輪遍歷的最大下標(biāo),如果該下標(biāo)超過(guò)了數(shù)組長(zhǎng)度,那么結(jié)束遍歷,返回步數(shù),否則將上一輪的最終節(jié)點(diǎn)加一作為起始節(jié)點(diǎn),并將下一輪最大下標(biāo)最為結(jié)束節(jié)點(diǎn),繼續(xù)遍歷。 題目要求 Given an array of non-negative integers, you are initially positioned at the ...

    shiguibiao 評(píng)論0 收藏0
  • [LeetCode] 487. Max Consecutive Ones II

    Problem Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at most one 0. Example 1:Input: [1,0,1,1,0]Output: 4Explanation: Flip the first zero will get the ...

    nanfeiyan 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<