File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change
1
+ ##==================================
2
+ ## Leetcode
3
+ ## Student: Vandit Jyotindra Gajjar
4
+ ## Year: 2020
5
+ ## Problem: 977
6
+ ## Problem Name: Sort Array by Parity
7
+ ##===================================
8
+ #
9
+ #Given an array of integers A sorted in non-decreasing order,
10
+ #return an array of the squares of each number, also in sorted non-decreasing order.
11
+ #
12
+ #Example 1:
13
+ #
14
+ #Input: [-4,-1,0,3,10]
15
+ #Output: [0,1,9,16,100]
16
+ #Example 2:
17
+ #
18
+ #Input: [-7,-3,2,3,11]
19
+ #Output: [4,9,9,49,121]
20
+ class Solution :
21
+ def sortedSquares (self , A ):
22
+ tmp = [abs (i ) for i in A ] #Initialize tmp and convert all negatives to positive using absolute function
23
+ tmp .sort () #Sort tmp list
24
+ tmpSquare = [0 ]* len (A ) #Initialize tmpSquare same length as A
25
+ for i in range (len (tmp )): #Loop through tmp
26
+ tmpSquare [i ] = tmp [i ]* tmp [i ] #Update the tmpSquare by adding squares of number
27
+ return tmpSquare #Return tmpSquare
You can’t perform that action at this time.
0 commit comments