본문 바로가기
반응형

프로그래머스143

[프로그래머스 - JAVA] 제일 작은 수 제거하기 내 풀이 import java.util.*; class Solution { public int[] solution(int[] arr) { int[] answer = {}; // arr의 크기가 1이면 비교할 값이 없기 때문에 -1 return if(arr.length == 1){ answer = new int[1]; answer[0] = -1; }else { List check = new ArrayList(); // 가장 작은 수를 제거하기위해 배열값 -1 // 가장 작은 같은 작은 수가 여러개 일 수도 있는데 글에는 그런 내용이 우선 없음 answer = new int[arr.length-1]; for (int a : arr){ check.add(a); } // 오름차순으로 한번 정렬해주고 다시 내림차.. 2023. 3. 28.
[프로그래머스 - JAVA] 음양 더하기 내 풀이 class Solution { public int solution(int[] absolutes, boolean[] signs) { int answer = 123456789; answer = 0; // absolutes와 signs 의 개수는 같으니 그대로 + , - 해주면 된다 for (int i = 0; i < absolutes.length; i++) { if (signs[i] == true){ answer += absolutes[i]; }else { answer -= absolutes[i]; } } return answer; } } 다른 사람 풀이 class Solution { public int solution(int[] absolutes, boolean[] signs) { int answ.. 2023. 3. 28.
[프로그래머스 - JAVA] 핸드폰 번호 가리기 내 풀이 class Solution { public String solution(String phone_number) { String answer = ""; String[] check = phone_number.split(""); for (int i = 0; i < check.length-4; i++) { check[i] = "*"; } for (String a : check){ answer += a; } return answer; } } 다른 사람 풀이 class Solution { public String solution(String phone_number) { return phone_number.replaceAll(".(?=.{4})", "*"); } } 2023. 3. 27.
[프로그래머스 - JAVA] 서울에서 김서방 찾기 내 풀이 class Solution { public String solution(String[] seoul) { String answer = ""; for(int i = 0; i 2023. 3. 27.
[프로그래머스 - JAVA] 콜라츠 추측 내 풀이 class Solution { public int solution(int num) { int answer = 0; // 숫자가 너무 커지면 int 형에서 오버플로우가 일어난다 long check = num; for (int i = 0; i 1){ answer = -1; } return answer; } } if의 난발이다.... 다른 사람 풀이 class Solution { public .. 2023. 3. 27.
[프로그래머스 - JAVA] 두 정수 사이의 합 내 풀이 class Solution { public long solution(int a, int b) { long answer = 0; if (a < b){ for (int i = a; i 2023. 3. 26.
반응형