본문 바로가기

프로그래머스 - 문자열 내 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[..
프로그래머스 - 모의고사 실패한 코드class Solution { public int[] solution(int[] answers) { // int[] answers = {1,3,2,4,2}; int[] cnt = new int[3]; int[] answer = {}; int length = answers.length; int[][] arr = new int[3][length]; int std; int[] pattern1 = {1, 2, 3, 4, 5}; int[] pattern2 = {1, 3, 4, 5, 2}; int[] pattern3 = {3, 1, 2, 4, 5}; /** * 수포자 3명 배열 초기화 */ for (int i = 0; i < arr.length; i++) { if (i == 0) { for (int j ..
프로그래머스 - 짝수와 홀수 class Solution { public String solution(int num) { String answer = ""; answer = num % 2 == 0 ? "Even" : "Odd"; return answer; } }