프로그래머스 Lv.0 0 떼기
개발자가 되기까지 (2023.08.16~2024.04.15)/[Algorithm] Programmers ver.Java2024. 1. 10. 23:35프로그래머스 Lv.0 0 떼기

class Solution { public String solution(String n_str) { String answer = ""; int idx = 1; if(n_str.charAt(0) == '0') { while(n_str.charAt(idx++) == '0') { if(n_str.charAt(idx) != '0') { break; } } answer += n_str.substring(idx); } else { answer += n_str.substring(0); } return answer; } } 샘플 테스트는 통과가 되는데 코드를 제출하니 test 3번, test 5번에서 실패했다. 왜 안되는 걸까, 테스트도 입출력을 알려주면 좋으련만.. 그래서 그냥 정규표현식을 사용했다. class So..

프로그래머스 Lv.0 콜라츠 수열 만들기
개발자가 되기까지 (2023.08.16~2024.04.15)/[Algorithm] Programmers ver.Java2024. 1. 10. 20:03프로그래머스 Lv.0 콜라츠 수열 만들기

class Solution { public int[] solution(int n) { String str = String.valueOf(n) + ","; int len = 0; while ( n > 1) { if(n % 2 == 0) { n = n / 2; str += String.valueOf(n) + ","; } else { n = 3 * n + 1; str += String.valueOf(n) + ","; } len++; } String[] strArr = (str.split(",")); int[] answer = new int [strArr.length]; int idx = 0; for(String s : strArr) { answer[idx++] = Integer.parseInt(s); } re..

프로그래머스 Lv.0 직각삼각형 출력하기
개발자가 되기까지 (2023.08.16~2024.04.15)/[Algorithm] Programmers ver.Java2024. 1. 10. 13:35프로그래머스 Lv.0 직각삼각형 출력하기

import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i = 1 ; i

프로그래머스 Lv.0 5명씩
개발자가 되기까지 (2023.08.16~2024.04.15)/[Algorithm] Programmers ver.Java2024. 1. 10. 12:30프로그래머스 Lv.0 5명씩

class Solution { public String[] solution(String[] names) { int len = (int) Math.ceil(names.length / (double) 5); String[] answer = new String [len]; int idx = 0; for(int i = 0; i < names.length; i++) { if(i == 0 || i % 5 == 0) { answer[idx++] = names[i]; } } return answer; } }

프로그래머스 Lv.0 가장 큰 수 찾기
개발자가 되기까지 (2023.08.16~2024.04.15)/[Algorithm] Programmers ver.Java2024. 1. 10. 12:10프로그래머스 Lv.0 가장 큰 수 찾기

class Solution { public int[] solution(int[] array) { int[] answer = new int [2]; for(int i = 0; i < array.length; i++) { for(int j = i+1; j array[j]) { answer[0] = array[i]; answer[1] = i; } } } return answer; } } 예제 테스트는 모두 통과했는데 채점에서 test2만 통과에 실패했다. 제한사항을 다시 읽어보니 array 길이가 최소 1이라고 되어있어서 array.length가 1일 때의 조건을 추가해 보았다. class Solution { public int[] solution(int[] array) { int[] answer = new ..

728x90
반응형
image