[JAVA 문제 풀이] 391. 테이블 해시 함수

프로그래머스 (147354)
Stupefyee's avatar
Jul 22, 2025
[JAVA 문제 풀이] 391. 테이블 해시 함수
notion image
 

내가 작성한 코드

import java.util.*; class Solution { public int solution(int[][] data, int col, int row_begin, int row_end) { int answer = 0; // 기준에 따라 정렬 Arrays.sort(data, new Comparator<int[]>() { @Override public int compare(int[] a, int[] b) { if (a[col - 1] != b[col - 1]) { // 1차 기준: col 오름차순 return Integer.compare(a[col - 1], b[col - 1]); } else { // 2차 기준: data[i][0] 내림차순 return Integer.compare(b[0], a[0]); } } }); // 한줄로 줄이기 // Arrays.sort(data,(o1, o2) -> o1[col-1]!=o2[col-1] ?o1[col-1]-o2[col-1] :o2[0]-o1[0]); // XOR 연산 for (int i = row_begin - 1; i < row_end; i++) { int modSum = 0; // 나머지 합을 담을 변수 for(int j = 0; j < data[i].length; j++) { modSum += data[i][j] % (i + 1); } answer ^= modSum; } return answer; } }
Share article

stupefyee