본문 바로가기
코딩테스트

[kotlin] leetcode - Squares of a Sorted Array

by 얘리밍 2022. 4. 27.
320x100
728x90

977. Squares of a Sorted Array : https://leetcode.com/problems/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

 

class Solution {
    fun sortedSquares(nums: IntArray): IntArray {
        for(i in 0 until nums.size){
            nums[i] *= nums[i];
        }
        var arr = nums.sortedArray()
        return arr
    }
}
Runtime: 574 ms, faster than 66.81% of Kotlin online submissions for Squares of a Sorted Array.
Memory Usage: 71.8 MB, less than 46.24% of Kotlin online submissions for Squares of a Sorted Array.

 

 

 

 

- 정렬 결과 배열을 반환하는 함수 : 

sortedArray() : 오름차순으로 정렬한 결과를 반환

sortedArrayDescending() : 함수는 내림차순으로 정렬한 결과를 반환

위 두 함수는 정렬 결과를 반환

 

 

- 배열에 대해 정렬만을 수행하는 함수:

sort() , sortDescending()

위 두 함수는 정렬 대상인 배열에 대하여 정렬만

728x90
반응형