- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究这个问题。似乎我找到了正确的答案并返回 true,但随后它被 false 覆盖。Java 新手,抱歉,如果这是一个虚拟问题。我如何返回 true?预先感谢您
问题给定一个字符串 s 和一本单词字典 dict,确定 s 是否可以分割成一个或多个字典单词的空格分隔序列。
例如,给定s = "力特代码",dict = [“leet”,“代码”]。
返回 true,因为“leetcode”可以分段为“leet code”。
import java.util.HashSet;
import java.util.Set;
public class Hi {
public static void main(String[] args) {
String str = "leetcode";
Set<String> set = new HashSet<String>();
set.add("leet");
set.add("code");
boolean b = wordBreak(str, set);
System.out.println("b is " + b);
}
public static boolean wordBreak(String s, Set<String> wordDict) {
if(s.length() == 0 || wordDict.isEmpty()) {
return false;
}
return helper(s, wordDict, 0);
}
public static boolean helper(String s, Set<String> wordDict, int index) {
if(index == s.length()) {
System.out.println("1 is called.. ");
return true;
}
int curIndex = index;
System.out.println("curIndex is " + curIndex);
while(index < s.length()) {
//System.out.println("s.length() is " + s.length());
curIndex++;
if(curIndex > s.length()) {
System.out.println("2 is called.. ");
//return false;
return false;
}
if(wordDict.contains(s.substring(index, curIndex))) {
System.out.println(s.substring(index, curIndex) + " curIndex is " + curIndex);
helper(s, wordDict, curIndex);
}
}
System.out.println("3 is called.. ");
return false;
}
输出:curIndex 为 0
leet curIndex 为 4
curIndex 为 4
代码 curIndex 为 8
1 被称为..
2 被称为..
2 被称为..
b 为假
最佳答案
这可能无法回答您的问题,但我只是提到了一种方法,绝不是说我的方法更好或更优化。
在您的代码中,没有 return true
语句。该代码执行正确的工作,但在最后,由于循环不会在任何地方中断,因此它总是返回 false。我的意思是,您需要根据某些条件以及我在下面的示例中提到的此类条件之一在某处返回 true。
private static boolean test(String str, Set<String> set) {
int i = 1;
int start = 0;
List<String> tokens = new ArrayList<String>();
while (i <= str.length()) {
String substring = str.substring(start, i);
if (set.contains(substring)) {
tokens.add(substring);
start = substring.length();
}
i++;
}
String abc = "";
for (String a : tokens) {
abc = abc + a;
}
System.out.println(abc);
if (abc.equals(str)) {
return true;
} else {
return false;
}
}
下面是调试器内调试跟踪的屏幕截图。
关于java - leetcode 139. 分词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39380031/
大家好,其实我快疯了,我竭尽全力解决这个简单的问题。 如您所见,狭窄空间中的简单标签导致单个单词“Verification”被分成两行,这当然是 Not Acceptable 。 我知道我只能将行数设
我正在尝试创建类似句子的东西,其中包含随机单词。具体来说,我会有类似的东西: "The weather today is [weather_state]." 并且能够做一些事情,比如找到 [brack
我希望我的导航栏 (.top-bar) 比现在更具响应性。目前,如果屏幕缩小太多,.top-bar-right 类只会下降到 .menu 类之下。我需要 .top-bar-right 来分割自己或打破
我正在尝试编写一个函数来将命令行参数解析为一个 vector 。问题是我似乎无法消除使用全局指针数组作为 vector 。 代码是: /** parse command line arguments
我正在做一些分词实验,如下所示。 lst是一个字符序列,output是所有可能的词。 lst = ['a', 'b', 'c', 'd'] def foo(lst): ... retu
我正在尝试解决 this问题。问题如下 给定一个输入字符串和一个单词字典,看看是否可以将输入字符串分割成以空格分隔的字典单词序列。 字典是一个字符串数组。 我的方法是以下递归 fn 并存储递归调用的结
我正在研究这个问题。似乎我找到了正确的答案并返回 true,但随后它被 false 覆盖。Java 新手,抱歉,如果这是一个虚拟问题。我如何返回 true?预先感谢您 问题给定一个字符串 s 和一本单
我正在使用 word-break css 属性,但即使是一个简单的示例似乎也无法让它工作。我的代码是: react : render() { return ( A very very lo
我正在尝试更改 word-break某些内联元素的属性,例如 和 以获得更好的页面内容流。 Firefox 似乎只识别显示为 block 的元素的分词属性(例如 ),而 Chrome 尊重分词的请求
我想标记用户输入的任何字符串。我的代码是这样的: #include #include #include int main(void) { char str; char *toke
有没有办法让单词正确对齐?我尝试添加 word-break 和 word-wrap 属性,但没有任何不同。 Subtotal S$42.50 Tota
如何防止 Bash 拆分子字符串中的单词?这是一个有点人为的例子来说明这个问题: touch file1 'foo bar' FILES="file1 'foo bar'" ls -la $FILES
我正在创建一个非常薄的页面(它被打印在收据纸上:56 毫米宽) 我正在尝试显示一些文本(在本例中为运送选择)。有时这个文本是正常的一些间隔单词,例如'Signed for 1st Class',有时是
我正在尝试弄清楚 IFS 如何影响 bash 中的分词。该行为依赖于上下文,其方式似乎与分词的直觉不符。 总体思路似乎很简单。引自 bash 手册页: The shell treats each ch
今天我 Handlebars 机升级到 iOS7,发现了一些奇怪的问题。 (博客.niwyclin.org)这是我网站的测试帖子页面 在桌面浏览器上它看起来不错。 我用Responsivator查了一
我在 jsfiddle 中有以下示例: https://jsfiddle.net/27L545rr/3/ Word-break should cause just the extra charact
我有一个应用程序,我需要解析或标记 XML 并保留原始文本(例如,不解析实体、不转换属性中的空格、保持属性顺序等)在 Java 程序中。 我今天花了几个小时尝试使用 StAX、SAX、XSLT、Tag
到目前为止,这是我的代码: ssssssssssssssssssssssssssssssssssssss 但是, word-wrap:break-word; word-br
我正在尝试使用 word-break打破一个长字符超过其父宽度的单词。 在这个例子中,我有一个 与 width:43px和里面的“玩”字。在 chrome 中,这个词很合适,但在 Firefox 中,
list(gensim.utils.simple_preprocess("i you he she I it we you they", deacc=True)) 给出结果: ['you', 'he'
我是一名优秀的程序员,十分优秀!