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

1. Rect ํด๋ž˜์Šค์— ์ž…๋ ฅ๋ฐ›๋Š” ๋ฉ”์†Œ๋“œ, ๋‘˜๋ ˆ๊ตฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ, ๋„“์ด ๊ตฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ, ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ ๋‚˜๋ˆ ์„œ ๊ตฌํ˜„ํ•œ ํ›„

๊ฐ’์„ ๊ตฌํ•˜์—ฌ๋ผ.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Scanner;
 
class Rect{//์ง์‚ฌ๊ฐํ˜• ํด๋ž˜์Šค ์„ค๊ณ„
 
        //๋ฐ์ดํ„ฐ ( ์†์„ฑ, ์ƒํƒœ) → ๋ณ€์ˆ˜
        int w, h;                //๊ฐ€๋กœ ์„ธ๋กœ
 
        //๊ธฐ๋Šฅ (๋™์ž‘, ํ–‰์œ„) →๋ฉ”์†Œ๋“œ
        void input(){                //๊ฐ€๋กœ์„ธ๋กœ์ž…๋ ฅ() 
                                    //์‚ฌ์šฉ์ž๋กœ ๋ถ€ํ„ฐ ๊ฐ€๋กœ,์„ธ๋กœ๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ๋งค๊ฐœ๋ณ€์ˆ˜ ํ•„์š”์—†์Œ
                                    // ๋ฐ˜ํ™˜๊ฐ’์ด ์—†์œผ๋ฉด void ์‚ฌ์šฉ, ์—†์„ ๋•Œ ์•„๋ฌด๊ฒƒ๋„ ์•ˆ์“ฐ๋Š” ๋ฌธ๋ฒ•์€ ์ด๋ฏธ ์ƒ์„ฑ์ž๊ฐ€ ๊ฐ€์ง€๊ณ  ๊ฐ”๋”ฐ.
            
            Scanner sc = new Scanner(System.in);
 
            System.out.print("๊ฐ€๋กœ ์ž…๋ ฅ:");
            w = sc.nextInt();
 
            System.out.print("์„ธ๋กœ ์ž…๋ ฅ:");
            h = sc.nextInt();
            
        }
        int calArea(){                //๋„“์ด๊ณ„์‚ฐ()//๋ชจ๋‘ ๋ฉค๋ฒ„์ด๊ธฐ ๋•Œ๋ฌธ์—
                                    //๋„“์ด์— ๋Œ€ํ•œ ๋ณ€์ˆ˜๊ฐ€ ์—†์œผ๋‹ˆ๊น return ๊ฐ’์œผ๋กœ int ๋กœ ๊ณฑํ•ด์„œ ์ค€๋‹ค.//๊ฐ์ฒด๋„ ๋ฐ˜ํ™˜ํ•ด์ค„์ˆ˜ ์žˆ์Œ
            int result;
            result= w * h ;
            return result;            //return ๋’ค์— ์ ์–ด ๋ดค์ž ์•„๋ฌด ์˜๋ฏธ ์—†์Œ
 
        }
        int calLength(){            //๋‘˜๋ ˆ ๊ณ„์‚ฐ()
 
            int result;
            result = (w + h) *2;
            return result;
 
        }
        void print(int a, int l)    {//๊ฒฐ๊ณผ ์ถœ๋ ฅ()//๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์€ ์ƒ๊ด€์—†๋‹ค
 
            System.out.println("๊ฐ€๋กœ : " + w);    //๊ฐ€๋กœ : 10
            System.out.println("์„ธ๋กœ : " + h);    //์„ธ๋กœ : 20
            System.out.println("๋„“์ด : " + a);    //๋„“์ด : xxx
            System.out.println("๋‘˜๋ ˆ : " + l);  //๋‘˜๋ ˆ : xxx
        }
}
public class Test065{
 
    public static void main(String[] args){
 
        //Rect ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ -- Rect๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผํ•œ๋‹ค./์ด์ œ ๋ถ•์–ด๋นต
        Rect ob = new Rect();
 
        //์ž…๋ ฅ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
        ob.input();
 
        //๋„“์ด ์—ฐ์‚ฐ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
         int area = ob.calArea();    //ob.calArea();ํ˜ธ์ถœ๋˜๋ฉด 55๋ผ๊ณ  ๊ฐ’์„ ๋‘๊ณ ๊ฐ€์„œ ๊ทธ๊ฑธ ๋‹ด์„ ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”
        
        //๋‘˜๋ ˆ ์—ฐ์‚ฐ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
         int length = ob.calLength();
 
         //๊ฒฐ๊ณผ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
         ob.print(area , length);
 
    }
}
cs

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

 

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

array์˜ ๊ฐ element ์ค‘ divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” element๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋‹ค๋ฉด ๋ฐฐ์—ด์— -1์„ ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”. ์ œํ•œ์‚ฌํ•ญ arr์€ ์ž์—ฐ์ˆ˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. ์ •์ˆ˜ i, j์— ๋Œ€ํ•ด i ≠ j ์ด๋ฉด arr[i] ≠ arr[j] ์ž…๋‹ˆ๋‹ค. divisor๋Š” ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค. array๋Š” ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ arr divi

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
import java.util.Arrays;
class Solution {
  public int[] solution(int[] arr, int divisor) {
      int[] temp2;
      int[] temp = new int[arr.length];
      int k=0;
      //n์œผ๋กœ ๋‚˜๋ˆ ์ง€๋Š” ๊ฒƒ์„ temp์— ์ €์žฅ
      for(int i=0; i<arr.length; i++){
          if(arr[i]%divisor==0){  
              temp[k]=arr[i]; 
              k++;  }
      }
      //k=0์ผ ๋•Œ 0 ์•„๋‹ ๋•Œ๋กœ ๋‚˜๋ˆ„์–ด ์ฒ˜๋ฆฌ
      if(k==0){
          temp2 =new int[1];
          temp2[0]=-1;
      }else{
          temp2 =Arrays.copyOfRange(temp,0,k);
          Arrays.sort(temp2);
      }
      return temp2;
  }
}

 

