javascript动态规划之word break
在本地是对的,但在leetcode上总是出错,有同样在刷题的亲故们能帮忙看看吗?此题属于leetcode上139题
/** * @param {string} s * @param {string[]} wordDict * @return {boolean} */ var dp = {}; var wordBreak = function(s, wordDict) { wordBreakSub(s, wordDict); return dp[s]; }; function wordBreakSub(str, wordDict){ if(dp[str]!==undefined){//如果已经存入 return dp[str]; } if(inDict(str, wordDict)){//如果这个字符串本身就在字典里 return dp[str] = true; } for(let i=1; i<str.length; i++){ let left = str.substring(0,i); let right = str.substring(i); if(wordBreakSub(left, wordDict)&&inDict(right, wordDict)){ return dp[str] = true; } } return dp[str] = false; } function inDict(str, wordDict){ if(wordDict.includes(str)){ return true; }else{ return false; } }