๊ฐœ๋ฐœ์ผ์ง€

[Java] ์ •์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ ๋ณธ๋ฌธ

Algorithm

[Java] ์ •์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ

O'mil 2024. 5. 8. 14:49
728x90

๐Ÿ”— ๋งํฌ

์ •์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ

 

๐Ÿ“Œ ๋ฌธ์ œ ์„ค๋ช…

  • ์ •์ˆ˜ n์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์Œ.
    n์˜ ์ž๋ฆฟ์ˆ˜๋ฅผ ํฐ๊ฒƒ๋ถ€ํ„ฐ ์ž‘์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ์ƒˆ๋กœ์šด ์ •์ˆ˜๋ฅผ ๋ฆฌํ„ด.

 

๐Ÿ‘€ ์˜ˆ์ œ

 

 

โœ๏ธ ํ’€์ด ์ˆœ์„œ

  1. ๋ฐฐ์—ด์— ๋„ฃ๊ธฐ
  2. ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
  3. ์ถœ๋ ฅ

 

๐Ÿ’ป ์ฝ”๋“œ

import java.util.*;

class Solution {
    public long solution(long n) {
        
        String s = n + "";
        int num = Integer.parseInt(s);
        Integer[] arr = new Integer[s.length()];
        
        for (int i = 0; i < s.length(); i++) {
            arr[i] = num % 10;
            num /= 10;
        }
        
        Arrays.sort(arr, Collections.reverseOrder());
        
        String answer = "";
        for (int i = 0; i < arr.length; i++) {
            answer += arr[i];
        }
        
        return Long.parseLong(answer);
    }
}

 

 

 

๐Ÿ’ก ๊ฒฐ๊ณผ

  • ์ •ํ™•์„ฑ: 43.8
  • ํ•ฉ๊ณ„: 43.8 / 100.0

 


๐Ÿ”’ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

import java.util.*;
 
class Solution {
	public long solution(long n) {
		String[] arr = String.valueOf(n).split("");
		Arrays.sort(arr); // ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
 
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < arr.length; i++) {
			sb.append(arr[i]); // ๋ฌธ์ž ํ•˜๋‚˜์”ฉ ํ•ฉ์น˜๊ธฐ
		}
        
		return Long.parseLong(sb.reverse().toString());
	}
}
  1. sort์ •๋ ฌ์„ ์œ„ํ•ด ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๋ฐฐ์—ด String arr์„ ์ƒ์„ฑํ•จ.
    • String.valueof(n): ์ •์ˆ˜ → ๋ฌธ์ž์—ด ๋ณ€ํ™˜
    • split(""): "" ๋‹จ์œ„๋กœ ๋ฌธ์ž๋ฅผ ์ชผ๊ฐฌ
    • → arr: ["1", "1", "8", "3", "7", "2"]
  2. ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
  3. StringBuilder ์„ ์–ธ (๋ฌธ์ž์—ด ๋”ํ•˜๊ธฐ ์œ„ํ•ด)
  4. append ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํ•˜๋‚˜์”ฉ ํ•ฉ์น˜๊ธฐ
  5. reverse() ๋ฉ”์„œ๋“œ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐ”๊พธ๊ณ , toString ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
  6. Long.parseLong()์œผ๋กœ longํ˜•์œผ๋กœ ๋ณ€ํ™˜

 


๐Ÿ’ป ์ฝ”๋“œ

1. sort()์—์„œ Collections.reverseOrder() ์‚ฌ์šฉํ•ด์„œ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ธฐ

import java.util.*;

class Solution {
    public long solution(long n) {
        
        String[] arr = String.valueOf(n).split("");
        Arrays.sort(arr, Collections.reverseOrder());
        
        StringBuilder sb = new StringBuilder();
        for (String s: arr) sb.append(s);
        
        return Long.parseLong(sb.toString());
    }
}

 

 

2. return์—์„œ reverse() ์‚ฌ์šฉํ•ด์„œ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ธฐ

import java.util.*;

class Solution {
    public long solution(long n) {
        
        String[] arr = String.valueOf(n).split("");
        Arrays.sort(arr);
        
        StringBuilder sb = new StringBuilder();
        for (String s: arr) sb.append(s);
        
        return Long.parseLong(sb.reverse().toString());
    }
}

 

 


๐Ÿ“šReference

 

(Java/์ž๋ฐ”) ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv1 - ์ •์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ

728x90
Comments