알고리즘

[프로그래머스] 삼총사

hminor 2023. 7. 5. 10:27
# 조합으로 푸는 방법

from itertools import combinations

def solution (number) :
    cnt = 0
    for i in combinations(number,3) :
        if sum(i) == 0 :
            cnt += 1
    return cnt
# 3중 for문을 이용해 푸는 방법

def solution(number):
    ln, cnt = len(number), 0
    for i in range(ln-2):
        for j in range(i+1, ln-1):
            for z in range(j+1, ln):
                if number[i]+number[j]+number[z] == 0: cnt+= 1
    return cnt