leetCode_Word Break
来源:互联网 发布:淘宝app的开发 编辑:程序博客网 时间:2024/06/08 13:44
题意:给定一个单词和一个单词序列,看看这个单词能不能分割成一系列单词,并且分割后的单词都在单词序列当中
解法:很简单的dp,用s[0...word.length]表示到第几位能否分割。若word.substring(0,i+1)在这个单词里或者s[j]=true并且word.substring(j+1,i+1)在这个单词序列里,则s[i]=true。最后返回的值为s[word.length-1]
代码如下:
public class Solution {public boolean wordBreak(String s, Set<String> wordDict) {boolean[] has = new boolean[s.length()];int i, j, k;for (i = 0; i < s.length(); i++) {if (wordDict.contains(s.substring(0, i + 1)))has[i] = true;for (j = 0; j < i; j++) {if (has[j] == true && wordDict.contains(s.substring(j + 1, i + 1)))has[i] = true;}}return has[s.length() - 1];}}
0 0
- LeetCode_Word Break
- leetcode_Word Break
- leetCode_Word Break
- Leetcode_word-break-ii
- LeetCode_Word Break II
- Leetcode_word-break(c++ and python version)
- LeetCode_Word Ladder
- LeetCode_Word Ladder
- LeetCode_Word Search
- leetcode_Word Search
- leetcode_Word Pattern
- leetcode_Word Pattern
- leetCode_Word BreakII
- Leetcode_word-search(c++ version)
- Leetcode_word-ladder(c++ version)
- Leetcode_word-ladder-ii
- 【题解】leetcode_Word Frequency
- leetCode_Word Ladder【BFS】
- make smdk2410_config流程分析
- 乐乐一下
- 文章标题
- Get Start With HTML
- Struts2基础总结-入门篇
- leetCode_Word Break
- 1201 - 数据存储 - 前言
- n元一次不定方程 模板
- hdu 1002 大数相加
- 【一步一步学习VBA】将Excel的每行数据导出为txt文本
- 剑指Offer_16_合并两个排序的链表
- java连接数据库,进行增删改查
- List Set Map以及子接口用法总结
- cmd 命令行