Can anyone recorrect this code? Code is not working properly. The output should be a square sorted array. Example :Input: [-4,-1,0,3,10] Output: [0,1,9,16,100] I’m using Leetcode platform.
JavaScript
x
13
13
1
class Solution(object):
2
def sortedSquares(self, nums: List[int]):
3
out = []
4
for num in nums:
5
out.append(num**2)
6
self.sorte(out)
7
8
def sorte(self, out):
9
if len(out)<2:
10
return out
11
else:
12
return self.sorte([each for each in out[1:] if each < out[0]]) + [out[0]] + self.sorte([each for each in out[1:] if each >= out[0]])
13
Advertisement
Answer
Accepted solution:
JavaScript
1
5
1
class Solution:
2
def sortedSquares(self, A: List[int]) -> List[int]:
3
out = [x*x for x in A]
4
return sorted(out)
5
If you still want to do like yours check if this can help:
JavaScript
1
13
13
1
class Solution:
2
def sortedSquares(self, A: List[int]) -> List[int]:
3
out = []
4
for num in A:
5
out.append(num**2)
6
return self.sorte(out)
7
8
def sorte(self, out):
9
if len(out)<2:
10
return out
11
else:
12
return self.sorte([each for each in out[1:] if each < out[0]]) + [out[0]] + self.sorte([each for each in out[1:] if each >= out[0]])
13