๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/JAVA] ๊ฐ€์žฅ ํฐ ์ˆ˜ - ์ •๋ ฌ

by ynzu๐Ÿค 2022. 2. 20.
๋ฐ˜์‘ํ˜•

 

 

์ด ๋ฌธ์ œ๋„ ์‰ฝ๊ฒŒ ์ž˜ ํ’€๋ ธ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ณ , ๋ฌธ์ œ์˜ ์˜ˆ์ œ๊ฐ€ ๋‚ด๊ฐ€ ๋Œ€์ž…ํ•œ ์ˆซ์ž๋กœ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์ž˜ ๋‚˜์™”๋‹ค.

ํ•˜์ง€๋งŒ ํ…Œ์ŠคํŠธ 33.3 ์ ์„ ๋ฐ›์•˜๋‹ค..ใ…Žใ…Ž ์ด ์ •๋„๋ฉด ์˜ˆ์ œ๊ฐ€ ์šด ์ข‹๊ฒŒ ๋งž์€ ์ˆ˜์ค€..

  public String solution(int[] numbers) {
        String answer = "";
        
        Integer[]numbers2 = Arrays.stream(numbers).boxed().toArray(Integer[]::new); 
        Arrays.sort(numbers2, new Comparator<Integer>() {

			@Override
			public int compare(Integer o1, Integer o2) {
				
				
				if(o1.toString().charAt(0) == o2.toString().charAt(0)) {
					if(o1.toString().length() != o2.toString().length()) {
						
						
						if(o1.toString().length() > o2.toString().length()) {
							
							String temp = null;
							for(int i= 0; i<o1.toString().length()-1; i++) {
								if(i==0) {
									temp = o2.toString();
								}
								temp = temp + o2.toString();
							}
							return Integer.parseInt(temp) - o1;
						}else {
							
							String temp = null;
							for(int i= 0; i<o2.toString().length()-1; i++) {
								if(i==0) {
									temp = o1.toString();
								}
								temp = temp + o1.toString();
							}
							return o2 - Integer.parseInt(temp);
						}
					}
					return o2 - o1;
				}else {
					return o2.toString().charAt(0) - o1.toString().charAt(0);
				}
				
			}
		});

        
        	
        for(int num : numbers2) {
        	answer = answer + num;
		}
        
        return answer;
    }

 

๊ทธ๋ž˜์„œ ์ด๋ฒˆ์—” Integer Array๊ฐ€ ์•„๋‹Œ  String Array๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ Sortingํ•˜์˜€๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด 3, 34๋ฅผ ๋น„๊ตํ•  ๋•Œ "3"+"34" = "334"์™€ "34"+"3" = "343" ์„ ๋น„๊ตํ•˜์—ฌ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ๋˜๋„๋ก ์ฒ˜๋ฆฌํ–ˆ๋‹ค.

public String solution(int[] numbers) {
        String answer = "";
        
        String[] strArr = Arrays.stream(numbers).mapToObj(String :: valueOf).toArray(String[]::new);
        
        Arrays.sort(strArr, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				int a = Integer.parseInt(o2+o1);
				int b = Integer.parseInt(o1+o2);
				return a-b;
			}
        	
        });
        
        for(String str : strArr) {
        	answer = answer + str;
		}
        
       return answer;
    }

 

ํ•˜์ง€๋งŒ ๋˜ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ํ•˜๋‚˜๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๊ณ , ์ด์œ ๋ฅผ ์ฐพ์•„๋ณด๋‹ˆ 0๋งŒ ์žˆ๋Š” ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ 0์„ ๋ฆฌํ„ดํ•ด์•ผํ•˜๋Š”๋ฐ 000 ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฆฌํ„ดํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. 

 

int i = Integer.parseInt(answer);
if(i == 0) {
    return "0";
}

์ด ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋Š”๋ฐ ๋Ÿฐํƒ€์ž„์—๋Ÿฌ๊ฐ€ ์šฐ์ˆ˜์ˆ˜ ๋–จ์–ด์ง€๋”๋ผ.. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ค‘ ์ˆซ์ž๊ฐ€ ํฐ ๊ฒฝ์šฐ int ๋ฒ”์œ„ ๋ฐ–์ด๋ผ ๊ทธ๋Ÿฐ๋“ฏ..

 

์•„๋ž˜์™€ ๊ฐ™์ด ์†Œ์Šค๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ํ…Œ์ŠคํŠธ์— ํ†ต๊ณผํ•˜์˜€๋‹ค! 

public String solution(int[] numbers) {
        String answer = "";
        
        String[] strArr = Arrays.stream(numbers).mapToObj(String :: valueOf).toArray(String[]::new);
        
        Arrays.sort(strArr, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				int a = Integer.parseInt(o2+o1);
				int b = Integer.parseInt(o1+o2);
				return a-b;
			}
        	
        });
        
        int zeroCnt = 0;
        for(String str : strArr) {
        	 int i = Integer.parseInt(str);
        	 if(i ==0) {
        		 zeroCnt++;
        	 }
        	answer = answer + str;
		}
        if(zeroCnt == strArr.length) {
        	return "0";
        }
       
        
       return answer;
    }

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€