leecode 题目977 有序数组的平方(python)

咱就是说很简单的题目还是要写很久呢
给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。

示例 1:

输入:nums = [-4,-1,0,3,10]
输出:[0,1,9,16,100]
解释:平方后,数组变为 [16,1,0,9,100]
排序后,数组变为 [0,1,9,16,100]

示例 2:

输入:nums = [-7,-3,2,3,11]
输出:[4,9,9,49,121]

class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        length = len(nums)
        if length < 1:
            return nums
        elif nums[0] >= 0:
            nums = [i*i for i in nums]
            return nums
        elif nums[-1] <= 0:
            nums = [i*i for i in nums]
            return nums[::-1]

        new_nums = []
        l = 0
        
        max_position = 0
        while(l<length):
            number = nums[l]*nums[l]
            if (nums[l] < 0):
                new_nums.insert(0,number)
            elif (nums[l] == 0):
                new_nums.insert(0,0)
            else:
                temp_len = len(new_nums)
                for t in range(max_position,temp_len):
                    if new_nums[t] >= number:
                        new_nums.insert(t,number)
                        max_position = t
                        break
                if new_nums[-1] < number:
                    new_nums.append(number)
            l += 1    
        '''
        while(l<length and nums[l] < 0):
            new_nums.insert(0,nums[l]*nums[l])
            l += 1

        while (l<length and nums[l] == 0):
            new_nums.insert(0,0)
            l += 1

        max_position = 0
        while(l<length and nums[l] > 0):
            if new_nums == []:
                new_nums = [i*i for i in nums]
                break
            number = nums[l]*nums[l]
            temp_len = len(new_nums)
            for t in range(max_position,temp_len):
                if new_nums[t] >= number:
                    new_nums.insert(t,number)
                    max_position = t
                    break
            if new_nums[-1] < number:
                new_nums.append(number)
            l += 1
        '''
        return new_nums
                       

这个是和同学讨论之后写的新方法
先使用二分法找到正负的交界点
从交界点向前向后用双指针搜索下一个插入的最小平方数

class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        length = len(nums)
        new_nums = []
        if length < 1:
            return nums
        elif nums[0] >= 0:
            nums = [i*i for i in nums]
            return nums
        elif nums[-1] <= 0:
            nums = [i*i for i in nums]
            return nums[::-1]
        
        start,end = 0,length-1
        i1 = int(length/2)
        while(end - start > 1):
            if nums[i1] >= 0:
                end = i1
            else:
                start = i1
            i1 = int((end+start)/2)
        
        start_num = nums[start]*nums[start]
        end_num = nums[end]*nums[end]

        for i in range(length):
            if (start_num <= end_num):
                new_nums.append(start_num)  
                start = start - 1  
                if start >= 0:
                    start_num = nums[start]*nums[start]
                else:
                    start_num = 9999999999999999
            else:
                new_nums.append(end_num)
                end = end + 1
                if end < length:
                    end_num = nums[end]*nums[end]
                else:
                    end_num = 9999999999999999

        return new_nums
           

测试结果:

执行用时: 52 ms
在所有 Python3 提交中击败了84.97% 的用户
内存消耗:16.6 MB
在所有 Python3 提交中击败了11.96% 的用户
通过测试用例: 137 / 137

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/squares-of-a-sorted-array

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丁lingling哇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值