https://programmers.co.kr/learn/courses/30/lessons/12915

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ฌธ์ž์—ด ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

๋ฌธ์ž์—ด๋กœ ๊ตฌ์„ฑ๋œ ๋ฆฌ์ŠคํŠธ strings์™€, ์ •์ˆ˜ n์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค n๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด strings๊ฐ€ [sun, bed, car]์ด๊ณ  n์ด 1์ด๋ฉด ๊ฐ ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค 1์˜ ๋ฌธ์ž u, e, a๋กœ strings๋ฅผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ์ œํ•œ ์กฐ๊ฑด strings๋Š” ๊ธธ์ด 1 ์ด์ƒ, 50์ดํ•˜์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. strings์˜ ์›์†Œ๋Š” ์†Œ๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. strings์˜ ์›์†Œ๋Š” ๊ธธ์ด 1 ์ด์ƒ, 100์ดํ•˜์ธ

programmers.co.kr

 

< ๋ฌธ์ž์—ด ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ >

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Arrays;
class Solution {
  public String[] solution(String[] strings, int n) {
       String temp ;
       int cnt=0;
       int jungbok=0;
       int jung=0;
       int cnt1=0;
   
       for(int i =0; i<strings.length-1; i++){
            for(int j=0; j<strings.length-1; j++){
                if(strings[j].charAt(n) > strings[j+1].charAt(n)){
                    temp = strings[j];
                    strings[j]=strings[j+1];
                    strings[j+1]=temp;   
                    cnt++;
                }
                if(strings[j].charAt(n) == strings[j+1].charAt(n)){
                    jungbok++;
                    jung=j;
                }
                 
            }if(cnt==0)
                break;
       }
        //์ค‘๋ณต์ด ์žˆ์„๊ฒฝ์šฐ
        if(jungbok>0){
            for(int i =jung; i<strings.length-1; i++){
                for(int j=jung; j<strings.length-1; j++){    
                    if(strings[j].charAt(n+1> strings[j+1].charAt(n+1)){
                         temp = strings[j];
                         strings[j]=strings[j+1];
                         strings[j+1]=temp;   
                         cnt1++;
                     }
                     
                        
                            
                } if(cnt1==0)
                    break;                
            }                  
        }  
    
        
        
             
      return strings;
  }
}
cs

+ Recent posts