일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
29 | 30 |
Tags
- compiler
- kotlin
- redis
- Github
- build_test
- aws
- db
- git
- Airflow
- immutable
- rds
- JPA
- JUnit
- mutable
- Action
- AOP
- 사이드 프로젝트
- springboot
- java
- string
- QueryDSL
- 알고리즘
- workflow
- CodeDeploy
- template
- Spring
- EC2
- 토비의 스프링
Archives
- Today
- Total
개발 일기
프로그래머스 레벨2 튜플 (JAVA) 본문
이번 주말에는 레벨2를 계속 풀어보았다
오늘 풀 문제
https://programmers.co.kr/learn/courses/30/lessons/64065
코딩테스트 연습 - 튜플
"{{2},{2,1},{2,1,3},{2,1,3,4}}" [2, 1, 3, 4] "{{1,2,3},{2,1},{1,2,4,3},{2}}" [2, 1, 3, 4] "{{4,2,3},{3},{2,3,4,1},{2,3}}" [3, 2, 4, 1]
programmers.co.kr
튜플!
모든 코드는 GitHub 에 올려놓았다.
이문제는.. 흠 좀 코드가 길다.
조금 생각하면 코드를 줄일 수 있을 거 같은데.
다음에 줄여봐야겠다
코드는 이렇다..
import java.util.*;
class Solution {
public int[] solution(String s) {
Map<Integer, Integer> map = new HashMap<>();
StringBuilder builder = new StringBuilder(s);
builder.deleteCharAt(0);
builder.deleteCharAt(builder.length() -1);
String[] arr = builder.toString().split(",");
for(String str : arr) {
String[] temp = str.replace("{", "").replace("}","").split(",");
for(String tempStr : temp) {
int key = Integer.parseInt(tempStr);
if(map.containsKey(key)) {
map.put(key , map.get(key) + 1);
continue;
}
map.put(key , 1);
}
}
ArrayList<Map.Entry<Integer, Integer>> sortList = new ArrayList<>(map.entrySet());
sortList.sort(((o1, o2) -> o2.getValue().compareTo(o1.getValue())));
int[] answer = new int[sortList.size()];
int index = 0;
for(Map.Entry<Integer, Integer> entry : sortList) {
answer[index] = entry.getKey();
index++;
}
return answer;
}
}
양옆에 {}를 제거해주고
, (콤마)로 split 해준후
매번 인덱스에서 뽑을 때마다 {}를 다시 또 제거해주었다..
그리고 맵에 각 숫자별로
키 , 중첩된 수
를 넣고 value 값으로 sort 해준 후
배열로 뽑아서 return 시켜 주었다..
조금씩 레벨2에 도전하고 있고
알고리즘 책도 포기 시작하였다
알고리즘 책의 관한 글도 따로 올리도록 해야겠다..!
'알고리즘 > programmers' 카테고리의 다른 글
프로그래머스 레벨2 124 나라의 숫자 (JAVA) (0) | 2021.07.13 |
---|---|
프로그래머스 레벨2 전화번호 목록 (JAVA) (0) | 2021.07.11 |
프로그래머스 레벨 2 짝지어 제거하기 (JAVA) (0) | 2021.07.10 |
프로그래머스 레벨 1 숫자 문자열과 영단어 (JAVA) (0) | 2021.07.10 |
프로그래머스 레벨 1 키패드 누르기 알고리즘 (JAVA) (0) | 2021.07.10 |