Algorithms/- 프로그래머스
-
프로그래머스 - 오픈채팅방 [자바]Algorithms/- 프로그래머스 2022. 2. 4. 00:16
문제 링크 : 오픈채팅방 코딩테스트 연습 - 오픈채팅방 오픈채팅방 카카오톡 오픈채팅방에서는 친구가 아닌 사람들과 대화를 할 수 있는데, 본래 닉네임이 아닌 가상의 닉네임을 사용하여 채팅방에 들어갈 수 있다. 신입사원인 김크루는 카카오톡 오 programmers.co.kr 정답 import java.util.*; class Solution { public String[] solution(String[] record) { Map ssMap = new HashMap(); int cnt = 0; for(String str : record){ if(str.startsWith("Enter") || str.startsWith("Change")){ String[] arr = str.split(" "); ssMap.pu..
-
프로그래머스 - 정수 내림차순으로 배치하기 [자바]Algorithms/- 프로그래머스 2022. 2. 3. 23:41
문제 링크 : 정수 내림차순으로 배치하기 코딩테스트 연습 - 정수 내림차순으로 배치하기 함수 solution은 정수 n을 매개변수로 입력받습니다. n의 각 자릿수를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요. 예를들어 n이 118372면 873211을 리턴하면 됩니다. 제한 조건 n은 1이 programmers.co.kr 정답 import java.util.*; class Solution { public long solution(long n) { char[] arr = String.valueOf(n).toCharArray(); Arrays.sort(arr); return Long.valueOf(new StringBuilder(new String(arr)).reverse().toString()..
-
프로그래머스 - 두 정수 사이의 합 [자바]Algorithms/- 프로그래머스 2022. 2. 3. 23:24
문제 링크 : 두 정수 사이의 합 코딩테스트 연습 - 두 정수 사이의 합 두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요. 예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다. 제한 조건 a와 b가 같은 경우 programmers.co.kr 정답 class Solution { public long solution(int a, int b) { if(a == b){ return a; }else{ int t = 0; if(b < a){ t = a; a = b; b = t; } int len = b - a; long res = 0; for(int i=0; i
-
프로그래머스 - 짝수와 홀수 [자바]Algorithms/- 프로그래머스 2022. 2. 3. 23:23
문제 링크 : 짝수와 홀수 코딩테스트 연습 - 짝수와 홀수 정수 num이 짝수일 경우 "Even"을 반환하고 홀수인 경우 "Odd"를 반환하는 함수, solution을 완성해주세요. 제한 조건 num은 int 범위의 정수입니다. 0은 짝수입니다. 입출력 예 num return 3 "Odd" 4 "Even" programmers.co.kr 정답 class Solution { public String solution(int num) { return num % 2 == 0 ? "Even": "Odd"; } } 분석 단순 홀짝 비교
-
프로그래머스 - 신고 결과 받기 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 23:54
문제 링크 : 신고 결과 받기 코딩테스트 연습 - 신고 결과 받기 문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의 programmers.co.kr 정답 시간 초과 정답 import java.util.*; import java.util.stream.*; class Solution { public int[] solution(String[] id_list, String[] report, int k) { //유저당 신고한 사람 set 한 list로 가지고 있어야 함. //신고된 사람 //유저가 신고한 사람 setlist와 신고된 사람 카운트 해서 각 index에 맞게 ++ S..
-
프로그래머스 - 자연수 뒤집어 배열로 만들기 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 22:20
문제 링크 : 자연수 뒤집어 배열로 만들기 코딩테스트 연습 - 자연수 뒤집어 배열로 만들기 자연수 n을 뒤집어 각 자리 숫자를 원소로 가지는 배열 형태로 리턴해주세요. 예를들어 n이 12345이면 [5,4,3,2,1]을 리턴합니다. 제한 조건 n은 10,000,000,000이하인 자연수입니다. 입출력 예 n return 12345 programmers.co.kr 정답 class Solution { public int[] solution(long n) { int len = (int)Math.log10(n) + 1; int[] a = new int[len]; for(int i=0; i
-
프로그래머스 - 문자열 내 마음대로 정렬하기 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:50
문제 링크 : 문자열 내 마음대로 정렬하기 코딩테스트 연습 - 문자열 내 마음대로 정렬하기 문자열로 구성된 리스트 strings와, 정수 n이 주어졌을 때, 각 문자열의 인덱스 n번째 글자를 기준으로 오름차순 정렬하려 합니다. 예를 들어 strings가 ["sun", "bed", "car"]이고 n이 1이면 각 단어의 인덱 programmers.co.kr 정답 import java.util.*; import java.util.stream.*; class Solution { public String[] solution(String[] strings, int n) { return Arrays.stream(strings) .sorted((s1, s2) -> { if(s1.charAt(n) == s2.charA..
-
프로그래머스 - 나누어 떨어지는 숫자 배열 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:48
문제 링크 : 나누어 떨어지는 숫자 배열 코딩테스트 연습 - 나누어 떨어지는 숫자 배열 array의 각 element 중 divisor로 나누어 떨어지는 값을 오름차순으로 정렬한 배열을 반환하는 함수, solution을 작성해주세요. divisor로 나누어 떨어지는 element가 하나도 없다면 배열에 -1을 담아 반환하 programmers.co.kr 정답 import java.util.*; import java.util.stream.*; class Solution { public int[] solution(int[] arr, int divisor) { List iList = new ArrayList(); for(int i : arr){ if(i % divisor == 0){ iList.add(i); ..