[leetcode]139. Word Break单词能否拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Note:
- The same word in the dictionary may be reused multiple times in the segmentation.
- You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".
题意:
给定字符串S和字典wordDict, 判断字符串S是否能由字典中的单词组合而成
思路:
用一维boolean数组记录是否能字符串S是否能被分隔
l e e t c o d e
T | F | F | F | F | F | F | F | F |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
dp[0]初始化为true, 其他为default的false
dp[s.length() + 1]
i = 1 j = 1, dp[1] 为default F, 没必要再去验证wordDict.contains(s.substring(j,i))
j = 0, dp[0] 为 T , 验证wordDict.contains(s.substring(0,1)) 为F
i = 2 j = 2, dp[2] 为default F
j = 1, dp[1] 为default F
j = 0, dp[0] 为 T , 验证wordDict.contains(s.substring(0,2)) 为F
i = 3 j = 3, dp[3] 为default F
j = 2, dp[2] 为default F
j = 1, dp[1] 为default F
j = 0, dp[0] 为 T , 验证wordDict.contains(s.substring(0,3)) 为F
i = 4 j = 4, dp[4]为default F
j = 3, dp[3] 为default F
j = 2, dp[2] 为default F
j = 1, dp[1] 为default F
j = 0, dp[0] 为 T , 验证wordDict.contains(s.substring(0,4)) 为T ----> 更新dp[4] = true
T | F | F | F | T | F | F | F | F |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
代码:
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
// corner case
if (s == null || s.length() < 1 || wordDict == null || wordDict.size() < 1) {
return false;
}
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
//外层循环scan字符串S
for(int i = 1; i <= s.length(); i++ ){
//内层循环找分割点
for(int j = i; j >= 0; j--){
if(dp[j] && wordDict.contains(s.substring(j,i))){
dp[i] = true;
}
}
}
return dp[s.length()];
}
}
[leetcode]139. Word Break单词能否拆分的更多相关文章
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- [LeetCode] 139. Word Break 拆分词句
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- 139 Word Break 单词拆分
给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...
- leetcode 139. Word Break ----- java
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- LeetCode #139. Word Break C#
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [LeetCode] 139 Word Break(BFS统计层数的方法)
原题地址: https://leetcode.com/problems/word-break/description/ 题目: Given a non-empty string s and a dic ...
随机推荐
- uploadify是通过flash上传,服务器获取type为application/octet-stream
uploadify是通过flash上传,服务器获取type为application/octet-stream,因此允许上传的类型要加上application/octet-stream
- Windows下MySQL免安装版的安装、卸载
一.安装 1.下载 到MySQL官网http://dev.mysql.com/downloads/mysql/ 下载mysql-5.6.15-win32.zip. 2.拷贝 将mysql-5.6.15 ...
- numpy的shuffle函数
import numpy as np from numpy.random import shuffle import pandas as pd df = pd.DataFrame([[1,2,3],[ ...
- RmNet,CDC-ECM ,NDIS,RNDIS区别
RmNet和CDC-ECM区别:更像是两种拨号方式的区别,RmNet获取公网IP,ECD-ECM获取局域网IP. 在高通平台上,rmnet driver 和标准的CDC-ECM是有区别的,rmnet ...
- spark 应用程序部署工具 spark-submit
打包 Spark application 使用spark-submit启动Spark application spark-submit usage spark-submit option 运行模式相关 ...
- 网络命令ping/netstat/ipconfig/arp/tracert/nbstat
1.1 Ping命令的使用 ping检测网络故障步骤: ping 127.0.0.1 ping环绕指针检测是否在计算上anzhaung了TCP/IP协议及配置是否正确 ping本机IP这个命令被被送到 ...
- OpenCL 双调排序 CPU 版
▶ 学习了双调排序,参考(https://blog.csdn.net/xbinworld/article/details/76408595) ● 使用 CPU 排序的代码 #include <s ...
- 0_Simple__simpleTemplates + 0_Simple__simpleTemplates_nvrtc
使用 C++ 的模板 ▶ 源代码:静态使用 // sharedmem.cuh #ifndef _SHAREDMEM_H_ #define _SHAREDMEM_H_ // SharedMemory 的 ...
- Redis 通用操作2
01, 一次设置多个键值 => mset key1 value1 key2 value2 key3 value3 ...... 02, 一次获取多个值 => mget ke1 key2 k ...
- webstorm使用
1. 打开toolbar 2. 安装vue.js 3. 设置new vue file模板 <template> <div> <header-component/> ...