Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.06.25 - [PG] 내 마음대로 정렬하기 #94

Closed
suhyunsim opened this issue Jun 26, 2021 · 0 comments
Closed

21.06.25 - [PG] 내 마음대로 정렬하기 #94

suhyunsim opened this issue Jun 26, 2021 · 0 comments
Assignees
Labels
lv.1 프로그래머스 - level 1 성공 맞은 문제 정렬 정렬하기

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Jun 26, 2021

문제

핵심 아이디어

  • 직접 compareTo 구현

어려운 점, 실수

풀이

package main.java.com.poogle.PG.Q12915;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Solution {
    static String[] solution(String[] strings, int n) {
        List<Dictionary> list = new ArrayList<>();
        for (String string : strings) {
            list.add(new Dictionary(string.charAt(n), string));
        }
        Collections.sort(list);
        String[] answer = new String[strings.length];
        for (int i = 0; i < list.size(); i++) {
            answer[i] = list.get(i).string;
        }
        return answer;
    }

    public static void main(String[] args) {
        String[] arr = {"sun", "bed", "car"};
        System.out.println(Arrays.toString(solution(arr, 1)));
    }

    private static class Dictionary implements Comparable<Dictionary> {
        Character s;
        String string;

        public Dictionary(char s, String string) {
            this.s = s;
            this.string = string;
        }

        @Override
        public int compareTo(Dictionary dict) {
            if (this.s > dict.s) {
                return 1;
            } else if (this.s == dict.s) {
                return this.string.compareTo(dict.string);
            }
            return -1;
        }
    }
}
@suhyunsim suhyunsim added 성공 맞은 문제 정렬 정렬하기 lv.1 프로그래머스 - level 1 labels Jun 26, 2021
@suhyunsim suhyunsim added this to the 6월 4주 차 milestone Jun 26, 2021
@suhyunsim suhyunsim self-assigned this Jun 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lv.1 프로그래머스 - level 1 성공 맞은 문제 정렬 정렬하기
Projects
None yet
Development

No branches or pull requests

1 participant