๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Develop Study ๐Ÿ’ป

java / ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆซ์ž ๋ฐฐ์—ด

728x90

๋ฌธ์ œ : array์˜ ๊ฐ element ์ค‘ divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” element๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋‹ค๋ฉด ๋ฐฐ์—ด์— -1์„ ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.

 - arr๋Š” ์ž์—ฐ์ˆ˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด์ด๊ณ  divisor ์€ ์ž์—ฐ์ˆ˜์ž„

์˜ˆ์‹œ๋กœ arr[5,9,8,10]์ด๊ณ  divisor์ด 5์ด๋ฉด [5,10]์ด ๋ฆฌํ„ด๋œ๋‹ค

 

๋ญ”๊ฐ€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๊ฐ’์„ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ํ•˜๋‚˜์”ฉ ๋ฐฐ์—ดํฌ๊ธฐ๊นŒ์ง€ ๋Š˜๋ ค๊ฐ€๋Š” for ๋ฐ˜๋ณตํ•˜๊ณ ,  ๊ทธ ์ธ๋ฑ์Šค ๊ฐ’์— ์žˆ๋Š” ์ˆซ์ž (int)๋ฅผ divior๋กœ ๋‚˜๋ˆ„์–ด์„œ  ๋‚˜๋จธ์ง€๊ฐ€ 0์ธ ๊ฒฝ์šฐ์—๋งŒ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ๋ฆฌํ„ด์„ ํ•ด์ฃผ๋ฉด ๋ ๊ฑฐ๊ฐ™๋‹ค.

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
class Solution {
  public int[] solution(int[] arr, int divisor) {
      
       List mylist = new ArrayList();
      for (int i = 0 ; i < arr.length ; i ++ )
      {
          if (arr[i] % divisor == 0 )
          {
              list.add(arr[i]);
             
          }else {
               continue ; 
          }
              
     }
      if(list.isEmpty()){
          list.add(0,-1);
      }
      Collections.sort(list);
      
        int[] answer = new int[list.size()];
      for (int j = 0 ; j < answer.length ; j ++ ){
          answer[j] = list.get(j);
      }
      
      return answer;
      }
      
      
      
  }
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

31

class Solution {

  public int[] solution(int[] arr, int divisor) {

      

       List mylist = new ArrayList();

      for (int i = 0 ; i < arr.length ; i ++ )

      {

          if (arr[i] % divisor == 0 )

          {

              list.add(arr[i]);

             

          }else {

               continue ; 

          }

              

     }

      if(list.isEmpty()){

          list.add(0,-1);

      }

      Collections.sort(list);

      

        int[] answer = new int[list.size()];

      for (int j = 0 ; j < answer.length ; j ++ ){

          answer[j] = list.get(j);

      }

      

      return answer;

      }

      

      

      

  }

Colored by Color Scripter

cs
728x90