977.有序陣列的平方
題目:https://leetcode.cn/problems/squares-of-a-sorted-array/description/
第一次寫的時候直接就是先排序了,但是好像超時了
class Solution(object): def sortedSquares(self, nums): """ :type nums: List[int] :rtype: List[int] """ for i in range(len(nums)): min = abs(nums[i]) min_index = i for j in range(i, len(nums)): if abs(nums[j]) < min: min = abs(nums[j]) min_index = j mid = abs(nums[i]) nums[i] = nums[min_index]*nums[min_index] if not i== min_index: nums[min_index] = mid return nums
我好像對演算法不是很熟,感覺不太能想象多維陣列的執行,重新去看教程了