Algorithms/- 프로그래머스
-
프로그래머스 - 문자열 내 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 ..
-
프로그래머스 - 같은 숫자는 싫어 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:28
문제 링크 : 같은 숫자는 싫어 코딩테스트 연습 - 같은 숫자는 싫어 배열 arr가 주어집니다. 배열 arr의 각 원소는 숫자 0부터 9까지로 이루어져 있습니다. 이때, 배열 arr에서 연속적으로 나타나는 숫자는 하나만 남기고 전부 제거하려고 합니다. 단, 제거된 후 남은 programmers.co.kr 정답 import java.util.*; import java.util.stream.*; public class Solution { public int[] solution(int []arr) { List list = new ArrayList(); int a = -1; for(int n : arr){ if(a != n){ list.add(n); a = n; } } return list.stream().ma..
-
프로그래머스 - 서울에서 김서방 찾기 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:25
문제 링크 : 서울에서 김서방 찾기 코딩테스트 연습 - 서울에서 김서방 찾기 String형 배열 seoul의 element중 "Kim"의 위치 x를 찾아, "김서방은 x에 있다"는 String을 반환하는 함수, solution을 완성하세요. seoul에 "Kim"은 오직 한 번만 나타나며 잘못된 값이 입력되는 경우는 없습니 programmers.co.kr 정답 class Solution { public String solution(String[] seoul) { int x = 0; for(int i=0; i
-
프로그래머스 - 소수 찾기 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:24
문제 링크 : 소수 찾기 코딩테스트 연습 - 소수 찾기 1부터 입력받은 숫자 n 사이에 있는 소수의 개수를 반환하는 함수, solution을 만들어 보세요. 소수는 1과 자기 자신으로만 나누어지는 수를 의미합니다. (1은 소수가 아닙니다.) 제한 조건 n은 2이상 programmers.co.kr 정답 class Solution { public int solution(int n) { int answer = 0; for(int i=2; i
-
프로그래머스 - 수박수박수박수박수박수 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:23
문제 링크 : 수박수박수박수박수박수 코딩테스트 연습 - 수박수박수박수박수박수? 길이가 n이고, "수박수박수박수...."와 같은 패턴을 유지하는 문자열을 리턴하는 함수, solution을 완성하세요. 예를들어 n이 4이면 "수박수박"을 리턴하고 3이라면 "수박수"를 리턴하면 됩니다. 제한 programmers.co.kr 정답 class Solution { public String solution(int n) { StringBuilder sb = new StringBuilder(); for(int i=0; i
-
프로그래머스 - 문자열을 정수로 바꾸기 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:22
문제 링크 : 문자열을 정수로 바꾸기 코딩테스트 연습 - 문자열을 정수로 바꾸기 문자열 s를 숫자로 변환한 결과를 반환하는 함수, solution을 완성하세요. 제한 조건 s의 길이는 1 이상 5이하입니다. s의 맨앞에는 부호(+, -)가 올 수 있습니다. s는 부호와 숫자로만 이루어져있습니 programmers.co.kr 정답 class Solution { public int solution(String s) { return Integer.valueOf(s); } } 분석 String -> Integer