ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2022-02-09 14:32:47  阅读:163  来源: 互联网

标签:977 end nums python number start leecode num new


咱就是说很简单的题目还是要写很久呢
给你一个按 非递减顺序 排序的整数数组 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

标签:977,end,nums,python,number,start,leecode,num,new
来源: https://blog.csdn.net/weixin_44230172/article/details/122833915

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有