📌Back-End/프로그래머스

Java 프로그래머스 코딩테스트 입문 Day 19 문자열, 배열, 조건문

구 일 2024. 7. 12. 19:30
728x90
반응형

 

7의 개수

프로그래머스 7의 개수 자바

 

 

/**
 * 코딩테스트 입문 Day 19 문자열, 배열, 조건문
 * 7의 개수
 */
public class Main1 {
    public static void main(String[] args) {
        System.out.println(solution(new int[]{7, 77, 17}));
        System.out.println(solution(new int[]{10, 29}));
    }

    public static int solution(int[] array) {
        int answer = 0;

        for (int num : array) {
            String[] strArr = String.valueOf(num).split("");

            for (String s : strArr) {
                if (s.contains("7")) {
                    answer++;
                }
            }
        }

        return answer;
    }
}

 

 

잘라서 배열로 저장하기

프로그래머스 잘라서 배열로 저장하기 자바

 

 

/**
 * 코딩테스트 입문 Day 19 문자열, 배열, 조건문
 * 잘라서 배열로 저장하기
 */
public class Main2 {
    public static void main(String[] args) {
        String[] result = solution("abc1Addfggg4556b", 6);
        for (String s : result) {
            System.out.print(s + " ");
        }
        System.out.println();

        result = solution("abcdef123", 3);
        for (String s : result) {
            System.out.print(s + " ");
        }
        System.out.println();
    }

    public static String[] solution(String my_str, int n) {
        int size = (my_str.length() % n) == 0 ? my_str.length() / n : (my_str.length() / n) + 1;
        String[] answer = new String[size];
        int idx = 0;

        for (int i = 0; i < answer.length; i++) {
            if (idx + n < my_str.length()) {
                answer[i] = my_str.substring(idx, idx + n);
            } else {
                answer[i] = my_str.substring(idx);
            }

            idx += n;
        }

        return answer;
    }
}

 

 

중복된 숫자 개수

프로그래머스 중복된 숫자 개수 자바

 

 

import java.util.Arrays;

/**
 * 코딩테스트 입문 Day 19 문자열, 배열, 조건문
 * 중복된 숫자 개수
 */
public class Main3 {
    public static void main(String[] args) {
        System.out.println(solution(new int[]{1, 1, 2, 3, 4, 5}, 1));
        System.out.println(solution(new int[]{0, 2, 3, 4}, 1));
    }

    public static int solution(int[] array, int n) {
        return (int) Arrays.stream(array).filter(e -> e == n).count();
    }
}

 

 

머쓱이보다 키 큰 사람

프로그래머스 머쓱이보다 키 큰 사람 자바

 

 

import java.util.Arrays;

/**
 * 코딩테스트 입문 Day 19 문자열, 배열, 조건문
 * 머쓱이보다 키 큰 사람
 */
public class Main4 {
    public static void main(String[] args) {
        System.out.println(solution(new int[]{149, 180, 192, 170}, 167));
        System.out.println(solution(new int[]{180, 120, 140}, 190));
    }

    public static int solution(int[] array, int height) {
        return (int) Arrays.stream(array).filter(e -> e > height).count();
    }
}

 

 

 

728x90
반응형