pon*_*cat 4 java arrays search phrase
将字符串拆分为其后缀数组的最有效方法是什么?
假设你有字符串"天气很好",我想生成一个后缀数组:
[0] = "nice"
[1] = "is nice"
[2] = "weather is nice"
[3] = "the weather is nice"
Run Code Online (Sandbox Code Playgroud)
我可以从头到尾以令牌(单词)的形式访问迭代器形式的字符串.
使用the在空格上拆分数组split
,然后从后到前遍历生成的标记,获取前一个后缀,并将当前标记添加到其前面.如果没有先前的后缀,请使用空字符串:
String str = "quick brown fox jumps over the lazy dog";
List<String> res = new ArrayList<String>();
String last = null;
String[] tok = str.split(" ");
for (int i = tok.length-1 ; i >= 0 ; i--) {
if (last == null) {
last = tok[i];
} else {
last = tok[i] + " " + last;
}
res.add(last);
}
for (String s : res) {
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)
这打印
dog
lazy dog
the lazy dog
over the lazy dog
jumps over the lazy dog
fox jumps over the lazy dog
brown fox jumps over the lazy dog
quick brown fox jumps over the lazy dog
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
178 次 |
最近记录: |