GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — 恢复空格-面试题 17.13 · Issue #100 · sl1673495/leetcode-javascript Open Labels Description哦,不!你不小心把一个长篇文章中的空格、标点都删掉了,并且大写也弄成了小写。像句子"I reset the computer. It still didn’t boot!"已经变成了"iresetthecomputeritstilldidntboot"。在处理标点符号和大小写之前,你得先把它断成词语。当然了,你有一本厚厚的词典 dictionary,不过,有些词没在词典里。假设文章用 sentence 表示,设计一个算法,把文章断开,要求未识别的字符最少,返回未识别的字符数。 注意:本题相对原题稍作改动,只需返回未识别的字符数 提示: 0 <= len(sentence) <= 1000 来源:力扣(LeetCode) 思路动态规划,从前向后遍历, 状态转移方程,遍历
最后只需要返回 /** * @param {string[]} dictionary * @param {string} sentence * @return {number} */ let respace = function (dictionary, sentence) { let n = sentence.length let dp = [0] for (let i = 1; i <= n; i++) { let min = dp[i - 1] + 1 for (let word of dictionary) { if (sentence.substring(i - word.length, i) === word) { min = Math.min(min, dp[i - word.length]) } } dp[i] = min } return dp[n] } ActivityMetadataMetadataAssigneesLabelsProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |