Notice
Recent Posts
Recent Comments
Link
JiYoung Dev 🖥
[JAVA] 배열 크기 변경하기 본문
반응형
Arrays.copyOf(배열, 변경하고자 하는 크기)
copyOf로 배열 복하하고 원하는 크기로 지정
class Solution {
public int[] solution(int []arr) {
int idx = 1;
int[] answer = new int[arr.length];
answer[0] = arr[0];
for(int i = 1 ; i < arr.length ; i++) {
if(arr[i] != arr[i-1]) {
answer[idx] = arr[i];
idx++;
}
}
int[] copy = Arrays.copyOf(answer, idx);
return copy;
}
}
반응형
'Question' 카테고리의 다른 글
[python] 소수점 자릿수 지정 (round) (0) | 2023.08.22 |
---|---|
[JAVA] char 에서 문자열로 변환하기 (0) | 2023.08.21 |
[JAVA] 문자열 자르기(substring) (0) | 2023.08.11 |
[C#] System.Threading.Timer 쓰레드 구현 (0) | 2023.08.10 |
[Thymeleaf] th:onclick과 location.href 함께 사용하기 (0) | 2023.08.08 |