728x90
SMALL

using System;

public class Solution {
    public int[] solution(int n, int[] numlist) {
        int length = 0;
        int length2 = 0;
        
        for(int i=0;i<numlist.Length;i++)
        {
            if(numlist[i]%n == 0)
            {
                length2++;
            }
        }
        int[] answer = new int[length2];

/// 배열의 길이 정의하기
        
        for(int i=0;i<numlist.Length;i++)
        {
            if(numlist[i]%n == 0)
            {
                answer[length] = numlist[i];
                length++;
            }
        }
    
        return answer;
    }
}

 

//리스트를 모른다고 생각했을 때 노가다 방식으로

 

//List를 사용했을 때

 

using System;
using System.Collections.Generic;

public class Solution {
    public int[] solution(int n, int[] numlist) {
        int[] answer = new int[] {};
        List<int> insList = new List<int>();

        for(int i = 0; i < numlist.Length; i++)
        {
            if(numlist[i]%n == 0)
            {
                insList.Add(numlist[i]);
            }
        }
        
        answer = insList.ToArray();
        return answer;
    }
}

 

파이썬

 

def solution(n, numlist):
    answer = [i for i in numlist if i%n==0]
    return answer

 

 

자바

 

 

import java.util.ArrayList;

class Solution {
    public int[] solution(int n, int[] numlist) {
        ArrayList<Integer> List = new ArrayList<>();
        
        for(int i = 0;i < numlist.length; i++)
        {
            if(numlist[i] % n == 0)
            {
                List.add(numlist[i]);
            }
        }
        
        int[] answer = new int[List.size()];
        for(int i = 0; i< List.size(); i++)
        {
            answer[i] = List.get(i);
        }
        
        return answer;
    }
}

 

 

import java.util.List;
import java.util.ArrayList;

class Solution {
    public int[] solution(int n, int[] numlist) {
        List<Integer> answer = new ArrayList<>();
        
        for(int num : numlist)
        {    
            if(num % n == 0)
            {
                answer.add(num);
            }
        }
        
        return answer.stream().mapToInt(x -> x).toArray();
    }
}
728x90

설정

트랙백

댓글