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

上一篇:企业链表测试


下一篇:FJ的字符串(递归)