본문 바로가기
알고리즘 & 자료구조/프로그래머스

삼총사

by 신재권 2022. 10. 30.
package programmers;

public class 삼총사 {

   public static void main(String[] args) {
      System.out.println(solution(new int[] {-2, 3, 0, 2, -5}));
      System.out.println(solution(new int[] {-3, -2, -1, 0, 1, 2, 3}));
      System.out.println(solution(new int[] {-1, 1, -1, 1}));
   }

   public static 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;
   }

}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

폰켓몬  (0) 2022.11.01
2016년  (0) 2022.11.01
두 개 뽑아서 더하기  (0) 2022.10.30
K번째수  (0) 2022.10.29
문자열 내 마음대로 정렬  (0) 2022.10.29