본문 바로가기
코딩테스트

[kotlin] leetcode - 283. Move Zeroes

by 얘리밍 2022. 5. 2.
320x100
728x90

283. Move Zeroes : https://leetcode.com/problems/move-zeroes/

 

Move Zeroes - 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 moveZeroes(nums: IntArray): Unit {
        val arr = arrayListOf<Int>();
        val numList = arrayListOf<Int>();
        
        nums.forEach{
            when(it) {
                0 -> arr.add(it)
                else -> numList.add(it)
            }
        }

        //원본 배열에 덮어씌우기 
      
        val sorted = numList + arr
        
        nums.indices.forEach{
            nums[it] = sorted[it]
        }
        
    }
}
74 / 74 test cases passed.
Status: 

Accepted

Runtime: 709 ms
Memory Usage: 72.6 MB
Submitted: 6 minutes ago

 

주어지는 배열은 수정 불가 배열

수정 가능한 배열 : arrayListOf() 와 mutableListOf() 

 

indices => index value of list 

 

 

728x90
반응형