โ—‹ ์ถ”์ƒํด๋ž˜์Šค ( abstract : ๋ฏธ์™„์„ฑ์ด๋ผ๋Š” ์˜๋ฏธ )

 - ์„ ์–ธ๋งŒ ์žˆ๊ณ  ์ •์˜๊ฐ€ ์—†๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ฉ”์†Œ๋“œ(์ถ”์ƒ ๋ฉ”์†Œ๋“œ)๋ฅผ ๊ฐ–๋Š” ํด๋ž˜์Šค๋กœ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ(overriding)     ํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜๋Š” ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•ด ๋ฉ”๋ชจ๋ฆฌ๋‚ญ๋น„ ์—†์ด ๋ฏธ๋ฆฌ ํ˜ธ์ถœ ํ•  ๊ณ„ํš์„ ์„ธ์›Œ ๋‘๊ธฐ ์œ„ํ•ด ๋งŒ๋“ ๋‹ค.

 

โ—‹ ํ˜•์‹ ๋ฐ ๊ตฌ์กฐ

    [์ ‘๊ทผ ์ œ์–ด์ง€์‹œ์ž] abstract class ํด๋ž˜์Šค๋ช…{

  

    [์ ‘๊ทผ ์ œ์–ด์ง€์‹œ์ž] abstract ์ž๋ฃŒํ˜• ๋ฉ”์†Œ๋“œ๋ช…([๋งค๊ฐœ๋ณ€์ˆ˜], ...);  //๋ฉ”์†Œ๋“œ์˜ ์„ ์–ธ๋ถ€๋งŒ ์ž‘์„ฑํ•œ๋‹ค.

 

    }

 

    ์ œ์–ด์ž - 1. ์ ‘๊ทผ์ œ์–ด ์ง€์‹œ์ž : public , protected , default , private

                - 2.  ๊ทธ์™ธ ์ง€์‹œ์ž : static, final , abstract , ...๋“ฑ๋“ฑ 

     **๋‹จ, ์ ‘๊ทผ ์ œ์–ด์ง€์‹œ์ž๋Š” ๋„ค๊ฐ€์ง€์ค‘ ํ•˜๋‚˜๋งŒ ์„ ํƒํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค. ์ œ์–ด์ž๋“ค ๊ฐ„์˜ ์ˆœ์„œ๋Š” ๊ด€๊ณ„์—†์ง€๋งŒ ์ฃผ๋กœ ์ ‘๊ทผ           ์ œ์–ด ์ง€์‹œ์ž๋ฅผ ์ œ์ผ ์™ผ์ชฝ์— ๋†“๋Š” ๊ฒฝํ–ฅ์ด ์žˆ๋‹ค.

 

โ—‹ ํŠน์ง•

     ํด๋ž˜์Šค๊ฐ€ ์ ์–ด๋„ ํ•˜๋‚˜ ์ด์ƒ์˜ ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋ฅผ ํฌํ•จํ•  ๋•Œ ๊ทธ ํด๋ž˜์Šค๋Š” ํด๋ž˜์Šค ์•ž์—  'abstract' ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ 

     ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ๋ช…์‹œํ•ด์•ผํ•˜๋ฉฐ, ์ถ”์ƒํด๋ž˜์Šค๋กœ ์„ ์–ธํ•œ ๊ฒฝ์šฐ์—๋Š” ๋ถˆ์™„์ „ํ•œ ํ˜•ํƒœ์˜ ํด๋ž˜์Šค์ด๋ฏ€๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜         ์—†๋‹ค. ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ถ”์ƒ ํด๋ž˜์Šค ๋งˆ์ €๋„ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ ํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด๋‹ค. 

      → ์ถ”์ƒ๋ฉ”์†Œ๋“œ๊ฐ€ ์—†๋Š” ํด๋ž˜์Šค๋Š” ์™œ ์กด์žฌ ํ• ๊นŒ?

           - ํด๋ž˜์Šค ์ค‘ ํด๋ž˜์Šค์•ˆ์— ๋‚ด์šฉ์ด ์—†๋Š” ๋ฉ”์†Œ๋“œ๋งŒ ์กด์žฌํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ ์žˆ๋Š”๋ฐ ์ด๋Ÿฐ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€                 ๋ชปํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํด๋ž˜์Šค ์•ž์— abstract๋ฅผ ๋ถ™์ธ๋‹ค. ๋‚˜์ค‘์— ์ƒ์†๋ฐ›์•„ ํ•„์š”ํ•œ ๋ฉ”์†Œ๋“œ๋งŒ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•˜์—ฌ                ์‚ฌ์šฉํ•œ๋‹ค ('abstract'๊ฐ€ ๋ถ™์€ ๋ฉ”์†Œ๋“œ๋Š” ๋ฌด์กฐ๊ฑด ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•ด์•ผํ•œ๋‹ค.)

 

     ์ฆ‰, ์ถ”์ƒํด๋ž˜์Šค๋Š” ๋…๋ฆฝํด๋ž˜์Šค ๋…๋ฆฝ์ ์œผ๋กœ ์กด์žฌ ํ• ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์†์„ ์œ„ํ•ด์„œ๋งŒ ์กด์žฌํ•˜๋ฉฐ ์ถ”์ƒ ํด๋ž˜์Šค์—์„œ

     ์ƒ์†๋ฐ›์€ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ๋Š” ๋ฐ˜๋“œ์‹œ ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ(overriding)ํ•ด์•ผํ•œ๋‹ค.

 

