๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜-๋ฐฑ์ค€(์ •๋ ฌ)] ์ž๋ฐ” 1181๋ฒˆ ๋‹จ์–ด ์ •๋ ฌ ๋ฌธ์ œํ’€์ด. (Comparator์„ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌ ๊ธฐ์ค€ ๋ณ€๊ฒฝ)

by Wonit 2020. 2. 24.

๋ฌธ์ œ

 

 

์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ ์˜ˆ์ œ

 

๋ฌธ์ œ ์ดํ•ด

 

๋ฌธ์ œ์— ๋ฐ”๋กœ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ •๋ ฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค ๋ผ๋Š” ๋ง์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ฌธ์ œ๋Š” ์ •๋ ฌ ๋ฌธ์ œ๋กœ ๋ถ„๋ฅ˜ํ•˜์—ฌ ํ‘ผ๋‹ค.

ํ•˜์ง€๋งŒ ์ •๋ ฌ ๊ธฐ์ค€์ด ๋‘ ๊ฐ€์ง€๋กœ ๋‚˜๋‰œ๋‹ค.

 

  1. ๊ธธ์ด๊ฐ€ ์งง์€ ๊ฒƒ ๋ถ€ํ„ฐ
  2. ๊ธธ์ด๊ฐ€ ๊ฐ™์œผ๋ฉด ์‚ฌ์ „์ˆœ์œผ๋กœ
  3. ์ค‘๋ณต ์ œ๊ฑฐ

 

์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ์ •๋ ฌ์„ ํ•  ๋•Œ int์˜ ์˜ค๋ฆ„์ฐจ์ˆœ ํ˜น์€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์„ ํ–ˆ์ง€๋งŒ ์ด ๋ฌธ์ œ๋Š” ํŠน์ดํ•˜๊ฒŒ ๋ฌธ์ž์˜ ๊ธธ์ด๋ฅผ ์š”๊ตฌํ•˜๋ฏ€๋กœ ํ•ด๋‹น ์š”๊ตฌ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” Comparator ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•ด์„œ ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค.

comparator

comparator์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์‚ฌ์šฉ๋ฒ• ์„ ํ†ตํ•ด์„œ ์‚ฌ์šฉ๋ฒ•์„ ์ˆ™์ง€ํ•˜์‹œ๊ณ  ์˜ค๋Š”๊ฒƒ๋„ ๋ฐฉ๋ฒ•์ด์—์š” :)

๋ฌธ์ œ ํ•ด๊ฒฐ ์ ‘๊ทผ

  • ๊ธธ์ด๊ฐ€ ์งง์€ ์ˆœ์œผ๋กœ ์ •๋ ฌ ์„ ์ˆ˜ํ–‰ํ•  ๋•Œ๋Š” str.length() ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ˆ˜ํ–‰
  • ๊ธธ์ด๊ฐ€ ๊ฐ™์œผ๋ฉด ์‚ฌ์ „์ˆœ ์ •๋ ฌ ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ Arrays.sort(/String/)๊ฐ€ ์‚ฌ์ „์ˆœ ์ •๋ ฌ์„ ์ง€์›ํ•˜๋ฏ€๋กœ ๊ฐ™์€ ๊ธธ์ด๋ผ๋ฆฌ ๋ฌถ๋Š”๊ฒƒ์ด ๊ด€๊ฑด
  • ์ค‘๋ณต ์ œ๊ฑฐ๋Š” for ๋ฐ˜๋ณต์„ ์ˆ˜ํ–‰ํ•˜๋ฉฐ ๋ฐฐ์—ด์˜ ๋ ๊นŒ์ง€ ํ˜„์žฌ index์˜ ๊ฐ’๊ณผ ๋‹ค์Œ index์˜ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด ์ถœ๋ ฅ ์‹œ์— ์ถœ๋ ฅํ•˜์ง€ ์•Š๊ณ  ๋„˜์–ด๊ฐ€๊ธฐ

์†Œ์Šค์ฝ”๋“œ

 

๊ธธ์ด๊ฐ€ ์งง์€ ์ˆœ์œผ๋กœ ์ •๋ ฌ

// ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ ์งง์€์ˆœ ์ •๋ ฌ
        Arrays.sort(word, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if(o1.length() > o2.length()) return 1;
                else if(o1.length() == o2.length()) return 0;
                else return -1;
            }
        });

๋‘ ๊ฐ์ฒด์˜ ๋น„๊ต๋ฅผ ์œ„ํ•ด comparator์˜ compare ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ ํ•œ๋‹ค.
์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผํ•  ์ ์€ o1.length()๋กœ ๋‹จ์–ด์˜ ๊ธธ์ด๋กœ ๋น„๊ต๋ฅผ ํ–ˆ๋˜ ๊ฒƒ์ด๋‹ค.

 

๊ธธ์ด๊ฐ€ ๊ฐ™์œผ๋ฉด ์‚ฌ์ „์ˆœ ์ •๋ ฌ

for (int i = 0; i < n; i++) {
            int j;
            for (j = i+1; j < n; j++) {
                if(word[i].length() != word[j].length()) break;
            }
            Arrays.sort(word, i,j);
            i = j-1;
        }

 

์ค‘๋ณต ์ œ๊ฑฐ

// ์ค‘๋ณต ์ œ๊ฑฐ ํ›„ ์ถœ๋ ฅ
        System.out.println(word[0]);
        for (int i = 1; i < n; i++) {
            if(word[i-1].equals(word[i])) continue;

            System.out.println(word[i]);
        }

 

์ „์ฒด ์†Œ์Šค์ฝ”๋“œ

package algorithm.class04_Sort;

import java.util.*;

public class Prob06_WordSorting {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int n = input.nextInt();

        String[] word = new String[n];

        for (int i = 0; i < word.length; i++) {
            word[i] = input.next();
            word[i] = word[i].toLowerCase();
        }



        // ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ ์งง์€์ˆœ ์ •๋ ฌ
        Arrays.sort(word, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if(o1.length() > o2.length()) return 1;
                else if(o1.length() == o2.length()) return 0;
                else return -1;
            }
        });

        // ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™์„ ๋•Œ ์‚ฌ์ „์ˆœ ์ •๋ ฌ
        for (int i = 0; i < n; i++) {
            int j;
            for (j = i+1; j < n; j++) {
                if(word[i].length() != word[j].length()) break;
            }
            Arrays.sort(word, i,j);
            i = j-1;
        }

        // ์ค‘๋ณต ์ œ๊ฑฐ ํ›„ ์ถœ๋ ฅ
        System.out.println(word[0]);
        for (int i = 1; i < n; i++) {
            if(word[i-1].equals(word[i])) continue;

            System.out.println(word[i]);
        }

    }
}

๋Œ“๊ธ€