动态规划——word-break&&word-breakii

来源:互联网 发布:中央财经大学网络教育 编辑:程序博客网 时间:2024/05/19 11:47

题目描述

Given a string s and a dictionary of wordsdict, determine ifs can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s ="leetcode",
dict =["leet", "code"].

Return true because"leetcode"can be segmented as"leet code".

思路:dp[i]  表示源串的前i个字符可以满足分割,那么 dp[ j ] 满足分割的条件是存在i(i<j) 使得 dp [i] && substring(i,j)在字典里

(dp[i]和substring(i,j)中的i的意义不同,dp[i]表示s的前i个字符在字典里,substring的i表示字符串中的下标i指向第i+1个字符)。

这里需要注意:dp[i]的下标i表示s的第i个字符,而s的第i个字符在s的下标为i-1!!!


import java.util.*;public class Solution {    public boolean wordBreak(String s, Set<String> dict) {        if(s == null||dict == null)            return false;        //dp[i]存放s前i个字符是否满足条件;        boolean[] dp=new boolean[s.length()+1];        dp[0]=true;        for(int i=1;i<=s.length();i++)            {            if(dp[i-1] == true)//前i-1个字符返回true;                {                  //s中第i个字符下标为i-1;s.substring(i-1,j);                  for(int j=i;j<=s.length();j++)                      {                      if(dict.contains(s.substring(i-1,j)))                          {                          dp[j]=true;                          if(j == s.length())                              {                              return true;                          }                      }                  }                }            }        return dp[s.length()];     }}

or:

import java.util.*;public class Solution {    public boolean wordBreak(String s, Set<String> dict) {        if(s == null||s.length() == 0||dict == null||dict.size() == 0){            return false;        }        boolean[] dp=new boolean[s.length()+1];        //dp[i]表示s长度为i时是否在dict中        dp[0]=true;        for(int i=1;i<=s.length();i++){            for(int j=0;j<i;j++){                if(dp[j] == true&&dict.contains(s.substring(j,i))){                    dp[i]=true;                }            }        }        return dp[s.length()];    }}



题目描述:word-breakii

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given
s ="catsanddog",
dict =["cat", "cats", "and", "sand", "dog"].

A solution is["cats and dog", "cat sand dog"].

标准的DFS套路题。

话不投机半句多!

import java.util.*;public class Solution {    public ArrayList<String> wordBreak(String s, Set<String> dict) {         ArrayList<String> array=new  ArrayList<String>();        if(s == null||dict == null||s.length() == 0||dict.size() == 0)            return array;        dfs(s,s.length(),"",array,dict);        return array;    }    public void dfs(String s,int size,String result,ArrayList<String> array,Set<String> dict)        {        if(size == 0)            {            result=result.trim();            array.add(result);            return;        }        for(int i=size-1;i>=0;i--)            {            if(dict.contains(s.substring(i,size)))                {                dfs(s,i,s.substring(i,size)+" "+result,array,dict);            }        }    }}


0 0
原创粉丝点击