[프로그래머스, 181867] x 사이의 개수 (java)
Problem Solving/Programmers

[프로그래머스, 181867] x 사이의 개수 (java)

728x90

https://school.programmers.co.kr/learn/courses/30/lessons/181867

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

728x90

메모리: 79.6 MB, 시간: 7.32 ms

사용 알고리즘: 구현

import java.util.*;

class Solution {
    public int[] solution(String myString) {
        
        // 답을 임시로 담아둘 리스트
        List<Integer> list = new ArrayList<>();
        
        int idx = 0, count;
        while(idx < myString.length()) {
            count = 0;
            
            while(idx < myString.length() && myString.charAt(idx) != 'x') {
                idx++;
                count++;
            }
            
            list.add(count);
            idx++;
        }
        // 마지막이 "x"로 끝난 경우
        if(myString.charAt(myString.length() - 1) == 'x') list.add(0);
        
        // 리스트 -> 배열
        int[] answer = new int[list.size()];
        for(int i = 0; i < list.size(); i++) answer[i] = list.get(i);
        return answer;
    }
}
728x90