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

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ–‰๋ ฌ์˜ ๋ง์…ˆ | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

ํ–‰๋ ฌ์˜ ๋ง์…ˆ์€ ํ–‰๊ณผ ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์€ ๋‘ ํ–‰๋ ฌ์˜ ๊ฐ™์€ ํ–‰, ๊ฐ™์€ ์—ด์˜ ๊ฐ’์„ ์„œ๋กœ ๋”ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 2๊ฐœ์˜ ํ–‰๋ ฌ arr1๊ณผ arr2๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, ํ–‰๋ ฌ ๋ง์…ˆ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ์ œํ•œ ์กฐ๊ฑด ํ–‰๋ ฌ arr1, arr2์˜ ํ–‰๊ณผ ์—ด์˜ ๊ธธ์ด๋Š” 500์„ ๋„˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ arr1 arr2 return [[1,2],[2,3]] [[3,4],[5,6]] [[4,6],[7,9]] [[1],[2]] [[3],[4]] [[4],[

programmers.co.kr

<ํ–‰๋ ฌ์˜ ๋ง์…ˆ>

 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
  public int[][] solution(int[][] arr1, int[][] arr2) {
     
      int[][] answer = new int[arr1.length][arr1[0].length];
   
      for(int i=0; i<arr1.length;i++){
          for(int j=0; j<arr1[i].length; j++){
              answer[i][j]=arr1[i][j]+arr2[i][j];
          }
      }
      return answer;
  }
}
cs

1.์‹œํ–‰ ์ฐฉ์˜ค
- ๋งจ์ฒ˜์Œ ํ–‰๊ณผ ์—ด์ด ๊ฐ™์€ ํ–‰๋ ฌ์ด๋ผ๊ณ  ํ•ด์„œ ์ •์‚ฌ๊ฐํ˜•์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ i ์˜ ์กฐ๊ฑด๊ณผ j์˜ ์กฐ๊ฑด์„ arr1.length๋กœ ์„ค์ •ํ–ˆ๋‹ค.

 

 

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

 

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

์ •์ˆ˜๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด arr์˜ ํ‰๊ท ๊ฐ’์„ returnํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด๋ณด์„ธ์š”. ์ œํ•œ์‚ฌํ•ญ arr์€ ๊ธธ์ด 1 ์ด์ƒ, 100 ์ดํ•˜์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. arr์˜ ์›์†Œ๋Š” -10,000 ์ด์ƒ 10,000 ์ดํ•˜์ธ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ arr return [1,2,3,4] 2.5 [5,5] 5

programmers.co.kr

 

ํ‰๊ท ๊ตฌํ•˜๊ธฐ ๋ฌธ์ œ

 

 

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

1
2
3
4
5
6
7
8
9
10
class Solution {
  public double solution(int[] arr) {
      double answer = 0;
      for(int i=0; i<arr.length;i++){
          answer+=arr[i];
      }
      answer/=arr.length;
      return answer;
  }
}
cs

+ Recent posts