본문 바로가기

Coding Test/프로그래머스

(138)
[프로그래머스/자바] 콜라츠 추측 class Solution { public int solution(long num) { if (num == 1) return 0; int count = 0; while (count < 500) { num = num % 2 == 0 ? num /= 2 : num * 3 + 1; count++; if (num == 1) break; } return count != 500 ? count : -1; } }
[프로그래머스/자바] 문자열 내림차순으로 배치하기 import java.util.Arrays; class Solution { public String solution(String s) { char[] arr = s.toCharArray(); Arrays.sort(arr); return new StringBuilder(new String(arr)).reverse().toString(); } }
[프로그래머스/자바] 행렬의 덧셈 class Solution { public int[][] solution(int[][] arr1, int[][] arr2) { int[][] answer = arr1; for (int i = 0; i < arr1.length; i++) { for (int j = 0; j < arr1[0].length; j++) { answer[i][j] += arr2[i][j]; } } return answer; } }
[프로그래머스/자바] 같은 숫자는 싫어 import java.util.Stack; public class Solution { public int[] solution(int[] arr) { if (arr.length == 1) return arr; Stack stack = new Stack(); stack.push(arr[0]); for (int i = 1; i = 0; i--) { answer[i] = stack.pop(); } return answer; } }
[프로그래머스/자바] 수박수박수박수박수박수? class Solution { public String solution(int n) { StringBuilder sb = new StringBuilder(); for (int i=0; i
[프로그래머스/자바] 문자열 다루기 기본 풀이1) class Solution { public boolean solution(String s) { if (s.length() != 4 && s.length() != 6) return false; char[] arr = s.toCharArray(); for (char c : arr) { if ((c - '0') > 9) return false; } return true; } } 문자열 s의 길이가 4 혹은 6이라는 제한 조건을 못 봐서 한 번 실패했었다...ㅎㅎ 지문을 한 글자 한 글자 제대로 읽는 습관을 들여야겠다. 풀이2) class Solution { public boolean solution(String s) { if (s.length() != 4 && s.length() != 6) retur..
[프로그래머스/자바] 가운데 글자 가져오기 class Solution { public String solution(String s) { return s.length() % 2 == 0 ? s.substring(s.length() / 2 - 1, s.length() / 2 + 1) : s.substring(s.length() / 2, s.length() / 2 + 1); } }
[프로그래머스/자바] 정수 제곱근 판별 class Solution { public long solution(long n) { double sqrt = Math.sqrt(n); if ((int)sqrt == sqrt) { long num = (long)(sqrt + 1); return num * num; } else return -1; } } 참고용 다른 분 풀이 class Solution { public long solution(long n) { double i = Math.sqrt(n); return Math.floor(i) == i ? (long) Math.pow(i + 1, 2) : -1; } }