单词拆分 I · Word Break
[抄题]:
给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词。
s = "lintcode"
dict = ["lint","code"]
返回 true 因为"lintcode"可以被空格切分成"lint code"
[思维问题]:
看到字符串就怕:还是要掌握一般规律
[一句话思路]:
还是看rU手册456页的解法吧
前部完美切分+后部是单词
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

"aaaaaaa" ["aaaa","aa"] false。f[2] = f["li"] f[0] f[全部]都是true
lin完美切分+t在词典中
[一刷]:
- 布尔型can数组的长度是字符串长度+1,不是词典数+1
[二刷]:
- i <= s.length() 表示最后一位边界也要处理
- 取子串的方法是.substring()
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
为了取出长度为j的单词,起点其实是i - j

[复杂度]:Time complexity: O(n*l^2+m) Space complexity: O(n^2)
查询字符串是否在词典中也是n
N字符串长度
M单词个数
L单词平均长度
[英文数据结构或算法,为什么不用别的数据结构或算法]:
从右边开始切割,待判断单词由短到长,割到最大单词长度L还没有即可终止。
从左边开始切割,待判断单词由长到短,复杂度太高。

hash map查询单词,长度L不可忽略时,复杂度是L, 可忽略时为1
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
word break2
[代码风格] :

public class Solution {
/*
* @param s: A string
* @param dict: A dictionary of words dict
* @return: A boolean
*/
//maxLength
private int maxLength (Set<String> dict) {
int max = 0;
for (String word : dict) {
max = Math.max(max, word.length());
}
return max;
}
public boolean wordBreak(String s, Set<String> dict) {
//corner case
if (s == null || dict == null) {
return false;
}
//state
//boolean can[] = new boolean[dict.size() + 1];
boolean can[] = new boolean[s.length() + 1];
int max_len = maxLength (dict);
//initialization
can[0] = true;
//function
for (int i = 1; i <= s.length(); i++) {
can[i] = false;
for (int j = 0; j <= i && j <= max_len; j++) {
if (can[i - j] == false) {
continue;
}
String word = s.substring(i - j, i);
if (dict.contains(word)) {
can[i] = true;
break;
}
}
}
//answer
return can[s.length()];
}
}
单词拆分 I · Word Break的更多相关文章
- leetcode 140 单词拆分2 word break II
单词拆分2,递归+dp, 需要使用递归,同时使用记忆化搜索保存下来结果,c++代码如下 class Solution { public: //定义一个子串和子串拆分(如果有的话)的映射 unorder ...
- leetcode 139 单词拆分(word break)
一开始的错误答案与错误思路,幻想直接遍历得出答案: class Solution { public: bool wordBreak(string s, vector<string>& ...
- [Swift]LeetCode140. 单词拆分 II | Word Break II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- LeetCode 139. 单词拆分(Word Break)
139. 单词拆分 139. Word Break
- Word Break II 求把字符串拆分为字典里的单词的全部方案 @LeetCode
这道题相似 Word Break 推断能否把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不不过能否拆分,而是要求出全部的拆分方案. 因此用递归. 可是直接递归做会超时,原因是Le ...
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 140. Word Break II 单词拆分II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
随机推荐
- pig概述
pig概述pig是一个用于并行计算的高级数据流语言和执行框架:类sql.文件处理框架:有一套和sql类似的执行语句,处理的对象是HDFS上文件.Pig的数据处理语言是数据流方式的,一步一步的进行处理: ...
- win 关闭正在使用的端口
1.Windows平台 在windows命令行窗口下执行: 1.查看所有的端口占用情况 C:\>netstat -ano 2.查看指定端口的占用情况 C:\>netstat -aon|fi ...
- windows 和 Linux 安装rabbitmq
windows 安装 rabbitmq 1,安装erlang 点击进入官网下载:http://erlang.org/download/ 2.安装rabbitmq 点击进入官网下载:http://www ...
- File处理
package com.cfets.ts.u.shchgateway.util; import java.io.BufferedInputStream; import java.io.Buffered ...
- python气象分析
数据分析实例 -- 气象数据 一.实验介绍 本实验将对意大利北部沿海地区的气象数据进行分析与可视化.我们在实验过程中先会运用 Python 中matplotlib库的对数据进行图表化处理,然后调用 s ...
- python入门-测试代码
断言 测试函数 def get_formatted_name(first,last): """generate a neatly formattef full name& ...
- HTML5 Canvas ( 线段的绘制 ) beginPath, moveTo, strokeStyle, stroke
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- leetcode167
public class Solution { public int[] TwoSum(int[] numbers, int target) { Dictionary<int, int> ...
- 使用MATPLOTLIB 制图(散点图,热力图)
import numpy as np import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv('D:\\myfil ...
- Activity服务类-4 HistoryService服务类
一共个方法15个方法 用于查询历史工作流信息1.创建查询(7个方法)//创建一个新的编程查询来搜索{@link HistoricProcessInstance}.HistoricProcessInst ...