Created
March 14, 2023 16:56
-
-
Save VallarasuS/7b837356634d34deb7e5aed079d9b73a to your computer and use it in GitHub Desktop.
977. Squares of a Sorted Array - https://leetcode.com/problems/squares-of-a-sorted-array/description/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int[] sortedSquares(int[] nums) { | |
for(int i=0; i < nums.length; i++) { | |
nums[i] *= nums[i]; | |
} | |
this.quickSort(nums, 0, nums.length - 1); | |
return nums; | |
} | |
public void quickSort(int[] nums, int left, int right) { | |
if(left >= right) { | |
return; | |
} | |
int pivot = this.partition(nums, left, right); | |
this.quickSort(nums, left, pivot - 1); | |
this.quickSort(nums, pivot + 1, right); | |
} | |
public int partition(int[] nums, int left, int right) { | |
int swapPoint = left - 1; | |
int pivot = nums[right]; | |
for(;left < right; left++) { | |
if(nums[left] <= pivot) { | |
swapPoint++; | |
this.swap(nums, swapPoint, left); | |
} | |
} | |
this.swap(nums, swapPoint + 1, right); | |
return swapPoint + 1; | |
} | |
public void swap(int[] nums, int left, int right) { | |
int item = nums[left]; | |
nums[left] = nums[right]; | |
nums[right] = item; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment