본문 바로가기

프로그래머스

프로그래머스 - x만큼 간격이 있는 n개의 숫자 내 코드 - 오답class Solution { public long[] solution(int x, int n) { long[] answer = {}; answer = new long[n]; for (int i = 0; i < n; i++) { /** * 여기서 answer[i]에는 int가 들어감 * * 따라서 만약, * x * (i + 1)이 int 범위를 초과한다면 엉뚱한 값이 들어감 */ answer[i] = x * (i + 1); } return answer; } } 문제에서 -10,000,000
프로그래머스 - 나누어 떨어지는 숫자 배열 내 코드import java.util.ArrayList; import java.util.Collections; import java.util.List; class Solution { public int[] solution(int[] arr, int divisor) { int[] answer = {}; List list = new ArrayList(); for (int num : arr) { if (num % divisor == 0) { list.add(num); } } if (list.size() == 0) { list.add(-1); } else { Collections.sort(list); } answer = new int[list.size()]; for (int i = 0; i < answer.le..
프로그래머스 - 문자열 내 p와 y의 개수 내 코드class Solution { boolean solution(String s) { boolean answer = true; int pCnt = 0; int yCnt = 0; char c; for (int i = 0; i < s.length(); i ++) { c = s.charAt(i); if (c == 'p' || c == 'P') { pCnt++; } else if (c == 'y' || c == 'Y') { yCnt++; } } answer = pCnt == yCnt ? true : false; return answer; } } 다른 방법 - s.toLowerCase();
프로그래머스 - 가운데 글자 가져오기 class Solution { public String solution(String s) { String answer = ""; if (s.length() % 2 != 0) { answer = answer + s.charAt(s.length() / 2); } else { answer = answer + s.charAt(s.length() / 2 - 1) + s.charAt(s.length() / 2); } return answer; } }
프로그래머스 - 문자열을 정수로 바꾸기 class Solution { public int solution(String s) { int answer = 0; char firstChar = s.charAt(0); answer = sumTotal(firstChar, s); return answer; } /** * 제곱수 구하는 메서드 * @param num * @param n * @return */ public int powPow(int num, int n) { int i = 0; int result = num; while (i < n) { result *= 10; i++; } return result; } /** * answer 구하는 메서드 * @param firstChar * @param s * @return */ public int sumT..
프로그래머스 - 시저 암호 class Solution { public String solution(String s, int n) { String answer = ""; char c; char newC; for (int i = 0; i = 65 && c 90) { newC -= 26; } answer += newC; /** * 소문자 */ } else if (c >= 97 && c 122) { newC -= 26; } answer += newC; /** * 공백 */ } else if (c == 32) { answer += c; } } return answer; } } 딱히 획기적으로 푼 사람은 없었다
프로그래머스 - 문자열 다루기 기본 class Solution { public boolean solution(String s) { boolean answer = true; char c; int cToI; if (s.length() == 4 || s.length() == 6) { for (int i = 0; i 57) { answer = false; break; } } } else { answer = false; } return answer; } }
프로그래머스 - K번째수 import java.util.Arrays; class Solution { public int[] solution(int[] array, int[][] commands) { int[] answer = {}; answer = new int[commands.length]; int startIdx; int endIdx; int tmpLength; int n; int[] tmpArr; int idx; for (int i = 0; i < commands.length; i++) { startIdx = commands[i][0] - 1; endIdx = commands[i][1] - 1; n = commands[i][2]; tmpLength = endIdx - startIdx + 1; tmpArr = new int[..