基于输入的算法无法用于排序平方数组的算法
我正在努力构建一种算法,该算法为一系列非核心整数制成,这并没有通过我的某些测试。我想知道为什么?我还包括了一个样本输入和输出。
import java.util.*;
class Program {
public int[] sortedSquaredArray(int[] array) {
int[] res = new int[array.length];
int leftPointer = 0;
int rightPointer = array.length - 1;
int counter = 0;
while (counter < array.length) {
int leftSquared = array[leftPointer] * array[leftPointer];
int rightSquared = array[rightPointer] * array[rightPointer];
if (leftSquared < rightSquared) {
res[counter] = leftSquared;
leftPointer++;
} else if (rightSquared <= leftSquared) {
res[counter] = rightSquared;
rightPointer--;
}
counter++;
}
return res;
}
}
"array": [-50, -13, -2, -1, 0, 0, 1, 1, 2, 3, 19, 20]
预期输出:
[0, 0, 1, 1, 1, 4, 4, 9, 169, 361, 400, 2500]
我得到的:
[400, 361, 9, 4, 1, 1, 0, 0, 1, 4, 169, 2500]
I'm working on building an algorithm that sorts in place for an array of nondecreasing integers, and it's not passing some of my tests. I was wondering why? I've included a sample input and output as well.
import java.util.*;
class Program {
public int[] sortedSquaredArray(int[] array) {
int[] res = new int[array.length];
int leftPointer = 0;
int rightPointer = array.length - 1;
int counter = 0;
while (counter < array.length) {
int leftSquared = array[leftPointer] * array[leftPointer];
int rightSquared = array[rightPointer] * array[rightPointer];
if (leftSquared < rightSquared) {
res[counter] = leftSquared;
leftPointer++;
} else if (rightSquared <= leftSquared) {
res[counter] = rightSquared;
rightPointer--;
}
counter++;
}
return res;
}
}
"array": [-50, -13, -2, -1, 0, 0, 1, 1, 2, 3, 19, 20]
expected output:
[0, 0, 1, 1, 1, 4, 4, 9, 169, 361, 400, 2500]
what I'm getting:
[400, 361, 9, 4, 1, 1, 0, 0, 1, 4, 169, 2500]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如@greybeard写道的那样:您的错误是从下端填充,但是您还不知道最低的正方形,因为您正在检查具有最大平方值的两个数字。
此功能应执行您想要的操作:
注意第3和4行中的优化,并仅在需要时计算平方值。另外,此功能不是在实地排序!它正在返回一个带有排序正方形的新数组。可以通过在返回或直接使用传递的数组之前将值从排序的数组重新分配到传递数组的值来实现现场排序指针指向更大的价值。
您可以使用示例数据在这里。
Just as @greybeard wrote: your mistake is to fill from the lower end, but you do not know the lowest square yet, since you are checking the two numbers with the BIGGEST square value.
This function should do what you want:
Note the optimisations in line 3 and 4 and calculating the squared values only when needed. Also this function is NOT doing in-place sorting! It is returning a new array with the sorted squares. Doing an in-place sorting could be accomplished by re-assigning the values from the sorted array to the passed-in array before returning or using the passed-in array directly but having to move around the array-values a lot, if the left pointer is pointing to the bigger value.
You can watch the code in action with your example data here.
如果 将数组指定为越来越多的顺序,那么您的尝试非常接近:
只需填充较大的正方形到较低的结果即可。
(如果数组以非阴性值开头,则只需返回输入数组的(副本)即可。)
If the array was specified to be in increasing order, your attempt was very close:
Just fill the result from larger squares to lower.
(If the array starts with a non-negative value, just return (a copy of) the input array.)