[英]Time complexity of StringBuilder reverse method
I want to find the time complexity of StringBuilder reverse method. 我想找到StringBuilder反向方法的时间复杂度。 Here the source code of the reverse method: 这里是reverse方法的源代码:
public AbstractStringBuilder reverse() {
boolean hasSurrogate = false;
int n = count - 1;
for (int j = (n-1) >> 1; j >= 0; --j) {
char temp = value[j];
char temp2 = value[n - j];
if (!hasSurrogate) {
hasSurrogate = (temp >= Character.MIN_SURROGATE && temp <= Character.MAX_SURROGATE)
|| (temp2 >= Character.MIN_SURROGATE && temp2 <= Character.MAX_SURROGATE);
}
value[j] = temp2;
value[n - j] = temp;
}
if (hasSurrogate) {
// Reverse back all valid surrogate pairs
for (int i = 0; i < count - 1; i++) {
char c2 = value[i];
if (Character.isLowSurrogate(c2)) {
char c1 = value[i + 1];
if (Character.isHighSurrogate(c1)) {
value[i++] = c1;
value[i] = c2;
}
}
}
}
return this;
}
Here's a link to the doc: documentation Which is the time complexity? 这是文档的链接: 文档这是什么时间复杂度?
Is there any way to perform more efficiently the reversion of a String? 有什么方法可以更有效地执行String的还原?
无法将字符串还原为小于O(n)的值,并且您明确发布的算法为O(n),因为它包含两个连续的O(n)循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.