일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- programmers
- DI
- google oauth
- 항해99
- JPA
- oauth
- Stream
- hanghae99
- server send event
- 생명주기 콜백
- WIL
- session
- web
- Spring Security
- bean
- jQuery
- real time web
- Hibernate
- html
- python
- jenkins
- SseEmitter
- Java
- JWT
- javascript
- Anolog
- Project
- spring
- flask
- cookie
Archives
- Today
- Total
끄적끄적 코딩일지
[Programmers]나누어 떨어지는 숫자 배열(난이도:★★★) 본문
https://programmers.co.kr/learn/courses/30/lessons/12910
import java.util.Arrays;
class Solution {
public int[] solution(int[] arr, int divisor) {
// stream 사용하기
// int[] answer =Arrays.stream(arr).filter(e->e%divisor == 0).sorted().toArray();
// return answer.length ==0?new int[]{-1}:answer;
return anotherSolution(arr,divisor);
}
// 다른 해법
public int[] anotherSolution(int[] arr,int divisor){
int[] tmp = new int [arr.length];
int idx = 0;
int count = 0;
// 나누어 떨어지는 숫자만 남기기
for(int i : arr ){
if(i % divisor == 0){
tmp[idx] = i;
idx++;
count++;
}
}
// 공백 배열 제거하기
int[] answer = new int[count==0?1:count];
if(count == 0){
answer[0] = -1;
return answer;
}else{
for(int i = 0 ; i < answer.length; i++){
answer[i] = tmp[i];
}
}
// 정렬하기
int idxs = answer.length-1;
for(int i = 0 ; i < answer.length-1; i++){
for(int j = 0; j < idxs; j++){
if(answer[j] > answer[j+1]){
int tmps = answer[j];
answer[j] = answer[j+1];
answer[j+1] = tmps;
}
}
idxs--;
}
return answer;
}
}
'알고리즘' 카테고리의 다른 글
[Programmers]문자열 내 p와 y의 개수(난이도:★★★) (0) | 2022.05.16 |
---|---|
[Programmers]내적(난이도:★★★) (0) | 2022.05.16 |
[Programmers]2016년(난이도:★★★) (0) | 2022.05.16 |
[Programmers]부족한 금액 계산하기(난이도:★★★) (0) | 2022.05.16 |
[Programmers]x만큼 간격이 있는 n개의 숫자(난이도:★★) (0) | 2022.05.16 |