-
프로그래머스 / 삼총사코딩 테스트/Level 1 2022. 10. 27. 23:14반응형
https://school.programmers.co.kr/learn/courses/30/lessons/131705
파이썬
억지스럽게...
from itertools import combinations solution = lambda number: sum(1 for each in combinations(number, 3) if sum(each) == 0)
정상적으로...
from itertools import combinations def solution(number): return sum(1 for each in combinations(number, 3) if sum(each) == 0)
코틀린
class Solution { fun solution(number: IntArray): Int { var answer: Int = 0 for (i1 in number.indices) for (i2 in i1 + 1 until number.size) for (i3 in i2 + 1 until number.size) if (number[i1] + number[i2] + number[i3] == 0) { answer++ } return answer } }
class Solution { fun solution(number: IntArray) = number.indices.sumOf { i1 -> (i1 + 1 until number.size).sumOf { i2 -> (i2 + 1 until number.size).count { i3 -> number[i1] + number[i2] + number[i3] == 0 } } } }
반응형