📌Back-End/프로그래머스

Java 프로그래머스 코딩테스트 입문 Day 18 문자열, 수학, 조건문, 정렬

구 일 2024. 7. 11. 15:47
728x90
반응형

 

문자열안에 문자열

프로그래머스 문자열안에 문자열 자바

 

 

/**
 * 코딩테스트 입문 Day 18 문자열, 수학, 조건문, 정렬
 * 문자열안에 문자열
 */
public class Main1 {
    public static void main(String[] args) {
        System.out.println(solution("ab6CDE443fgh22iJKlmn1o", "6CD"));
        System.out.println(solution("ppprrrogrammers", "pppp"));
        System.out.println(solution("AbcAbcA", "AAA"));
    }

    public static int solution(String str1, String str2) {
        return str1.contains(str2) ? 1 : 2;
    }
}

 

 

제곱수 판별하기

프로그래머스 제곱수 판별하기 자바

 

 

/**
 * 코딩테스트 입문 Day 18 문자열, 수학, 조건문, 정렬
 * 제곱수 판별하기
 */
public class Main2 {
    public static void main(String[] args) {
        System.out.println(solution(144));
        System.out.println(solution(976));
    }

    public static int solution(int n) {
        return Math.sqrt(n) % 1 == 0 ? 1 : 2;
    }
}

 

 

세균 증식

프로그래머스 세균 증식 자바

 

 

/**
 * 코딩테스트 입문 Day 18 문자열, 수학, 조건문, 정렬
 * 세균 증식
 */
public class Main3 {
    public static void main(String[] args) {
        System.out.println(solution(2, 10));
        System.out.println(solution(7, 15));
    }

    public static int solution(int n, int t) {
        return n * (int) Math.pow(2, t);
    }
}

 

 

문자열 정렬하기 (2)

프로그래머스 문자열 정렬하기 (2) 자바

 

 

import java.util.Comparator;
import java.util.LinkedList;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 코딩테스트 입문 Day 18 문자열, 수학, 조건문, 정렬
 * 문자열 정렬하기 (2)
 */
public class Main4 {
    public static void main(String[] args) {
        System.out.println(solution("Bcad"));
        System.out.println(solution("heLLo"));
        System.out.println(solution("Python"));
    }

    public static String solution(String my_string) {
        StringBuilder sb = new StringBuilder();
        String[] strArr = my_string.toLowerCase().split("");

        LinkedList<String> list= Stream.of(strArr).collect(Collectors.toCollection(LinkedList::new));
        list.sort(Comparator.naturalOrder());
        list.forEach(sb::append);

        return sb.toString();
    }
}

 

 

 

728x90
반응형