时间复杂度O(N ^ 2)在这里如何?

alp*_*aka 0 java time complexity-theory analysis

我已经知道这个问题的答案是,O(N^2)但是我不知道如何。我知道for循环的运行N时间,但是如何运行N^2呢?

public static String rev(String s) {
    String r = "";
    int N = s.length();
    for (int i = 0; i < N; i++) {
        r = s.charAt(i) + r;
    }
    return r;
}
Run Code Online (Sandbox Code Playgroud)

Ole*_*hov 5

在Java中,循环中的String串联r = s.charAt(i) + rO(N^2),因为Strings它们是不可变的- String每次串联都会创建的新副本。