Leetcode----<Re-Space LCCI>

题解如下:
/**
* 动态规划解法:
* 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>的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- vue 点击事件唤醒QQ
window.location.href = 'http://wpa.qq.com/msgrd?v=3&uin=QQ号' window.location.href = 'http://wpa. ...
- AcWing 【算法提高课】笔记02——搜索
搜索进阶 22.4.14 (PS:还有 字串变换 A*两题 生日蛋糕 回转游戏 没做) 感觉暂时用不上 BFS 1. Flood Fill 在线性时间复杂度内,找到某个点所在的连通块 思路 统计连通块 ...
- 原生 js 重点案例 [tab栏切换]
代码示例 : <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- Java指令重排序在多线程环境下的应对策略
一.序言 指令重排在单线程环境下有利于提高程序的执行效率,不会对程序产生负面影响:在多线程环境下,指令重排会给程序带来意想不到的错误. 本文对多线程指令重排问题进行复原,并针对指令重排给出相应的解决方 ...
- 一篇讲清楚String、StringBuffer和StringBuild
一.String篇 1.String基本介绍? (jdk文档原文)String类代表字符串. Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例. 说人 ...
- 这样理解 HTTP,面试再也不用慌了~
开源Linux 长按二维码加关注~ 上一篇:SSH只能用于远程Linux主机? 1 HTTP HTTP 协议是个无状态协议,不会保存状态. 2 Post 和 Get 的区别 先引入副作用和幂等的概念. ...
- Flutter和iOS混编详解
前言 下面的内容是最近在使用Flutter和我们自己项目进行混编时候的一些总结以及自己踩的一些坑,处理完了就顺便把整个过程以及一些我们可能需要注意的点全都梳理出来,希望对有需要的小伙伴有点帮助,也方便 ...
- 中国电子云数据库 Mesh 项目 DBPack 的实践
作者:刘晓敏 2022 年 4 月,中国电子云开源了其云原生数据库 Mesh 项目 DBPack.该项目的诞生,旨在解决用户上云过程中面临的一些技术难点,诸如分布式事务.分库分表等.由于它数据库 Me ...
- Git命令行提交代码步骤
先进入对应的项目目录 1.拉取服务器代码,避免覆盖他人代码 git pull 2.查看当前项目中有哪些文件被修改过 git status 具体状态如下: 1:Untracked: 未跟踪,一般为新增文 ...
- Jdbc从入门到入土
二刷jdbc 作者小结:从第一次大概几天快速刷完jdbc,到如今的二刷,才发现自己对jdbc的理解有点太浅.到学习javaweb是创建数据库层时的迷茫,到现在对这种设计模式的理解.我深有体会到了:实打 ...