My Code on this problem :
class Solution { public int numSubmatrixSumTarget(int[][] matrix, int target) { int count = 0; int line = matrix.length; int column = matrix[0].length + 1; int[][] sum = new int[line][column]; for (int i = 0; i < sum.length; i++){ for (int j = 1; j < sum[0].length; j++){ sum[i][j] = sum[i][j - 1] + matrix[i][j - 1]; } } for (int start = 0; start < column; start++){ for (int end = start + 1; end < column; end++ ){ int sumOfSubMatrix = 0; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); map.put(0, 1); for(int l = 0; l < line; l++){ sumOfSubMatrix += sum[l][end] - sum[l][start]; if (map.containsKey(sumOfSubMatrix - target)) count += map.get(sumOfSubMatrix - target); map.put(sumOfSubMatrix, map.getOrDefault(sumOfSubMatrix, 0) + 1); } } } return count; } }
Repo : github.com/Rohithv07/LeetCodeTopIn...
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My Code on this problem :
Repo : github.com/Rohithv07/LeetCodeTopIn...