Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

forDevLife

[프로그래머스] 튜플 본문

알고리즘

[프로그래머스] 튜플

JH_Lucid 2021. 7. 8. 19:50

String 분리에 대해서 더 깊게 공부하자.

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class Solution {
    public int[] solution(String s) {

        int[] answer = { };

        String[] arr = s.substring(2, s.length() - 2).split("},\\{");
        Arrays.sort(arr, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length() - o2.length();
            }
        });

        ArrayList<Integer> list = new ArrayList<>();
        for (String tmp : arr) {
            String[] var = tmp.split(",");

            for(int i=0; i<var.length; i++) {
                int num = Integer.parseInt(var[i]);
                if(!list.contains(num)) {
                    list.add(num);
                }
            }
        }

        answer = new int[list.size()];
        for(int i=0; i<list.size(); i++) {
            answer[i] = list.get(i);
            System.out.println(answer[i]);
        }


        return answer;
    }

    @Test
    public void 정답() {
        String s = "{{4,2,3},{3},{2,3,4,1},{2,3}}";

        solution(s);
    }
}
Comments