y*n
3 楼
Is this a DP problem, I tried a recursive solution which exceeds time limit
bool wordBreak(string s, unordered_set &dict) {
if(s.length()==0) return true;
bool foundBreak=false;
for(const string& current: dict){
if(s.compare(0, current.size(),current)==0){//find current
foundBreak= wordBreak(s.substr(current.size()), dict);
if(foundBreak)
break;
}
}
return foundBreak;
}
bool wordBreak(string s, unordered_set
if(s.length()==0) return true;
bool foundBreak=false;
for(const string& current: dict){
if(s.compare(0, current.size(),current)==0){//find current
foundBreak= wordBreak(s.substr(current.size()), dict);
if(foundBreak)
break;
}
}
return foundBreak;
}
l*o
4 楼
public class Solution {
public boolean wordBreak(String s, Set dict) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
boolean[] f = new boolean[s.length() + 1];
f[0] = true;
for (int i = 0; i <= s.length(); i++)
for (int j = 0; j < i; j++)
if (f[j] && dict.contains(s.substring(j, i))) {
f[i] = true;
break;
}
return f[s.length()];
}
}
public boolean wordBreak(String s, Set
// Note: The Solution object is instantiated only once and is reused
by each test case.
boolean[] f = new boolean[s.length() + 1];
f[0] = true;
for (int i = 0; i <= s.length(); i++)
for (int j = 0; j < i; j++)
if (f[j] && dict.contains(s.substring(j, i))) {
f[i] = true;
break;
}
return f[s.length()];
}
}
y*n
5 楼
Nice.
T*e
6 楼
哈,咱们思路差不多。
bool wordBreak(string s, unordered_set &dict) { //Time: O(n^2)
int sSize=s.size();
if(sSize<=0||dict.empty()) return false;
vector isWord(sSize+1, false);
isWord[0]=true;
for(int i=1; i<=sSize; ++i) {
for(int j=i-1; j>=0; --j) {
if(dict.find(s.substr(j, i-j))!=dict.end() &&isWord[j]) {
isWord[i]=true;
break;
}
}
}
return isWord[sSize];
}
reused
【在 l****o 的大作中提到】
: public class Solution {
: public boolean wordBreak(String s, Set dict) {
: // Note: The Solution object is instantiated only once and is reused
: by each test case.
: boolean[] f = new boolean[s.length() + 1];
: f[0] = true;
: for (int i = 0; i <= s.length(); i++)
: for (int j = 0; j < i; j++)
: if (f[j] && dict.contains(s.substring(j, i))) {
: f[i] = true;
bool wordBreak(string s, unordered_set
int sSize=s.size();
if(sSize<=0||dict.empty()) return false;
vector
isWord[0]=true;
for(int i=1; i<=sSize; ++i) {
for(int j=i-1; j>=0; --j) {
if(dict.find(s.substr(j, i-j))!=dict.end() &&isWord[j]) {
isWord[i]=true;
break;
}
}
}
return isWord[sSize];
}
reused
【在 l****o 的大作中提到】
: public class Solution {
: public boolean wordBreak(String s, Set
: // Note: The Solution object is instantiated only once and is reused
: by each test case.
: boolean[] f = new boolean[s.length() + 1];
: f[0] = true;
: for (int i = 0; i <= s.length(); i++)
: for (int j = 0; j < i; j++)
: if (f[j] && dict.contains(s.substring(j, i))) {
: f[i] = true;
b*m
7 楼
其实是前几天有人问那个(10)的简化版:
public class Solution {
public boolean wordBreak(String s, Set dict) {
if(s.length()==0){
return dict.contains(s);
}
boolean[] reachable = new boolean[s.length()+1];
reachable[0] = true;
for(int i=0;i if(!reachable[i]){
continue;
}
for(int j=i+1;j<=s.length();j++){
String e = s.substring(i,j);
if(dict.contains(e)){
reachable[j] = true;
}
}
}
return reachable[reachable.length-1];
}
}
public class Solution {
public boolean wordBreak(String s, Set
if(s.length()==0){
return dict.contains(s);
}
boolean[] reachable = new boolean[s.length()+1];
reachable[0] = true;
for(int i=0;i
continue;
}
for(int j=i+1;j<=s.length();j++){
String e = s.substring(i,j);
if(dict.contains(e)){
reachable[j] = true;
}
}
}
return reachable[reachable.length-1];
}
}
z*8
8 楼
这个case老是fail在这个test case:
Submission Result: Runtime Error
Last executed input: "aaaaaaa", ["aaaa","aa"]
谁能帮我看看?
public class Solution {
public boolean wordBreak(String s, Set dict) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
boolean[] result = new boolean[s.length()+1];
result[0] = true;
int size = s.length();
for(int i=1; i <= size; ++i)
{
if(result[i] == false && dict.contains(s.substring(0,i)))
{
result[i] = true;
}
if(result[i])
{
if(i==size)
{
return true;
}
for(int j= i+1; j <= size; ++j)
{
if(result[j] == false && dict.contains(s.substring(i,j-i
)))
{
result[j] = true;
}
if(result[j] && j== size)
{
return true;
}
}
}
}
return false;
}
}
Submission Result: Runtime Error
Last executed input: "aaaaaaa", ["aaaa","aa"]
谁能帮我看看?
public class Solution {
public boolean wordBreak(String s, Set
// Note: The Solution object is instantiated only once and is reused
by each test case.
boolean[] result = new boolean[s.length()+1];
result[0] = true;
int size = s.length();
for(int i=1; i <= size; ++i)
{
if(result[i] == false && dict.contains(s.substring(0,i)))
{
result[i] = true;
}
if(result[i])
{
if(i==size)
{
return true;
}
for(int j= i+1; j <= size; ++j)
{
if(result[j] == false && dict.contains(s.substring(i,j-i
)))
{
result[j] = true;
}
if(result[j] && j== size)
{
return true;
}
}
}
}
return false;
}
}
相关阅读
可不可以在follow up中说,如果你要是不想要我就告诉我一声是新H1B还是H1B transfer大家除了MONSTER还在哪里投简历呀?OPT case NSC: approved within 1 month申请状态 hold是什么意思?Write an iterative method that finds depth of a (non-balanced) binary tree.唉, 接到了拒信,挺伤心One onsite inverview question求助: 关于SSN (F1-OPT)请教,生产antibody的小公司能去么?问个问题binary search 的变体请问公司转部门, h1b amendment一般要多少时间?要电面了,也来求祝福C# developer needed in Silver Spring MD, no sponsership[合集] 微软面试题一道怎么才能知道这个职位是要求本科学历以上的?兼职启示H1B transfer 求解释F2身份做实习?大牛给讲讲abstract factory的妙处?