algo/di-yi-zhan-da78c/shou-ba-sh-daeca/qian-zhui--46e56/ #1447
Replies: 5 comments 1 reply
-
必须付费才能看的吧,关注公众号也看不了 |
Beta Was this translation helpful? Give feedback.
0 replies
-
能不能让所有代码都可以java转python |
Beta Was this translation helpful? Give feedback.
1 reply
-
这篇文章质量比较高! |
Beta Was this translation helpful? Give feedback.
0 replies
-
求最长和最短字符串的前缀的方法重构成一个方法: public String shortestPrefixOf(String query) {
return shortOrLongPrefixOf(root, query, true);
}
String shortOrLongPrefixOf(TrieNode<V> node, String query, boolean shortOrLong) {
if (node == null) {
return null;
}
int llen = 0, i = 0;
for (int len = query.length(); i < len; i++) {
if (node == null) {
break;
}
if (node.val != null) {
if (shortOrLong) { // short
return query.substring(0, i);
}
llen = i;
}
char c = query.charAt(i);
node = node.children[c];
}
// query 本身就是 key
if (i == query.length() && node.val != null) {
return query;
}
// 如果 求最长字符串
if (!shortOrLong && llen > 0) {
return query.substring(0, llen);
}
return null;
}
public String longestPrefixOf(String query) {
return shortOrLongPrefixOf(root, query, false);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
algo/di-yi-zhan-da78c/shou-ba-sh-daeca/qian-zhui--46e56/
Info 数据结构精品课 (https://aep.h5.xeknow.com/s/1XJHEO) 和 递归算法专题课 (https://aep.xet.tech/s/3YGcq3) 限时附赠网站会员!第 21 期打卡挑战 (https://opedk.xet.tech/s/4ptSo2) 最后几天报名! 读完本文,你不仅学会了算法套路,还可以顺便解决...
https://labuladong.github.io/algo/di-yi-zhan-da78c/shou-ba-sh-daeca/qian-zhui--46e56/
Beta Was this translation helpful? Give feedback.
All reactions