算法每日刷题 Day6 5.14:leetcode数组1道题,用时30min,明天按灵茶山艾府题单开刷,感觉数组不应该单算

14. 977.有序数组的平方(简单,学习,双指针)

977. 有序数组的平方 - 力扣(LeetCode)

思想

法一:
1.平方赋值到另一个数组+sort排序
法二:
1.寻找负数和非负数的分界线(学习代码如何写?),[0,neg]负数,[neg+1,n)非负数,两个指针neg和neg+1分别向左右移动,选择较小的放到新数组里面,一方遍历完把另一方全部平方加到后面,类似于归并排序思想(思考为什么?)
法三:
1.与法二都是双指针思想,不够left从0开始,right从n-1开始,向内遍历逆序把最大的平方放到新数组末尾,所以需要维护一个新数组待赋值位置变量

代码

法一:
c++:

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        vector<int> res;
        int n = nums.size();
        for (int i = 0; i < n; ++i) {
            res.push_back(nums[i] * nums[i]);
        }
        sort(res.begin(), res.end());
        return res;
    }
};

python:

class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        n = len(nums)
        res = []
        for i in range(n):
            res.append(nums[i] * nums[i])
        res.sort()
        return res

法二:
c++:

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        vector<int> res;
        int n = nums.size();
        int neg = -1;
        for (int i = 0; i < n; ++i) {
            if (nums[i] < 0)
                neg = i;
            else
                break;
        }
        int left = neg, right = neg + 1;
        while (left >= 0 && right < n) {
            if (nums[left] * nums[left] < nums[right] * nums[right]) {
                res.push_back(nums[left] * nums[left]);
                left--;
            } else {
                res.push_back(nums[right] * nums[right]);
                right++;
            }
        }
        if (left >= 0) {
            while (left >= 0) {
                res.push_back(nums[left] * nums[left]);
                left--;
            }
        } else {
            while (right < n) {
                res.push_back(nums[right] * nums[right]);
                right++;
            }
        }
        return res;
    }
};

python:

class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        n = len(nums)
        neg = -1
        for i in range(n):
            if nums[i] < 0:
                neg = i
            else:
                break
        left, right = neg, neg + 1
        res = []
        while left >= 0 and right < n:
            if nums[left] * nums[left] < nums[right] * nums[right]:
                res.append(nums[left] * nums[left])
                left -= 1
            else:
                res.append(nums[right] * nums[right])
                right += 1
        if left >= 0:
            for i in range(left, -1, -1):
                res.append(nums[i] * nums[i])
        else:
            for i in range(right, n):
                res.append(nums[i] * nums[i])
        return res

法三:
c++:

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        int n = nums.size();
        vector<int> res(n);
        int left = 0, right = n - 1;
        int id = n - 1;
        while (left <= right) {
            if (nums[left] * nums[left] < nums[right] * nums[right]) {
                res[id--] = nums[right] * nums[right];
                right--;
            } else {
                res[id--] = nums[left] * nums[left];
                left++;
            }
        }
        return res;
    }
};

python:

class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        n = len(nums)
        left, right = 0, n - 1
        id = n - 1
        res = [0] * n
        while left <= right:
            if nums[left] * nums[left] > nums[right] * nums[right]:
                res[id] = nums[left] * nums[left]
                left += 1
                id -= 1
            else:
                res[id] = nums[right] * nums[right]
                right -= 1
                id -= 1
        return res

1.res = [0] * n创建一个长度为n的空数组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值