Given an array of integers nums
, sort the array in ascending order.
Example 1:
Input: nums = [5,2,3,1] Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0] Output: [0,0,1,1,2,5]
Constraints:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000
根据约束计数排序
class Solution(object):
def sortArray(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res = []
counts = [0 for i in range(100001)]
for num in nums:
counts[num + 50000] += 1
for i in range(len(counts)):
while counts[i]:
res.append(i - 50000)
counts[i] -= 1
return res
if __name__ == '__main__':
solution = Solution()
nums = [5, 2, 3, 1]
print(solution.sortArray(nums))