반응형 java136 [프로그래머스 - JAVA] 같은 숫자는 싫어 내 풀이 import java.util.*; public class Solution { public int[] solution(int []arr) { int[] answer = {}; List list = new ArrayList(); list.add(arr[0]); int check = arr[0]; for (int i = 1; i < arr.length; i++) { if(check != arr[i]){ list.add(arr[i]); check = arr[i]; } } answer = new int[list.size()]; answer = list.stream().mapToInt(Integer::intValue).toArray(); // [실행] 버튼을 누르면 출력 값을 볼 수 있습니다. Syste.. 2023. 3. 30. [프로그래머스 - JAVA] 직사각형 별찍기 내 풀이 import java.util.Scanner; class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); // b가 세로의 길이기 때문에 먼저 돌아야한다 for (int i = 0; i < b; i++) { // a가 가로의 길이기 때문에 세로가 한번 돌때 가로가 한줄 도는식 for (int j = 0; j < a; j++) { System.out.print("*"); } System.out.println(); } // System.out.println(a + b); } } 다른 사람 풀이 import jav.. 2023. 3. 29. [프로그래머스 - JAVA] 행렬의 덧셈 내 풀이 class Solution { public int[][] solution(int[][] arr1, int[][] arr2) { int[][] answer = new int[arr1.length][arr1[0].length]; for(int i=0; i 2023. 3. 29. [프로그래머스 - JAVA] 부족한 금액 계산하기 입출력 예 price money count result 3 20 4 10 입출력 예 설명 이용금액이 3인 놀이기구를 4번 타고 싶은 고객이 현재 가진 금액이 20이라면, 총 필요한 놀이기구의 이용 금액은 30 (= 3+6+9+12) 이 되어 10만큼 부족하므로 10을 return 합니다. 내 풀이 class Solution { public long solution(int price, int money, int count) { long answer = -1; long sum = 0; for(int i = 1; i sum){ answer = 0; }else{ answer = sum - money; } return answer; } } 다른 사람 풀이 class Solution { public long sol.. 2023. 3. 29. [프로그래머스 - JAVA] 문자열 다루기 기본 내 풀이 class Solution { public boolean solution(String s) { boolean answer = true; int a = 0; if(s.length() == 4 || s.length() == 6){ try { a = Integer.parseInt(s); }catch (Exception e){ answer = false; } } return answer; } } String.matches 로 풀어보려다가 잘 안돼서 다른분 풀이를 참고해서 풀어보았다 위의 코드로는 answer를 return 시키려고해서 그런지 테스트케이스에서 많은 부분에서 실패하였다 터지는 부분을 이용하다보니 내가 생각하는대로 return answer 가 동작하지 않는거 같다. NumberFormatEx.. 2023. 3. 29. [프로그래머스 - JAVA] 문자열 내림차순으로 배치하기 내 풀이import java.util.Arrays;class Solution { public String solution(String s) { String answer = ""; String[] list = s.split(""); // 배열 오름차순으로 정리 Arrays.sort(list); StringBuilder sb = new StringBuilder(); for (String a : list) { sb.append(a); } // 글자 내림차순으로 변경 answer = sb.reverse().toString(); retu.. 2023. 3. 29. 이전 1 ··· 7 8 9 10 11 12 13 ··· 23 다음 반응형