C# 代码
public class Solution {
public int[][] DifferenceOfDistinctValues(int[][] grid) {
int n = grid.Length, m = grid[0].Length;
int[][] result = new int[n][];
for (int i = 0; i < n; i++){
result[i] = new int[m];
}
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
result[i][j] = Get(i, j);
}
}
return result;
int Get(int x, int y){
HashSet<int> left = new HashSet<int>();
HashSet<int> right = new HashSet<int>();
int r = x - 1, c = y - 1;
while (r >= 0 && c >= 0){
left.Add(grid[r][c]);
r--;
c--;
}
r = x + 1;
c = y + 1;
while (r < n && c < m){
right.Add(grid[r][c]);
r++;
c++;
}
return Math.Abs(left.Count - right.Count);
}
}
}