** 'abstract' ํ‚ค์›Œ๋“œ๋Š” ํด๋ž˜์Šค์™€ ๋ฉ”์†Œ๋“œ์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ , ๋กœ์ปฌ๋ณ€์ˆ˜์—์„œ๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค

     ํด๋ž˜์Šค - ํด๋ž˜์Šค ๋‚ด์— ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๊ฐ€ ์„ ์–ธ ๋˜์–ด ์žˆ์Œ์„ ์˜๋ฏธํ•œ๋‹ค.

     ๋ฉ”์†Œ๋“œ - ์„ ์–ธ๋ถ€๋งŒ ์ž‘์„ฑํ•˜๊ณ  ๊ตฌํ˜„๋ถ€๋Š” ์ž‘์„ฑํ•˜์ง€ ์•Š์€ ์ถ”์ƒ ๋ฉ”์„œ๋“œ์ž„์„ ์•Œ๋ฆฐ๋‹ค.

 

 

 

            

 

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - 2016๋…„ | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

2016๋…„ 1์›” 1์ผ์€ ๊ธˆ์š”์ผ์ž…๋‹ˆ๋‹ค. 2016๋…„ a์›” b์ผ์€ ๋ฌด์Šจ ์š”์ผ์ผ๊นŒ์š”? ๋‘ ์ˆ˜ a ,b๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ 2016๋…„ a์›” b์ผ์ด ๋ฌด์Šจ ์š”์ผ์ธ์ง€ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”. ์š”์ผ์˜ ์ด๋ฆ„์€ ์ผ์š”์ผ๋ถ€ํ„ฐ ํ† ์š”์ผ๊นŒ์ง€ ๊ฐ๊ฐ SUN,MON,TUE,WED,THU,FRI,SAT ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด a=5, b=24๋ผ๋ฉด 5์›” 24์ผ์€ ํ™”์š”์ผ์ด๋ฏ€๋กœ ๋ฌธ์ž์—ด TUE๋ฅผ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”. ์ œํ•œ ์กฐ๊ฑด 2016๋…„์€ ์œค๋…„์ž…๋‹ˆ๋‹ค. 2016๋…„ a์›” b์ผ์€ ์‹ค์ œ๋กœ ์žˆ๋Š” ๋‚ ์ž…๋‹ˆ๋‹ค.

programmers.co.kr

 

<2016๋…„>

