728x90
SMALL

using System;
using System.Collections.Generic;

public class Solution {
    public int[] solution(int n) {
        List<int> List = new List<int>();
        for(int i=1;i<n+1;i++)
        {
            if(n%i == 0)
            {
                List.Add(i);
            }
        }
        int[] answer = List.ToArray();
        return answer;
    }
}

 

파이썬

 

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

 

자바

 

import java.util.*;

class Solution {
    public int[] solution(int n) {
        List<Integer> list = new ArrayList<>();

        for (int i = 1; i <= n / 2; i++) 
        {
            if (n % i == 0) 
            {
                list.add(i);
            }
        }

        list.add(n);

        return list.stream().mapToInt(e -> e).toArray();
    }
}

 

 

import java.util.stream.IntStream;
import java.util.Arrays;

class Solution {
    public int[] solution(int n) {
        return IntStream.rangeClosed(1, n).filter(i -> n % i == 0).toArray();
    }
}
728x90

설정

트랙백

댓글