摘要:題目鏈接的題,可以直接暴力,優(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; } Mapmemo = new HashMap(); }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/66631.html
摘要:復(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...
摘要:建立動(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...
摘要:轉(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 ...
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 ...
閱讀 1636·2021-11-17 09:33
閱讀 1330·2021-10-11 10:59
閱讀 2973·2021-09-30 09:48
閱讀 1977·2021-09-30 09:47
閱讀 3099·2019-08-30 15:55
閱讀 2402·2019-08-30 15:54
閱讀 1551·2019-08-29 15:25
閱讀 1712·2019-08-29 10:57