โ—‹ ์ฒ˜์Œ ํ‘ผ ํ’€์ด(์™„์ „ ๋ชปํ’€์–ด์„œ ์•„์ด๋””์–ด๋ณด๊ณ  ๋‹ค์‹œํ’ˆ)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
  public String solution(int a, int b) {
      String answer = "";
      int[] month={31,29,31,30,31,30,31,31,30,31,30,31};
      int totDate=0;
      
      for(int i=0; i<a-1; i++)
          totDate+=month[i];          
      
      totDate+=b;
      switch(totDate%7){
          case 1:answer+="FRI";break;
          case 2:answer+="SAT";break;
          case 3:answer+="SUN";break;
          case 4:answer+="MON";break;
          case 5:answer+="TUE";break;
          case 6:answer+="WED";break;
          case 0:answer+="THU";break;
      }
 
      return answer;
  }
}
cs

 

 

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‹œ์ € ์•”ํ˜ธ | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์–ด๋–ค ๋ฌธ์žฅ์˜ ๊ฐ ์•ŒํŒŒ๋ฒณ์„ ์ผ์ •ํ•œ ๊ฑฐ๋ฆฌ๋งŒํผ ๋ฐ€์–ด์„œ ๋‹ค๋ฅธ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ฐ”๊พธ๋Š” ์•”ํ˜ธํ™” ๋ฐฉ์‹์„ ์‹œ์ € ์•”ํ˜ธ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด AB๋Š” 1๋งŒํผ ๋ฐ€๋ฉด BC๊ฐ€ ๋˜๊ณ , 3๋งŒํผ ๋ฐ€๋ฉด DE๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. z๋Š” 1๋งŒํผ ๋ฐ€๋ฉด a๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด s์™€ ๊ฑฐ๋ฆฌ n์„ ์ž…๋ ฅ๋ฐ›์•„ s๋ฅผ n๋งŒํผ ๋ฏผ ์•”ํ˜ธ๋ฌธ์„ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด ๋ณด์„ธ์š”. ์ œํ•œ ์กฐ๊ฑด ๊ณต๋ฐฑ์€ ์•„๋ฌด๋ฆฌ ๋ฐ€์–ด๋„ ๊ณต๋ฐฑ์ž…๋‹ˆ๋‹ค. s๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž, ๊ณต๋ฐฑ์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. s์˜ ๊ธธ์ด๋Š” 8000์ดํ•˜์ž…๋‹ˆ๋‹ค.

programmers.co.kr

< ์‹œ์ € ์•”ํ˜ธ>

โ—‹ ์ฒ˜์Œ ํ‘ผ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
  public String solution(String s, int n) {
      String answer = "";
      for(int i=0 ; i<s.length(); i++){
          if(s.charAt(i)==' ')
                answer+=' ';
          else {
              if(s.charAt(i)>=65 && s.charAt(i)<=90)
                 answer += s.charAt(i)+n>90 ? (char)(s.charAt(i)-26+n) :(char)(s.charAt(i)+n);
              else if(s.charAt(i)>=97 && s.charAt(i)<=122)
                 answer += s.charAt(i)+n>122 ? (char)(s.charAt(i)-26+n) :(char)(s.charAt(i)+n);
          }   
      }
           return answer;
  }
}
cs

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

1.๋ฐ€์–ด๋‚ธ ๋‹ค๋Š”๊ฒƒ -> char๊ฐ’์— n์”ฉ ๋”ํ•ด์•ผํ•œ๋‹ค

2. ์†Œ๋ฌธ์ž ๋Œ€๋ฌธ์ž๋กœ ๋‚˜๋ˆ ์•ผํ•œ๋‹ค.

3. 'Z' ํ˜น์€ 'z' ๊ฐ€ ๋˜์—ˆ์„ ๋•Œ ์กฐ๊ฑด์ด ์žˆ์–ด์•ผํ•œ๋‹ค.

 

โ—‹ ์‹œํ–‰์ฐฉ์˜ค

1.์ฒ˜์Œ์—๋Š” ๋ฒ”์œ„๋„ ๋‚˜๋ˆ„์–ด์ฃผ์ง€ ์•Š์•˜๊ณ  ๊ณต๋ฐฑ๋„ ์ƒ๊ฐ ๋ชปํ–ˆ๊ณ  ์กฐ๊ฑด์‹์„ z๊ฐ€ ๋„˜์—ˆ์„๋•Œ๋งŒ ์–ด๋–ป๊ฒŒ ํ•ด๋ผ๋งŒ ๋„ฃ์–ด์คŒ

 

 

 

โ—‹ ํ•จ๊ป˜ ๋ณด๊ณ ์‹ถ์€ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Caesar {
    String caesar(String s, int n) {
        String result = "";
    n = n % 26;
    for (int i = 0; i < s.length(); i++) {
      char ch = s.charAt(i);
      if (Character.isLowerCase(ch)) {
        ch = (char) ((ch - 'a' + n) % 26 + 'a');
      } else if (Character.isUpperCase(ch)) {
        ch = (char) ((ch - 'A' + n) % 26 + 'A');
      }
      result += ch;
    }
        return result;
    }
 
    public static void main(String[] args) {
        Caesar c = new Caesar();
        System.out.println("s๋Š” 'a B z', n์€ 4์ธ ๊ฒฝ์šฐ: " + c.caesar("a B z"4));
    }
}
cs

