1 Star 0 Fork 0

Roderland/algorithm

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Question977.java 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
Roderland 提交于 2020-10-23 18:19 +08:00 . add
package question;
import java.util.Arrays;
/*
@author: Roderland
@create: 2020-09-23---20:28
*/
public class Question977 {
public int[] sortedSquares(int[] A) {
for (int i = 0; i < A.length; i++) {
A[i] = A[i] * A[i];
}
Arrays.sort(A);
return A;
}
public static void main(String[] args) {
//[-4,-1,0,3,10]
int[] a = new int[]{-2, 0};
int[] ints = new Question977().sortedSquares2(a);
for (int i : ints) {
System.out.println(i);
}
}
public int[] sortedSquares2(int[] A) {
if (A.length == 1) return new int[]{A[0] * A[0]};
int[] a = new int[A.length];
int i = 0;
while (A[i] < 0) i++;
for (int j = 0; j < A.length; j++) A[j] = A[j] * A[j];
int left = i - 1;
int right = i;
int j = 0;
while (left >= 0 && right < A.length) {
if (A[left] < A[right]) a[j++] = A[left--];
else a[j++] = A[right++];
}
while (left >= 0) a[j++] = A[left--];
while (right < A.length) a[j++] = A[right++];
return a;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Roderland/algorithm.git
git@gitee.com:Roderland/algorithm.git
Roderland
algorithm
algorithm
master

搜索帮助