프로그래머스(자바)/LV.0(자바)

    한 번만 등장한 문자→frequency(), sorted(), groupingBy()★★★

    나의 풀이 import java.util.*; import java.util.stream.Collectors; class Solution { public String solution(String s) { List list= Arrays.asList(s.split("")); return list.stream().sorted().filter(i->Collections.frequency(list, i)==1).collect(Collectors.joining()); } } ▶ soted() 오름차순으로 정렬 ▶ Collections.frequency(Collections, 스트림요소) 다른 사람의 풀이 import java.util.Arrays; import java.util.Map; import java.ut..

    진료순서정하기 → 인덱스 장난치기, 동기화★★, List의 indexOf()

    나의 풀이 import java.util.*; import java.util.stream.Collectors; class Solution { public int[] solution(int[] emergency) { int[] answer = new int[emergency.length]; List intList = Arrays.stream(emergency) .boxed() .collect(Collectors.toList()); Collections.sort(intList, Collections.reverseOrder()); System.out.println(intList); for(int i=0; i Arrays.stream(emergency) .boxed().sorted(Comparator.rever..

    7의 개수★ →"k의 개수"문제와 유사

    나의 풀이 class Solution { public int solution(int[] array) { String str = ""; for(int i = 0; i s.equals("7")) .count(); } } ▶ mapToObj(String::valueOf)은 스트림의 모든 요소를 문자열로 바꾼다. ▶ joing() 메서드를 이용해서 모든 문자열을 합친다. ▶ split() 메서드를 이용해서 문자열을 글자 1개 단위로 쪼갠다. ▶ s는 낱개로 쪼개진 개별 문자열을 의미한다. ▶ filter(s -> s.equals("7")) 개별 문자열이 "7"과 같은 문자열만 추출한다.

    특이한 정렬★★★ sorted(), compareTo(), compare(A, B)

    다른 사람의 풀이 import java.util.Arrays; class Solution { public int[] solution(int[] numList, int n) { return Arrays.stream(numList) .boxed() .sorted((a, b) -> Math.abs(a - n) == Math.abs(b - n) ? b.compareTo(a) : Integer.compare(Math.abs(a - n), Math.abs(b - n))) .mapToInt(Integer::intValue) .toArray(); } } ▶ boxed()의 리턴타입은 Stream 이다. ★ ▶ mapToInt(Integer::intValue)의 리턴타입은 IntStream 이다. ▶ mapToInt(I..