6๋ฒˆ์งธ์ค„) n๋Š” 25๊นŒ์ง€๋กœ ์ •ํ•ด์ ธ ์žˆ์ง€๋งŒ ๋งŒ์•ฝ์— 25๊นŒ์ง€ ์ •ํ•ด์ ธ ์žˆ์ง€ ์•Š์•˜๋‹ค๋ฉด %26๋กœ ์—ฐ์‚ฐํ•ด์„œ ๋‚˜๋จธ์ง€๋ฅผ ๋ฐ›๋Š”๋‹ค 

8๋ฒˆ์งธ์ค„) ์ž…๋ ฅ๋ฐ›์€ ch ๊ฐ€ ์†Œ๋ฌธ์ž ๋ฒ”์œ„(122)๋ฅผ ๋„˜์„์ˆ˜ ์žˆ์œผ๋‹ˆ๊น -'a'๋ฅผ ๋นผ๊ณ  +n์„ ํ•œ๋‹ค์Œ %26์„ ๋‚˜๋ˆ„๊ณ  ๋‹ค์‹œ 'a'๋ฅผ

            ๋”ํ•ด์ค€๋‹ค  ์—ฌ๊ธฐ์„œ ํ•ต์‹ฌ์€ ๋‚˜๋จธ์ง€์—ฐ์‚ฐ์ž(%)๋ฅผ ํ†ตํ•ด์„œ ๋ฐ€์–ด์ค„ ์ˆซ์ž ๋งŒํผ๋งŒ ๋‚จ๊ฒจ์ฃผ๋Š”๊ฒƒ

Character.isLowerCase(ch)

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - K๋ฒˆ์งธ์ˆ˜ | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]

programmers.co.kr

< K๋ฒˆ์งธ์ˆ˜ >

 

 

 

โ—‹ ์‹œํ–‰์ฐฉ์˜ค

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];//commands์˜ i๊ฐ’ ๋งŒํผ ๋งŒ๋“ค๊ธฐ
        int temp=0;
        int count=0;
        
        for(int i=0 ; i<commands.length; i++){
            for(int j=commands[i][0]-1 ; j< commands[i][1]-1;j++){
                 if(array[commands[i][0]-1]>array[commands[i][0]]){
                        temp=array[commands[i][0]];
                        array[commands[i][0]]=array[commands[i][0]-1];
                        array[commands[i][0]-1]=temp;
                            count++;
                    }             
            }if(count==0){
                
                answer[i]=array[commands[i][2]];
                break;
            }
        }
        return answer;
    }
}

1.์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ์ œ๋Œ€๋กœ ํ™•์ธํ•˜์ง€ ์•Š์•˜๋‹ค.
2.i์™€ j๊ฐ€ ๊ฐ™์€ ๋•Œ๋ฅผ ์ƒ๊ฐํ•˜์ง€ ์•Š์•˜๋‹ค.
3."์ž๋ฅธํ›„ ์ •๋ ฌ" ์ž๋ฅด์ง€์•Š๊ณ  ๊ทธ๋Œ€๋กœ ์ธ๋ฑ์Šค ๊ฐ’๋งŒ ํ•ด์ฃผ๋ฉด ๋ฐฐ์—ด์˜ ์œ„์น˜๊ฐ€ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ๋ฒˆ์งธ ์ •๋ ฌ ๋˜์žˆ์„๋•Œ ์ฒ˜์Œ ๋ฐฐ์—ด ์ƒํƒœ๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ๋ฌด์กฐ๊ฑด ์ž˜๋ผ์„œ ํ•ด์•ผํ•œ๋‹ค.

 

โ—‹ ํ…Œ์ŠคํŠธ๋Š” ์„ฑ๊ณตํ•˜์ง€๋งŒ ํ’€์ด ์ œ์ถœ์‹œ ์‹คํŒจํ•œ ํ’€์ด

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
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        int temp=0;
        int count=0;
        int[][] arr1= new int[commands.length][array.length];
        
        //์ž๋ฅด๋ฉด์„œ ๋ฐฐ์—ด ๋ณต์‚ฌ
        for(int i=0 ;i<commands.length; i++){
            for(int j=commands[i][0]-1, k=0;j<=commands[i][1]-1; j++,k++)
                arr1[i][k]=array[j];
        }
        //๋ฒ„๋ธ”์ •๋ ฌ
        for(int i=0 ; i<commands.length; i++){
            if(commands[i][0]==commands[i][1]){
                answer[i]=arr1[i][commands[i][2]-1];
                continue;
                }
            for(int k=0; k<commands[i][1]-2; k++){
            for(int j=0 ; j<commands[i][1]-2;j++){
                 if(arr1[i][j]>arr1[i][j+1]){
                        temp=arr1[i][j];
                       arr1[i][j]=arr1[i][j+1];
                        arr1[i][j+1]=temp;
                               
                    } 
                }
            }
            answer[i]=arr1[i][commands[i][2]-1];
        }
        return answer;
    }
}

 

