[Java] LeetCode 977. Squares of a Sorted Array

Sommi·2023년 8월 1일
0

Algorithm

목록 보기
3/5

https://leetcode.com/problems/squares-of-a-sorted-array/

문제

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
배열값을 제곱한 수를 오름차순 정렬

소스코드

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

설명

새로운 배열 arr 정의해서 제곱값을 넣음
Arrays.sort() 함수를 사용하여 arr 정렬

profile
넌 할 수 있어 라고 말해 주세요~

1개의 댓글

comment-user-thumbnail
2023년 8월 1일

좋은 글 감사합니다. 자주 올게요 :)

답글 달기

관련 채용 정보