문제 링크:
https://school.programmers.co.kr/learn/courses/30/lessons/150370
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
해답 코드
import java.util.*;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
int[] answer = {};
List<Integer> listanswer = new ArrayList<>();
HashMap<String, Integer> map = new HashMap<>();
int idx =1;
int year = Integer.parseInt(today.split("\\.")[0]);
int month = Integer.parseInt(today.split("\\.")[1]);
int day = Integer.parseInt(today.split("\\.")[2]);
for(String str:terms){
map.put(str.split(" ")[0], Integer.valueOf(str.split(" ")[1]));
}
for(String privacy: privacies){
String date = privacy.split(" ")[0];
int difference = (year - Integer.parseInt(date.split("\\.")[0]))*12*28 +
(month - Integer.parseInt(date.split("\\.")[1]))*28 +
day - Integer.parseInt(date.split("\\.")[2]) ;
int standard = map.get(privacy.split(" ")[1]) * 28;
if(difference >=standard){
listanswer.add(idx);
}
idx++;
}
answer = listanswer.stream().filter(i -> i != null).mapToInt(i -> i).toArray();
//map.forEach((key, value) -> System.out.println(key + " " + value));
//System.out.printf("%d. %d. %d.", year, month, day);
System.out.println();
return answer;
}
}
기억해야 할 것
형 변환 : String to Integer
Integer integerValue = Integer.valueOf(String str);
형 변환 : String to int
int intValue = Integer.parseInt(String str);
String을 '.' (온점) 기준으로 split 하고 싶을 때
String str = "2023.06.13";
String[] strArr = str.split("\\.");
반응형
'코테 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 Lv2] 순위 검색 (0) | 2023.11.23 |
---|---|
[프로그래머스 Lv2] 행렬 테두리 회전하기 (1) | 2023.11.22 |
[자바 Lv0] 전국 대회 선발 고사 (0) | 2023.07.30 |
[프로그래머스 Lv0] 자바 안전지대 (0) | 2023.07.29 |
[프로그래머스 Lv1] 같은 숫자는 싫어! (0) | 2023.06.13 |