在O(1)中用Java反转一个字符串?

Dim*_*eou 19 java string

在给定CharSequence的情况下,标准Java库中是否有任何工具在O(1)时间内产生反转?

I guess this is "easy" to implement, just wondering whether it already exists. (I suspect the reason this is not offered is because the "easy" way would actually break multi-char code-points - but in many cases we know we are not dealing with those).

Thanks

Update Heh, it's a bit amusing that most thought this "impossible", good work guys! Well, actually it is (conceptually) trivial - pseudojava follows to make it clear:

class MyReverseString extends String { //of course I can't extend String!
   final String delegate;
   MyReverseString(String delegate) { this.delegate = delegate; }

   int length() { return delegate.length(); }

   int charAt(int i) { return delegate.charAt(delegate.length() - 1 - i); }
}
Run Code Online (Sandbox Code Playgroud)

I'm leaving the question open for some more, just in the rare event that something like the obvious solution (e.g. see Jon Skeet's one) already exists in the JDK and someone knows about it. (Again, highly unlikely due to those nasty code points).

Edit Probably the confusion came from me having "string" in the title (but not String!), whereas I only ask for "the reverse of a CharSequence". If you were confused, sorry. I would have hoped the O(1) part would make exactly clear what was being asked for.

And by the way, this was the question that made me ask this one. (That's a case where it would be easier to run a regex from right-to-left, not left-to-right, so there may be some practical value even for the simple/broken-codepoints implementation)

Jon*_*eet 31

Well, you can easily produce an implementation of CharSequence which returns the same length, and when asked for a particular character returns the one at length-index-1. toString() becomes O(n) of course...

Creating that reversed CharSequence would be O(1) - all it's got to do is store a reference to the original CharSequence, after all. Iterating over all the characters within the sequence is going to be O(n) though, obviously.

请注意,创建逆转CharSequence(根据你的问题的机构)是一样创建一个颠倒String(按照标题你的问题).实际上生成字符串是O(n),必须是.

示例代码,大部分未经测试:

public final class ReverseCharSequence implements CharSequence
{
    private final CharSequence original;

    public ReverseCharSequence(CharSequence original)
    {
        this.original = original;
    }

    public int length()
    {
        return original.length();
    }

    public char charAt(int index)
    {
        return original.charAt(original.length() - index - 1);
    }

    public CharSequence subSequence(int start, int end)
    {
        int originalEnd = original.length() - start;
        int originalStart = original.length() - end;
        return new ReverseCharSequence(
            original.subSequence(originalStart, originalEnd));
    }

    public String toString()
    {
        return new StringBuilder(this).toString();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在收集术语中,我认为你称之为"观点",对吧?你当然可以在`O(1)`中创建一个`CharSequence`的反向"视图",这就是你在这里展示的内容. (3认同)
  • @polygenelubricants:是的,没错.根据需要,该视图本身就是CharSequence.如果OP希望特定操作为O(1),他应该澄清:) (2认同)

jjn*_*guy 10

This is impossible. In order to reverse a string you must process each character at least once, thus, it must be at least O(n).

  • @Justin:然而,显然人们并不认为它有用.不是在这里寻找一个论点,但是发布无益的答案以便"先到达那里"然后再回到编辑中并不会以某种方式坐下来.我的意思是,输入"为了反转字符串需要多长时间,你必须至少处理一次字符,因此,它必须至少是'O(n)`." 还有? (6认同)
  • @Justin:*"我从不打算离开那里"*在发布你的答案的第一个版本之前,确保它有用,即使你打算回来改进它.否则,什么阻止人们发布"alsdfkjalsdkjflaskdfj"然后编辑它,以获得杆位? (5认同)
  • 不......那个贬低者认为简单地说"这是不可能的"并不是一个有用的答案.但是,在编辑之后,这个人认为完全相反(所以upvote);-) (3认同)
  • @Justin,尽管如此,如果您事先知道这不是您的最终答案,为什么要发布?其他人怎么会知道这不是你的最终答案呢?只要对这样的答案得到(或更多)投票,不要感到惊讶. (2认同)

Eta*_*tan 5

string result = new StringBuffer(yourString).reverse().toString();
Run Code Online (Sandbox Code Playgroud)

Depending on what you understand under O(1) it does not seem possible since even reading the string once needs O(n) with n being the number of characters in the string.