[LC BC] Move Zeroes 45

YJ·4일 전

LeetCode Bootcamp

목록 보기
1/6

https://www.hackerrank.com/contests/leetcode-bootcamp-week-1/challenges/move-zeroes-45-3/problem?isFullScreen=true

문제

0 을 뒤쪽으로 움직이는 문제 (in-place)

답안

public static List<Integer> moveZeroes(List<Integer> nums) {
	int count = 0;
	for (int num: nums) {
		if (num == 0) {
			count++;
		}
	}
	for (int i = 0; i < count; i++) {
		nums.remove(Integer.valueOf(0));
		nums.add(0);
	}
	return nums;
}

메모

돌면서 지워버리면 Concurrent 관련 Exception 이 뜬다.
그래서 돌면서 0 개수 세어주고, 그 이후에 그만큼 지우고 더하는 작업을 실행해줬다.

profile
Hello

0개의 댓글