题目链接:https://leetcode.cn/problems/squares-of-a-sorted-array/
利用双指针从输入数组的两端开始对比,看谁的绝对值大,将绝对值大的一端从后向前依次插入结果数组中(同时将指针向中间移动)。
class Solution {
public int[] sortedSquares(int[] nums) {
int lenNums = nums.length;
int leftIndex = 0;
int rightIndex = lenNums - 1;
int[] result = new int[lenNums];
int k = lenNums - 1;
while (leftIndex <= rightIndex){
int tempL = Math.abs(nums[leftIndex]);
int tempR = Math.abs(nums[rightIndex]);
if (tempL > tempR){
result[k--] = (int) Math.pow(tempL, 2);
leftIndex ++;
}else {
result[k--] = (int) Math.pow(tempR, 2);
rightIndex --;
}
}
return result;
}
}
时间复杂度:O(n)
空间复杂度:O(n)
双指针
先依次对数组中的元素计算平方,再进行快速排序
略
时间复杂度:O(n + nlogn)
空间复杂度:O(1)
快速排序
对于双指针法,第一反应是先找到绝对值最小的元素,从中间向两端进行遍历,虽然时间复杂上相差不多近似O(n),但是当两端的指针之一到达边界后,需要另外的判断条件,步骤上相对来说有较多的冗余。
public int[] sortedSquares(int[] nums) {
int leftIndex = 0;
int rightIndex = 1;
int lenNums = nums.length;
int[] result = new int[lenNums];
int k = 0;
if (lenNums == 1) return new int[]{(int) Math.pow(nums[0], 2)};
while (rightIndex < lenNums && Math.abs(nums[leftIndex]) >= Math.abs(nums[rightIndex])) {
leftIndex++;
rightIndex++;
}
while (leftIndex >= 0 && rightIndex < lenNums){
if (Math.abs(nums[leftIndex]) <= Math.abs(nums[rightIndex])) {
result[k++] = (int) Math.pow(nums[leftIndex--], 2);
} else {
result[k++] = (int) Math.pow(nums[rightIndex++], 2);
}
}
if (leftIndex == -1 && rightIndex < lenNums) {
while (rightIndex < lenNums) {
result[k++] = (int) Math.pow(nums[rightIndex++], 2);
}
}else if (leftIndex > -1 && rightIndex == lenNums){
while (leftIndex > -1) {
result[k++] = (int) Math.pow(nums[leftIndex--], 2);
}
}
return result;
}
快速排序待掌握
https://programmercarl.com/0704.%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE.html#%E6%80%9D%E8%B7%AF
题目链接:https://leetcode.cn/problems/minimum-size-subarray-sum/
滑动窗口,主要重点在于子串需要是连续的。
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int minLen = Integer.MAX_VALUE;
int left = 0;
int right = 0;
int sum = 0;
while (right < nums.length) {
sum += nums[right++];
while (sum >= target) {
int len = right - left;
if (len < minLen) {
minLen = len;
}
sum -= nums[left++];
}
}
return minLen == Integer.MAX_VALUE ? 0 : minLen;
}
}
时间复杂度:O(n)
空间复杂度:O(1)
快速排序
https://programmercarl.com/0704.%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE.html#%E6%80%9D%E8%B7%AF
题目链接:https://leetcode.cn/problems/spiral-matrix-ii/
模拟法,主要思想是以一圈一圈的遍历给定的矩阵,题目中给的是长宽相同的矩阵,可以忽略部分因素。
如果是长度和高度不同的矩阵该如何处理?参考题目leetcode54。
class Solution {
public int[][] generateMatrix(int n) {
int[][] result = new int[n][n];
int startX = 0;
int startY = 0;
int num = 1;
int loop = n / 2;
while (loop-- > 0) {
for (int i = startY; i < n - startY - 1; i++) {
result[startX][i] = num++;
}
for (int i = startX; i < n - startX - 1; i++) {
result[i][n - startY - 1] = num++;
}
for (int i = n - startY - 1; i > startY; i--) {
result[n - startX - 1][i] = num++;
}
for (int i = n - startX - 1; i > startX; i--) {
result[i][startY] = num++;
}
startX++;
startY++;
}
if (n % 2 == 1) {
int mid = n / 2;
result[mid][mid] = n * n;
}
return result;
}
}
时间复杂度:O(n)
空间复杂度:O(n)
快速排序
https://programmercarl.com/0704.%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE.html#%E6%80%9D%E8%B7%AF
题目链接:https://leetcode.cn/problems/spiral-matrix-ii/
和59题相比,一个是写出一个是写入,大同小异。
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>();
int right = matrix[0].length - 1;
int left = 0;
int top = 0;
int bottom = matrix.length - 1;
while (left <= right && top <= bottom) {
for (int column = left; column <= right; column++) {
result.add(matrix[top][column]);
}
for (int row = top+1; row<= bottom; row++){
result.add(matrix[row][right]);
}
if (left < right && top < bottom) {
for (int column = right - 1; column > left; column --){
result.add(matrix[bottom][column]);
}
for (int row = bottom; row > top; row --){
result.add(matrix[row][left]);
}
}
left ++;
right --;
top ++;
bottom --;
}
return result;
}
}
时间复杂度:O(n)
空间复杂度:O(n)
https://programmercarl.com/0704.%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE.html#%E6%80%9D%E8%B7%AF