继续练习双指针,题目链接:
https://leetcode-cn.com/problems/squares-of-a-sorted-array/submissions/
先来个暴力求解,遍历平方+调用排序算法
class Solution {
public int[] sortedSquares(int[] nums) {
int[] ans = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
ans[i] = nums[i] * nums[i];
}
Arrays.sort(ans);
return ans;
}
}
接下来就是风骚的双指针了
class Solution {
public int[] sortedSquares(int[] nums) {
int[] ans = new int[nums.length];
int i = 0, j = nums.length - 1;
int flag = nums.length - 1;
//左右指针向中间收拢,大的放入ans数组,flag--
while (i <= j) {
if(nums[i] * nums[i] > nums[j] * nums[j]) {
ans[flag] = nums[i] * nums[i];
i++;
} else {
ans[flag] = nums[j] * nums[j];
j--;
}
flag--;
}
return ans;
}
}
while的终止条件也可以是flag >= 0,一个意思。
趁机复习排序算法!
1.Bubble Sort
class Solution {
public int[] sortedSquares(int[] nums){
int[] ans = new int[nums.length];
for (int i = 0; i < nums.length; i++){
ans[i] = nums[i] * nums[i];
}
for (int i = 0; i < nums.length; i++){
for(int j = 0; j < nums.length - 1 - i;j++){
if(ans[j] > ans[j + 1]){
int temp = ans[j + 1];
ans[j + 1] = ans[j];
ans[j] = temp;
}
}
}
return ans;
}
}
2.Selection Sort
class Solution {
public int[] sortedSquares(int[] nums){
int[] ans = new int[nums.length];
int temp,min;
for (int i = 0; i < nums.length; i++){
ans[i] = nums[i] * nums[i];
}
for (int i = 0; i < nums.length - 1; i++){
min = i;
for(int j = i + 1; j < nums.length; j++){
if(ans[j] < ans[min]){
min = j;
}
}
temp = ans[i];
ans[i] = ans[min];
ans[min] = temp;
}
return ans;
}
}
3.Insertion Sort
class Solution {
public int[] sortedSquares(int[] nums){
int[] ans = new int[nums.length];
int pre,cur;
for (int i = 0; i < nums.length; i++){
ans[i] = nums[i] * nums[i];
}
for (int i = 0; i < nums.length - 1; i++){
pre = i;
cur = ans[i + 1];
while(pre >= 0 && ans[pre] > cur){
ans[pre + 1] = ans[pre];
pre--;
}
ans[pre + 1] = cur;
}
return ans;
}
}
4.Shell Sort
class Solution {
public int[] sortedSquares(int[] nums){
int[] ans = new int[nums.length];
int len = nums.length;
for (int i = 0; i < nums.length; i++){
ans[i] = nums[i] * nums[i];
}
for(int gap = len / 2; gap > 0; gap = gap / 2){
for(int i = gap; i < len; i++){
int j = i;
int current = ans[i];
while(j - gap >= 0 && current < ans[j - gap]){
ans[j] = ans[j - gap];
j = j - gap;
}
ans[j] = current;
}
}
//不得不说,这个牛逼
return ans;
}
}
5.Merge Sort
class Solution {
public int[] sortedSquares(int[] nums){
int[] ans = new int[nums.length];
int len = nums.length;
for (int i = 0; i < nums.length; i++){
ans[i] = nums[i] * nums[i];
}
sort(ans,0,len-1);
return ans;
}
public int[] sort(int[] a, int left, int right){
int mid = (left + right) / 2;
if(left < right){
sort(a, left, mid);
sort(a, mid + 1, right);
merge(a, left, right, mid);
}
return a;
}
public void merge(int[] a,int left, int right,int mid){
int[] temp = new int[right - left + 1];
int i = left;
int j = mid + 1;
int k = 0;
while(i <= mid && j <= right){
if(a[i] < a[j]){
temp[k++] = a[i++];
}else{
temp[k++] = a[j++];
}
}
while(i <= mid){
temp[k++] = a[i++];
}
while(j <= right){
temp[k++] = a[j++];
}
for(int x = 0; x < temp.length; x++){
a[x+left] = temp[x];
}
}
}
剩下几个排序下次再写,下次一定