Java 代码部队第322轮(第2部分)-B O(n)但仍超时
问题是: 我的代码处于打开状态,并且我将我的代码与其他代码进行了比较,无法理解为什么我的代码在n=100000的测试用例6中超过了时间限制?有什么想法吗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
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也不能让您有效地执行此操作
您需要找出如何以另一种方式构建答案。因为它依赖于字符串连接的用法,每个字符串连接都是开的。啊啊啊啊,没错!!非常感谢你!