Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 代码部队第322轮(第2部分)-B O(n)但仍超时_Java_Algorithm - Fatal编程技术网

Java 代码部队第322轮(第2部分)-B O(n)但仍超时

Java 代码部队第322轮(第2部分)-B O(n)但仍超时,java,algorithm,Java,Algorithm,问题是: 我的代码处于打开状态,并且我将我的代码与其他代码进行了比较,无法理解为什么我的代码在n=100000的测试用例6中超过了时间限制?有什么想法吗 private void solve() throws IOException { //String s = nextToken(); int n = nextInt(); int[] array = new int[n]; for (int i = 0; i < n; i++) { arr

问题是:

我的代码处于打开状态,并且我将我的代码与其他代码进行了比较,无法理解为什么我的代码在n=100000的测试用例6中超过了时间限制?有什么想法吗

 private void solve() throws IOException {
    //String s = nextToken();
    int n = nextInt();

    int[] array = new int[n];
   for (int i = 0; i < n; i++) {
       array[i] = nextInt();
   }

   int max = -1;
   String ans = "";
   for (int i = array.length-1; i >=0; i--) {
       if (array[i] >= max) {
           max = array[i];
           ans = 0 + " " + ans;
       }
       else {
           ans = ( max - array[i] +1) + " " + ans ;
       }
   }


   writer.println(ans.substring(0,ans.length()-1));


}
您的代码未打开;+上的字符串处于启用状态,而您的代码处于^2上。您需要改用StringBuilder

此外,在开头而不是结尾插入通常也是不好的,因此即使是StringBuilder也不能让您有效地执行此操作


您需要找出如何以另一种方式构建答案。

因为它依赖于字符串连接的用法,每个字符串连接都是开的。啊啊啊啊,没错!!非常感谢你!