注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
新人求大米~~
给一个sorted integer array, 返回其中每个数的平方组成的array,同样要排序好, 要求O(n)。
例子:[-3,-1,2,4] 返回 [1,4,9,16]
先找到负数和正数的分界点,然后用两个指针分别指向负数一侧和正数一侧。利用原数组已经排好序的性质,负数指针向前遍历,正数指针向后遍历即可
Two pointer 两头往中间靠拢,新建一个同样大小的数组,算两边数字平方然后对比大小,大的放新书组最尾这样更省事
import java.lang.*;
// This is the text editor interface.
// Anything you type or change here will be seen by the other person i
Solution s = new Solution();
int[] result = s.arrS(input);
for (int i = 0; i < result.length; i++) {
System.out.println(result【i】);
}
}
}
|