Skip to content

Commit 4729c6a

Browse files
committed
Adding Simple Solution Problem - 977 - Squares Sorted Array
1 parent 9eb0265 commit 4729c6a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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

0 commit comments

Comments
 (0)