728x90
SMALL

using System;

public class Solution {
    public int solution(int[] number) {
        int answer = 0;

        for(int i = 0;i<number.Length;i++)
        {
            for(int j = i+1;j<number.Length;j++)
            {
                for(int k = j+1;k<number.Length;k++)
                {
                    if(number[i]+number[j]+number[k]==0)
                    {
                        answer++;
                    }
                }
            }
        }
        
        return answer;
    }
}

 

// number의 길이가 그렇게 길지 않아서 걍 포문을 돌렸다.

 

 

 

파이썬

////

 

 

def solution(number):
    answer = 0
    l = len(number)
    for i in range(l-2):
        for j in range(i+1, l-1):
            for k in range(j+1, l):
                # print(number[i],number[j],number[k])
                if number[i]+number[j]+number[k] == 0:
                    answer += 1           
    return answer

 

////

from itertools import combinations
def solution(number):
    answer = 0
    for c in combinations(range(len(number)), 3):
        if sum([number[int(i)] for i in c]) == 0:
            answer += 1
    return answer

 

 

자바

////

 

 

class Solution {
    public int solution(int[] number) {
        int answer = 0;

        for(int i=0; i<number.length-2; i++)
        {
            for(int j=i+1; j<number.length-1; j++)
            {
                for(int k=j+1; k<number.length; k++)
                {
                    if(number[i]+number[j]+number[k]==0) 
                      answer++;
                }
            }
        }

        return answer;
    }
}
728x90

설정

트랙백

댓글