Notice
Recent Posts
Recent Comments
Link
«   2026/06   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

개발자도전

[LeetCode] Squares of a Sorted Array 본문

LeetCode

[LeetCode] Squares of a Sorted Array

도do 2023. 1. 19. 20:41
728x90

[문제]

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

감소하지 않는 순서로 정렬된 정수 배열을 정렬시켜 각 값의 제곱을 반환시키기.

 

예 1.

Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]

 

예 2.

Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]

 

[답]

for문으로 값을 빼준 후 i번째에 있는 값을 곱해서 다시 대입해주었다.

후 for문 밖에서 Arrays.sort함수를 사용해 nums 배열을 다시 정렬시켜주고 return 시켜주었다.

728x90

'LeetCode' 카테고리의 다른 글

[LeetCode] Remove Element  (0) 2023.01.26
[LeetCode] Merge Sorted Array  (0) 2023.01.25
[LeetCode] Duplicate Zeros  (0) 2023.01.20
[LeetCode] Find Numbers with Even Number of Digits  (1) 2023.01.19
[LeetCode] Max Consecutive Ones  (0) 2023.01.19
Comments