โ—‹ ์ƒ๊ฐ๊ณผ์ •

1.divisor๋กœ ๋‚˜๋ˆ„์–ด์ง€๋Š” ์ˆ˜๋ฅผ temp ๋ฐฐ์—ด์— ๋„ฃ๋Š”๋‹ค

2.temp์˜ ๊ธธ์ด๋ฅผ arr.length๋กœ ํ•ด์„œ ๋‹ด๊ธฐ์ง€ ์•Š์€ ์ชฝ์ด 0์œผ๋กœ ์ดˆ๊ธฐํ™” ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ •๋ ฌ์‹œ์— 0๋„ ์ •๋ ฌ ๋˜๊ธฐ ๋•Œ๋ฌธ์— K๋กœ ๊ฐ’์ด ๋„ฃ์–ด ์งˆ๋•Œ     ๋งˆ๋‹ค ์นด์šดํŠธ ํ•ด์„œ k๊ฐ’์œผ๋กœ temp2์— ๋ณต์‚ฌํ•ด์ค€๋‹ค.

3.Arrays.sort() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์ •๋ ฌํ•œ๋‹ค.

 

โ—‹ ๊ฐ™์ด ๋ณด๊ณ ์‹ถ์€ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Arrays;
 
class Divisible {
    public int[] divisible(int[] array, int divisor) {
        //ret์— array์— ํฌํ•จ๋œ ์ •์ˆ˜์ค‘, divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆซ์ž๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์œผ์„ธ์š”.
        return Arrays.stream(array).filter(factor -> factor % divisor == 0).toArray();
    }
    // ์•„๋ž˜๋Š” ํ…Œ์ŠคํŠธ๋กœ ์ถœ๋ ฅํ•ด ๋ณด๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
    public static void main(String[] args) {
        Divisible div = new Divisible();
        int[] array = {59710};
        System.out.println( Arrays.toString( div.divisible(array, 5) ));
    }
}
cs

 

โ—‹ ๋‹ค์‹œ ํ‘ผ ํ’€์ด

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
import java.util.Arrays;
class Solution {
  public int[] solution(int[] arr, int divisor) {
        int[] temp =new int[arr.length];
        int[] answer;
        int cnt=0;
      for(int i =0; i<arr.length;i++){
          if(arr[i]%divisor==0){
            temp[cnt]=arr[i]; 
              cnt++;
          }
      }     
      
      if(cnt==0){
          answer =new int[1];
          answer[0]=-1;
          
      }else { 
           answer =new int[cnt];
          for(int i=0; i<temp.length; i++){
            if(temp[i]!=0)
              answer[i]=temp[i];
            }
      }
      Arrays.sort(answer);
   
      return answer;
 
  }
}
cs

 

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

 

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

๋ฌธ์ž์—ด s์— ๋‚˜ํƒ€๋‚˜๋Š” ๋ฌธ์ž๋ฅผ ํฐ๊ฒƒ๋ถ€ํ„ฐ ์ž‘์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. s๋Š” ์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋ณด๋‹ค ์ž‘์€ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ์ œํ•œ ์‚ฌํ•ญ str์€ ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ s return Zbcdefg gfedcbZ

programmers.co.kr

<๋ฌธ์ž์—ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ>

โ—‹ ์ฒ˜์Œ ํ‘ผ ํ’€์ด (ํ•จ์ˆ˜ ์—†์ด ํ’€๊ธฐ)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Arrays;
class Solution {
  public String solution(String s) {
      String answer = "";
      char[] a= new char [s.length()];
     //๋ฌธ์ž์—ด charAt์œผ๋กœ char๋ฐฐ์—ด์— ๋‹ด๊ธฐ
      for(int i=0; i<s.length(); i++){
          a[i]=s.charAt(i);
      }
      //์ •๋ ฌ
       Arrays.sort(a);
       //๋ฐ˜๋Œ€๋กœ ๋”ํ•ด์ฃผ๊ธฐ
       for(int i=s.length()-1; i>=0; i--){
        answer+=a[i];
       }
      
      return answer;
  }
}
cs
 

 โ—‹ ์ฐธ๊ณ  ํ•  ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.Arrays;
 
public class ReverseStr {
    public String reverseStr(String str){
    char[] sol = str.toCharArray();
    Arrays.sort(sol);
    return new StringBuilder(new String(sol)).reverse().toString();
    }
 
    // ์•„๋ž˜๋Š” ํ…Œ์ŠคํŠธ๋กœ ์ถœ๋ ฅํ•ด ๋ณด๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
    public static void main(String[] args) {
        ReverseStr rs = new ReverseStr();
        System.out.println( rs.reverseStr("Zbcdefg") );
    }
}
cs

 

 

 

 

 

+ Recent posts