수박수박수박수박수 ~~~
·
코딩테스트/프로그래머스 Lv1
문제 이름이 좀 킹받는다. class Solution { public String solution(int n) { String answer = ""; String s = ""; for(int i = 0; i
나누어 떨어지는 숫자 배열
·
코딩테스트/프로그래머스 Lv1
class Solution { public int[] solution(int[] arr, int divisor) { int[] answer = {}; int count = 0; for(int i = 0; i < arr.length; i++){ if(arr[i] % divisor == 0){ count++; answer = new int[count]; } } if(count == 0){ answer = new int[1]; answer[0] = -1; } count = 0; for(int i = 0; i < arr.length; i++){ if(arr[i] % divisor == 0){ answer[count] = arr[i]; count++; } } int temp = 0; for(int i = 0; i..
두 정수 사이의 합
·
코딩테스트/프로그래머스 Lv1
class Solution { public long solution(int a, int b) { long answer = 0; if(b >= a){ for(int i = a; i b){ for(int i = b; i
문자열 내 p와 y의 갯수
·
코딩테스트/프로그래머스 Lv1
class Solution { boolean solution(String s) { boolean answer = true; int pcount = 0; int ccount = 0; for(int i = 0; i < s.length(); i++){ if(s.toUpperCase().charAt(i) == 80){ pcount++; }else if(s.toUpperCase().charAt(i) == 89){ ccount++; } } if(pcount != ccount){ answer = false; } return answer; } } int pcount = 0;와 int ccount = 0; 'P'와 'Y'의 갯수를 세기 위한 변수 pcount와 ccount를 선언하고 0으로 초기화한다. s.toUpper..
x만큼 간격이 있는 n개의 숫자
·
코딩테스트/프로그래머스 Lv1
class Solution { public long[] solution(int x, int n) { long[] answer = {}; answer = new long[n]; long su = 0; for(int i = 0; i < n; i++){ su = su + (long)(x); answer[i] = su; } return answer; } } n개 만큼의 배열 데이터 할당하고 반복문을 통해, 현재 su 값에 x를 더한다. su 변수는 등차수열의 항을 나타내고 x는 공차를 나타낸다. (long) 캐스팅을 통해 정수형 연산에서 오버플로우를 방지한다. answer[i] = su; 현재 항의 값을 answer 배열의 i번째 요소에 저장한다. 출처: 프로그래머스 코딩 테스트 연습 https://progra..
문자열을 정수로 바꾸기
·
코딩테스트/프로그래머스 Lv1
class Solution { public int solution(String s) { int answer = 0; answer = Integer.parseInt(s); System.out.println(answer); return answer; } } Integer.parseInt() 는 부호를 포함하고 있으면 문자열을 양수 혹은 음수의 정수형 타입으로 변환해 준다. 출처: 프로그래머스 코딩 테스트 연습 https://programmers.co.kr/learn/challenges