📌Back-End/프로그래머스

Java 프로그래머스 코딩테스트 입문 Day 13 문자열, 배열, 사칙연산, 수학, 조건문

구 일 2024. 7. 6. 12:18
728x90
반응형

 

컨트롤 제트

프로그래머스 컨트롤 제트 자바

 

 

import java.util.LinkedList;

/**
 * 코딩테스트 입문 Day 13 문자열, 배열, 사칙연산, 수학, 조건문
 * 컨트롤 제트
 */
public class Main1 {
    public static void main(String[] args) {
        System.out.println(solution("1 2 Z 3"));
        System.out.println(solution("10 20 30 40"));
        System.out.println(solution("10 Z 20 Z 1"));
        System.out.println(solution("10 Z 20 Z"));
        System.out.println(solution("-1 -2 -3 Z"));
    }

    public static int solution(String s) {
        LinkedList<Integer> list = new LinkedList<>();
        String[] strArr = s.split(" ");

        for (String str : strArr) {
            if (str.equals("Z")) {
                list.removeLast();
            } else {
                list.add(Integer.parseInt(str));
            }
        }

        return list.stream().mapToInt(Integer::intValue).sum();
    }
}

 

 

배열 원소의 길이

프로그래머스 배열 원소의 길이 자바

 

 

/**
 * 코딩테스트 입문 Day 13 문자열, 배열, 사칙연산, 수학, 조건문
 * 배열 원소의 길이
 */
public class Main2 {
    public static void main(String[] args) {
        int[] result = solution(new String[]{"We", "are", "the", "world!"});
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();

        result = solution(new String[]{"I", "Love", "Programmers."});
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();
    }

    public static int[] solution(String[] strlist) {
        int[] answer = new int[strlist.length];

        for (int i = 0; i < strlist.length; i++) {
            answer[i] = strlist[i].length();
        }

        return answer;
    }
}

 

 

중복된 문자 제거

프로그래머스 중볻된 문자 제거 자바

 

 

/**
 * 코딩테스트 입문 Day 13 문자열, 배열, 사칙연산, 수학, 조건문
 * 중복된 문자 제거
 */
public class Main3 {
    public static void main(String[] args) {
        System.out.println(solution("people"));
        System.out.println(solution("We are the world"));
    }

    public static String solution(String my_string) {
        String answer = "";

        String[] strArr = my_string.split("");
        for (int i = 0; i < strArr.length; i++) {
            if (!answer.contains(strArr[i])) {
                answer += strArr[i];
            }
        }

        return answer;
    }
}

 

 

삼각형의 완성조건 (1)

프로그래머스 삼각형의 완성조건 (1) 자바

 

 

import java.util.Arrays;

/**
 * 코딩테스트 입문 Day 13 문자열, 배열, 사칙연산, 수학, 조건문
 * 삼각형의 완성조건 (1)
 */
public class Main4 {
    public static void main(String[] args) {
        System.out.println(solution(new int[]{1, 2, 3}));
        System.out.println(solution(new int[]{3, 6, 2}));
        System.out.println(solution(new int[]{199, 72, 222}));
    }

    public static int solution(int[] sides) {
        Arrays.sort(sides);

        return sides[2] < sides[1] + sides[0] ? 1 : 2;
    }
}

 

 

728x90
반응형