Algorithms
-
프로그래머스 - 같은 숫자는 싫어 [자바]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
-
프로그래머스 - 시저 암호 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:12
문제 링크 : 시저 암호 코딩테스트 연습 - 시저 암호 어떤 문장의 각 알파벳을 일정한 거리만큼 밀어서 다른 알파벳으로 바꾸는 암호화 방식을 시저 암호라고 합니다. 예를 들어 "AB"는 1만큼 밀면 "BC"가 되고, 3만큼 밀면 "DE"가 됩니다. "z"는 1만큼 밀 programmers.co.kr 정답 class Solution { public String solution(String s, int n) { //소문자 97 ~ 122 //대문자 65 ~ 90 int idx = 0; char[] arr = s.toCharArray(); for(char ch : arr){ int a = (int)ch; if(a >= 65 && a 90){ a += n - 26; }else{ a += n; } }else if..
-
프로그래머스 - 약수의 합 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:11
문제 링크 : 약수의 합 코딩테스트 연습 - 약수의 합 정수 n을 입력받아 n의 약수를 모두 더한 값을 리턴하는 함수, solution을 완성해주세요. 제한 사항 n은 0 이상 3000이하인 정수입니다. 입출력 예 n return 12 28 5 6 입출력 예 설명 입출력 예 #1 12의 약수 programmers.co.kr 정답 class Solution { public int solution(int n) { int a = 0; for(int i=1; i
-
프로그래머스 - 이상한 문자 만들기 [자바]Algorithms/- 프로그래머스 2022. 2. 2. 11:09
문제 링크 : 이상한 문자 만들기 코딩테스트 연습 - 이상한 문자 만들기 문자열 s는 한 개 이상의 단어로 구성되어 있습니다. 각 단어는 하나 이상의 공백문자로 구분되어 있습니다. 각 단어의 짝수번째 알파벳은 대문자로, 홀수번째 알파벳은 소문자로 바꾼 문자열을 programmers.co.kr 정답 class Solution { public String solution(String s) { String[] arr = s.split(""); int idx = 0; for(int i=0; i