题解如下:

/**
* 动态规划解法:
* dp[i] 表示 0-i的最小不能被识别的字母个数
* 求 dp[k] 如果第K个字母 不能和前面的字母[0-{k-1}]合在一起被识别 那么dp[k] = dp[k-1]+1
* 如果可以别识别 dp[k] = min(dp[k],dp[j-1])
*
* 能不能被识别的判断又有几种解法:
* 1. 使用HashMap -- 本题解使用HashMap
* 2. 使用字典树
* 3. RK算法
* 参考链接:https://leetcode-cn.com/problems/re-space-lcci/solution/hui-fu-kong-ge-by-leetcode-solution/
* @param dictionary
* @param sentence
* @return
*/
public int respace3(String[] dictionary, String sentence) { HashMap<String, Integer> map = new HashMap<>();
for (String s : dictionary) {
map.put(s,1);
}
int length = sentence.length();
int[] dp = new int[length+1];
// 由于边界的处理我们让dp[i+1] 表示0-i的最小不能被识别的字母个数
for (int i = 0; i < length; i++) {
dp[i+1] = dp[i]+1;
for (int j = 0; j <= i; j++) {
if (map.getOrDefault(sentence.substring(j,i+1),0) == 1) {
dp[i+1] = Math.min(dp[i+1],dp[j]);
}
}
}
return dp[length]; }

Leetcode----<Re-Space LCCI>的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. 深入理解Kafka核心设计及原理(五):消息存储

    转载请注明出处:https://www.cnblogs.com/zjdxr-up/p/16127749.html 目录: 5.1文件目录布局 5.2消息压缩 5.3日志索引 5.4日志文件及索引文件分 ...

  2. Java语言学习day29--8月04日

    今日内容介绍1.Object2.String3.StringBuilder ###01API概念 * A:API(Application Programming Interface) * 应用程序编程 ...

  3. ArcGIS使用技巧(四)——山体阴影

    新手,若有错误还请指正! 最近在制图的时候出现如下的情况(图1),怎么调整Display的三个参数都没用. 图 1 查看其信息,发现dem的像元大小为0.00027(图2),是未投影的 图 2 查看A ...

  4. python基础练习题(题目 有序列表插入元素)

    day26 --------------------------------------------------------------- 实例039:有序列表插入元素 题目 有一个已经排好序的数组. ...

  5. mmdetection获取最高map的epoch

    自动从训练结果中获取最高的mAP所对应的epoch. <code>import json import os ''' :param work_dir 训练结果目录 :return 最好的m ...

  6. 接口测试框架实战(一) | Requests 与接口请求构造

    1080×388 33.4 KB Requests 是一个优雅而简单的 Python HTTP 库,其实 Python 内置了用于访问网络的资源模块,比如urllib,但是它远不如 Requests ...

  7. .net core JWT验证,HttpContext.User为空的问题

    这几天在学习张老师.net core教程JWT部分,链接 https://mp.weixin.qq.com/s/7135y3MkUlPIp-flfwscig 教程使用的.net core 2.2, 在 ...

  8. content应用

  9. AspNetCore7.0源码解读之UseMiddleware

    Use​Middleware​Extensions 前言 本文编写时源码参考github仓库主分支. aspnetcore提供了Use方法供开发者自定义中间件,该方法接收一个委托对象,该委托接收一个R ...

  10. Java操作Hadoop、Map、Reduce合成

    原始数据: Map阶段 1.每次读一行数据, 2.拆分每行数据, 3.每个单词碰到一次写个1 <0, "hello tom"> <10, "hello ...