문제풀이

LeetCode(977)::Squares of a Sorted Array

문제

 

Squares of a Sorted Array - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제파악

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 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • nums is sorted in non-decreasing order.

문제풀이

선형적으로 탐색하면서 전부 제곱한 후 다시 정렬해준다.

class Solution {
    public int[] sortedSquares(int[] nums) {
        for(int i = 0; i<nums.length;i++){
            nums[i] = nums[i]*nums[i];
        }
        
         Arrays.sort(nums);
        
        return nums;
    }
}

'문제풀이' 카테고리의 다른 글

BaekJoon(1010)::다리놓기  (0) 2021.11.23
BaekJoon(9461)::파도반 수열  (0) 2021.11.22
LeetCode(344)::Reverse String  (0) 2021.11.18
LeetCode(344)::Reverse String  (0) 2021.11.18
LeetCode(448)::Find All Numbers Disappeared in an Array  (0) 2021.11.18