본문 바로가기
반응형

전체 글289

[프로그래머스 - JAVA] 배열 회전시키기 내 풀이 import java.util.Arrays; class Solution { public int[] solution(int[] numbers, String direction) { int[] answer = {}; answer = new int[numbers.length]; if(direction.equals("right")) { answer[0] = numbers[numbers.length - 1]; for (int i = 0; i < numbers.length - 1; i++) { answer[i + 1] = numbers[i]; } } else if (direction.equals("left")) { answer[numbers.length -1] = numbers[0]; for (int i =.. 2023. 3. 10.
[프로그래머스 - JAVA] 인덱스 바꾸기 내 풀이 class Solution { public String solution(String my_string, int num1, int num2) { String answer = ""; String[] split = my_string.split(""); for (int i = 0; i < split.length; i++) { if (i == num1){ answer += split[num2]; }else if(i == num2){ answer += split[num1]; }else { answer += split[i]; } } return answer; } } 다른 사람 풀이 import java.util.Arrays; import java.util.Collections; import java.util.. 2023. 3. 10.
[프로그래머스 - JAVA] 최댓값 만들기(2) 내 풀이( 제출 테스트 1개 실패) class Solution { public int solution(int[] numbers) { int answer = 0; int result = 0; for(int i = 0; i < numbers.length; i++){ for(int j = i+1; j < numbers.length; j++){ result = numbers[i] * numbers[j]; if(answer < result){ answer = result; } } } return answer; } } 변경 풀이 import java.util.Arrays; class Solution { public int solution(int[] numbers) { int answer = 0; int result.. 2023. 3. 10.
[프로그래머스 - JAVA] 직각삼각형 출력하기 내 풀이 import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String star = "*"; for (int i = 0; i < n; i++) { for(int j = 0; j 2023. 3. 10.
[프로그래머스 - JAVA] 주사위의 개수 내 풀이 class Solution { public int solution(int[] box, int n) { int answer = 0; answer = (box[0] / n) * (box[1] / n) * (box[2] / n); return answer; } } 다른 사람들의 풀이도 비슷하다. 코드 자체는 별거 없는데 순간 정육면체 공식이 뭐였드라...하고 멈칫하게 되더라... 이걸로 시간을 뺐겼다. 2023. 3. 10.
[프로그래머스 - JAVA] n의 배수 고르기 내 풀이 import java.util.Arrays; class Solution { public int[] solution(int n, int[] numlist) { int[] answer = {}; answer = Arrays.stream(numlist).filter(value -> value % n == 0).toArray(); return answer; } } answer의 배열 크기를 초기화 시켜줘야하는데 배수의 값을 따로 구해서 초기화 시켜주고 다시 for문을 돌려서 answer에 넣기에는 코드를 반복해야하다보니 Arrays.stream을 이용하였다. 코딩테스트를 하면서 자주 사용하게 되었는데 매우 유용!! int[] answer의 타입을 ArrayList로 변경해서 add해도 되지만 문제의 틀.. 2023. 3. 10.
반응형