Algorithms
-
프로그래머스 - 짝수와 홀수 [자바]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); ..
-
프로그래머스 - 문자열 내 p와 y의 개수 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:35
문제 링크 : 문자열 내 p와 y의 개수 코딩테스트 연습 - 문자열 내 p와 y의 개수 대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 programmers.co.kr 정답 import java.util.*; class Solution { boolean solution(String s) { boolean answer = true; s = s.toLowerCase(); s = s.replaceAll("[^py]", ""); int p = 0; int y = 0; for(int i=0; i 'P'== e).count()..
-
프로그래머스 - 문자열 내림차순으로 배치하기 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:31
문제 링크 : 문자열 내림차순으로 배치하기 코딩테스트 연습 - 문자열 내림차순으로 배치하기 문자열 s에 나타나는 문자를 큰것부터 작은 순으로 정렬해 새로운 문자열을 리턴하는 함수, solution을 완성해주세요. s는 영문 대소문자로만 구성되어 있으며, 대문자는 소문자보다 작은 것으로 programmers.co.kr 정답 import java.util.*; class Solution { public String solution(String s) { if(s.length() == 1){ return s; } String[] arr = s.split(""); Arrays.sort(arr); String str = ""; for(int i=arr.length-1; i>=0; i--){ str += arr[i]..
-
프로그래머스 - 문자열 다루기 기본 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:29
문제 링크 : 문자열 다루기 기본 코딩테스트 연습 - 문자열 다루기 기본 문자열 s의 길이가 4 혹은 6이고, 숫자로만 구성돼있는지 확인해주는 함수, solution을 완성하세요. 예를 들어 s가 "a234"이면 False를 리턴하고 "1234"라면 True를 리턴하면 됩니다. 제한 사항 s는 길이 1 programmers.co.kr 정답 class Solution { public boolean solution(String s) { if(s.length() == 4 || s.length() == 6){ s = s.replaceAll("\\d", ""); if(s.length() > 0){ return false; }else{ return true; } } return false; } } 분석 길이가 4 ..