[프로그래머스] 튜플 64065번 (java) String
2020-05-12
튜플 64065번 (프로그래머스 > String)
문제설명
풀이
import java.util.*;
class Solution {
static public int[] solution(String s) {
s = s.substring(1, s.length()-1);
s += ",";
String sp[] = s.split("},");
for(int i=0; i<sp.length; i++)
sp[i] = sp[i].substring(1,sp[i].length());
int cnt[] = new int[sp.length];
StringTokenizer st = null;
ArrayList<Integer>[] list = new ArrayList[sp.length];
for(int i=0; i<list.length; i++) {
list[i] = new ArrayList<>();
st = new StringTokenizer(sp[i], ",");
while(st.countTokens() > 0) {
list[i].add(Integer.parseInt(st.nextToken()));
cnt[i]++;
}
}
int size = 1;
int idx = 0;
int num[] = new int[100000];
LinkedList<Integer> ans = new LinkedList<>();
while(size <=sp.length) {
for(int i=0; i<list.length; i++) {
if(list[i].size() == size) {
idx = i;
size++;
break;
}
}
for(int i=0; i<list[idx].size(); i++) {
for(int j=0; j<list[idx].size(); j++) {
int x = list[idx].get(j);
if(num[x] ==0) {
ans.add(x);
num[x] = 1;
}
}
}
}
return ans.stream().mapToInt(i->i).toArray();
}
}
후기 (1h 30min)
온갖 난리를 다침…ㅎㅎㅎ
- 더 간단하게 푼 사람 링크!
- https://cheonjoosung.github.io/blog/pg-tuple