1.ํ…Œ์ŠคํŠธ๋Š” ์„ฑ๊ณตํ–ˆ๋Š”๋ฐ ์ฝ”๋“œ ์ œ์ถœํ–ˆ์„ ๋•Œ ํ•œ๊ฐœ ๋นผ๊ณ  ๋ชจ๋‘ ์‹คํŒจํ–ˆ๋‹ค. sort ํ•จ์ˆ˜ ์‚ฌ์šฉํ•ด์„œ ๋‹ค์‹œ ํ•ด๋ด์•ผ๊ฒ ๋‹ค.

 ---------------------------------------------------- ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ ์„ฑ๊ณตํ•œ ํ’€์ด  --------------------------------------------------

โ—‹ ๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด๋ฅผ ๋ณด๊ณ  ํ‘ผ ํ’€์ด( sort() , copyOfRange() ์‚ฌ์šฉ)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Arrays;
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int [commands.length];        
        
        for(int i =0; i<commands.length; i++){
         int[] temp= Arrays.copyOfRange(array,commands[i][0]-1,commands[i][1]);
            Arrays.sort(temp);
            answer[i]=temp[commands[i][2]-1];
        }
        
        return answer;
    }
}

- Arrays.sort(๋ฐฐ์—ด) : ๋ฐฐ์—ด์•ˆ์— ์žˆ๋Š” ๋ฌธ์ž์—ด, ์˜๋ฌธ์ž , ์ˆซ์ž ๋ชจ๋‘ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ•ด์ค€๋‹ค.
- Arrays.copyOfRange(๋ฐฐ์—ด์ด๋ฆ„,์‹œ์ž‘,๋) : ๋ฐฐ์—ด์˜ ์‹œ์ž‘๊ณผ ๋์„ ์ •ํ•ด์„œ ๋ฐฐ์—ด๋กœ ๋ณต์‚ฌํ•ด์ค€๋‹ค.

 

โ—‹ sort() ํ•จ์ˆ˜๋งŒ ์‚ฌ์šฉํ•œ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Arrays;
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int [commands.length];        
                  
        for(int i =0; i<commands.length; i++){
            int start =commands[i][0]-1;
            int finish= commands[i][1]-1;
            int pick=commands[i][2]-1;
            int temp[]=new int [finish-start+1];//4-1=3+1=4 -> 0 1 2 3
            
            //๋ฐฐ์—ด ๋ณต์‚ฌ
            for(int j=start,k=0; j<=finish; j++,k++)//j=1 2 3 4 k=0 1 2 3
                temp[k]=array[j];                
            
            //์ •๋ ฌ                   
            Arrays.sort(temp);
            answer[i]=temp[pick];
        }
        return answer;
    }
 }
 
 

 

โ—‹sort()ํ•จ์ˆ˜ , copyOfRange()ํ•จ์ˆ˜ ์—†์ด ํ‘ผ ํ’€์ด

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
import java.util.Arrays;
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int [commands.length];        
                  
        for(int i =0; i<commands.length; i++){
            int start =commands[i][0]-1;
            int finish= commands[i][1]-1;
            int pick=commands[i][2]-1;
            int temp[]=new int [finish-start+1];//4-1=3+1=4 -> 0 1 2 3
            
            //๋ฐฐ์—ด ๋ณต์‚ฌ
            for(int j=start,k=0; j<=finish; j++,k++)//j=1 2 3 4 k=0 1 2 3
                temp[k]=array[j];                
            
            //ํ–ฅ์ƒ๋œ ๋ฒ„๋ธ”์†ŒํŠธ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌ
            int count=0;
            for(int j=0; j<temp.length-1;j++){
                for(int k=0;k<temp.length-1; k++){//0 1 2 
                    int swap;
                    if(temp[k]>temp[k+1]){
                        swap=temp[k];
                        temp[k]=temp[k+1];
                        temp[k+1]=swap;
                        count++;
                    }
                }if(count==0)   //swap์ด ๋”์ด์ƒ ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค๋ฉด break
                    break;
            }
            //์ •๋ ฌ์ดํ›„ answer์— ๋„ฃ๊ธฐ
            answer[i]=temp[pick];
        }
        return answer;
    }
 }
 
 
 
cs

+ Recent posts