본문 바로가기

Coding Test/프로그래머스

(138)
[프로그래머스/자바] 짝수는 싫어요 import java.util.List; import java.util.ArrayList; class Solution { public int[] solution(int n) { List list = new ArrayList(); for(int i=1; i
[프로그래머스/자바] 홀짝에 따라 다른 값 반환하기 class Solution { public int solution(int n) { if(n % 2 == 1) return odd(n); return even(n); } private int odd(int n) { int sum = 0; for(int i=1; i
[프로그래머스/자바] 배열의 유사도 class Solution { public int solution(String[] s1, String[] s2) { int answer = 0; for(int i=0; i
[프로그래머스/자바] 피보나치 수 ❌ 재귀 함수를 이용한 실패 답안 ❌ class Solution { public int solution(int n) { return fibonacci(n) % 1234567; } private int fibonacci(int n) { if(n == 0) return 0; if(n == 1) return 1; return fibonacci(n - 1) + fibonacci(n - 2); } } 실행하니 테스트 케이스 7번부터 시간 초과가 떴다. n이 커지면서 int 범위를 넘어서는 값이 들어가기 때문인 것 같다. ✅ 통과된 답안 ✅ class Solution { public int solution(int n) { int a = 0; int b = 1; int sum = 0; for(int i=2; i
[프로그래머스/자바] n보다 커질 때까지 더하기 class Solution { public int solution(int[] numbers, int n) { int answer = 0; for(int num : numbers) { answer += num; if(answer > n) break; } return answer; } }
[프로그래머스/자바] 조건에 맞게 수열 변환하기 1 class Solution { public int[] solution(int[] arr) { for(int i=0; i= 50 && arr[i] % 2== 0) { arr[i] /= 2; } else if(arr[i] < 50 && arr[i] % 2 != 0) { arr[i] *= 2; } } return arr; } }
[프로그래머스/자바] 수 조작하기 1 class Solution { public int solution(int n, String control) { char[] array = control.toCharArray(); for(char c : array) { if(c == 'w') n++; else if(c == 's') n--; else if(c == 'd') n += 10; else n -= 10; } return n; } }
[프로그래머스/자바] n 번째 원소부터 import java.util.Arrays; class Solution { public int[] solution(int[] num_list, int n) { return Arrays.copyOfRange(num_list, n - 1, num_list.length); } }