九章强化
第一周
三角形个数。
public int triangleCount(int s[]) { // write your code here if (s == null || s.length == 0) return 0; int res = 0; Arrays.sort(s); for (int i = 0; i < s.length; i++){ int left = 0; int right = i - 1; while (left < right){ if (s[left] + s[right] > s[i]){ res+=right - left; right--; } else{ left++; } } } return res; }
第二周
并查模版
int[] pre = new int[1000]; int find(int x){ int r = x; while (pre[r] != r){ r = pre[r]; } int i = x, j; while (i != r){ j = pre[i]; pre[i] = r; i = j; } return r; } void join(int x, int y){ int fx = find(x), fy = find(y); if (fx != fy){ pre[fx] = fy; } }
无向图的联通块
public class UserServiceImpl { public void addUser(){ System.out.println("addUser"); } public List<List<Integer>> connectedSet(ArrayList<UndirectedGraphNode> nodes) { HashSet<Integer> set = new HashSet<>(); for (UndirectedGraphNode node : nodes){ set.add(node.label); } Union union = new Union(set); for (UndirectedGraphNode node : nodes){ for (UndirectedGraphNode neighbor : node.neighbors){ union.union(node.label, neighbor.label); } } List<List<Integer>> res = new ArrayList<>(); HashMap<Integer, List<Integer>> all = new HashMap<>(); for (UndirectedGraphNode node : nodes){ Integer curfather = union.find(node.label); if (!all.containsKey(curfather)){ all.put(curfather, new ArrayList<Integer>()); } all.get(curfather).add(node.label); } for (List<Integer> list : all.values()){ res.add(list); } return res; } } class UndirectedGraphNode { int label; ArrayList<UndirectedGraphNode> neighbors; UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } }; class Union{ HashMap<Integer, Integer> map = new HashMap<>(); public Union(HashSet<Integer> set){ for (int now : set){ map.put(now, now); } } public int find(int x){ int r = map.get(x); while (r != map.get(r)){ r = map.get(r); } int j = x, i; while (j != r){ i = map.get(j); map.put(j, r); j = i; } return r; } public void union(int x, int y){ int fx = map.get(x), fy = map.get(y); if (fx != fy){ map.put(fx, fy); } } }
找小鸟数
Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is represented as the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent. Have you met this question in a real interview? Yes Example Given graph: [ [1, 1, 0, 0, 0], [0, 1, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1] ]
public int numIslands(char[][] grid) { if (grid == null || grid.length == 0) return 0; int res = 0; for (int i = 0; i < grid.length; i++){ for (int j = 0; j < grid[0].length; j++){ if (grid[i][j] == '1'){ res++; remove(grid, i, j); } } } return res; } public void remove(char[][] grid, int i, int j){ if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == '0') return; grid[i][j] = '0'; remove(grid, i - 1, j); remove(grid, i + 1, j); remove(grid, i, j - 1); remove(grid, i, j + 1); }
算小鸟数2
岛屿的个数II 描述 笔记 数据 评测 给定 n,m,分别代表一个2D矩阵的行数和列数,同时,给定一个大小为 k 的二元数组A。起初,2D矩阵的行数和列数均为 0,即该矩阵中只有海洋。二元数组有 k 个运算符,每个运算符有 2 个整数 A[i].x, A[i].y,你可通过改变矩阵网格中的A[i].x],[A[i].y] 来将其由海洋改为岛屿。请在每次运算后,返回矩阵中岛屿的数量。 注意事项 0 代表海,1 代表岛。如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。 您在真实的面试中是否遇到过这个题? Yes 样例 给定 n = 3, m = 3, 二元数组 A = [(0,0),(0,1),(2,2),(2,1)]. 返回 [1,1,2,2].
public class UserServiceImpl { public void addUser(){ System.out.println("addUser"); } public int converToId(int x, int y, int m){ return x * m + y; } public List<Integer> numIslands2(int n, int m, Point[] operators) { List<Integer> res = new ArrayList<>(); if (operators == null) { return res; } int[] dx = {0, 0, 1, -1}; int[] dy = {1, -1, 0, 0}; int count = 0; Union union = new Union(n, m); int[][] island = new int[n][m]; for (int i = 0; i < operators.length; i++){ count++; int x = operators[i].x; int y = operators[i].y; if (island[x][y] != 1){ island[x][y] = 1; int id = converToId(x, y, m); for (int j = 0; j < 4; j++){ int newX = x + dx[j]; int newY = y + dy[y]; if (newX >= 0 && newX < n && newY >= 0 && newY < m && island[newX][newY] == 1){ int newId = converToId(newX, newY, m); int fa = union.find(id); int newfa = union.find(newId); if (fa != newfa){ count--; union.union(fa, newfa); } } } } res.add(count); } return res; } class Point { int x; int y; Point() { x = 0; y = 0; } Point(int a, int b) { x = a; y = b; } } class Union{ HashMap<Integer, Integer> map = new HashMap<>(); public Union(int n, int m){ for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ map.put(converToId(i, j, m), converToId(i, j, m)); } } } public int converToId(int x, int y, int m){ return x * m + y; } public int find(int x){ int r = map.get(x); while (r != map.get(r)){ r = map.get(r); } int j = x, i; while (j != r){ i = map.get(j); map.put(j, r); j = i; } return r; } public void union(int x, int y){ int fx = map.get(x), fy = map.get(y); if (fx != fy){ map.put(fx, fy); } } } }
图是否是树
复制代码 给出 n 个节点,标号分别从 0 到 n - 1 并且给出一个 无向 边的列表 (给出每条边的两个顶点), 写一个函数去判断这张`无向`图是否是一棵树 注意事项 你可以假设我们不会给出重复的边在边的列表当中. 无向边 [0, 1] 和 [1, 0] 是同一条边, 因此他们不会同时出现在我们给你的边的列表当中。 您在真实的面试中是否遇到过这个题? Yes 样例 给出n = 5 并且 edges = [[0, 1], [0, 2], [0, 3], [1, 4]], 返回 true. 给出n = 5 并且 edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], 返回 false. 标签 相关题目
public class Solution { /** * @param n an integer * @param edges a list of undirected edges * @return true if it's a valid tree, or false */ public boolean validTree(int n, int[][] edges) { // Write your code here if (edges == null || edges.length == 0 || n == 0) return; Union u = new Union(n); for (int i = 0; i < edges.length; i++){ if (u.find(edges[i][0] == u.find(edges[i][1]){ return false; } u.union(edges[i][0], edges[i][1]); } return true; } } class Union{ HashMap<Integer, Integer> map = new HashMap<>(); public Union(int n){ for (int i = 0; i < n; i++){ map.put(i, i); } } public int find(int x){ r = map.get(x); while (r != map.get(r)){ r = map.get(r); } int j = x, i; while (r != j){ i = map.get(j); map.put(j, r); j = i; } return r; } public void union(int x, int y){ int fx = find(x), fy = find(y); if (fx != fy){ map.put(x, fy); } } }
Number of Airplanes in the Sky
class Solution { /** * @param intervals: An interval array * @return: Count of airplanes are in the sky. */ public int countOfAirplanes(List<Interval> airplanes) { // write your code here List<Point> list = new ArrayList<Point>(); for (Interval i : airplanes){ list.add(new Point(i.start, 1)); list.add(new Point(i.end, 0)); } Collections.sort(list, new Comparator<Point>() { public int compare(Point p1, Point p2) { if (p1.time == p2.time) { return p1.flag - p2.flag; } else { return p1.time - p2.time; } } }); int count = 0, res = 0; for (Point p : list){ if (p.flag == 1){ count++; } else{ count--; } res = Math.max(count, res); } return res; } } class Point{ int time; int flag; Point(int time, int flag){ this.time = time; this.flag = flag; } }
描述
笔记
数据
评测
给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机?
注意事项
如果多架飞机降落和起飞在同一时刻,我们认为降落有优先权。
您在真实的面试中是否遇到过这个题? Yes
样例
对于每架飞机的起降时间列表:[[1,10],[2,3],[5,8],[4,7]], 返回3。
第三周
左右指针‘
’1,灌水
接雨水 描述 笔记 数据 评测 给出 n 个非负整数,代表一张X轴上每个区域宽度为 1 的海拔图, 计算这个海拔图最多能接住多少(面积)雨水。 接雨水 您在真实的面试中是否遇到过这个题? Yes 样例 如上图所示,海拔分别为 [0,1,0,2,1,0,1,3,2,1,2,1], 返回 6. 挑战
/** * @param heights: an array of integers * @return: a integer */ public int trapRainWater(int[] heights) { // write your code here if (null == heights || heights.length == 0) { return 0; } int ans = 0; int start = 0; int end = heights.length - 1; while (start < end) { if (heights[start] < heights[end]) { int smaller = heights[start]; while (start < end && heights[start] <= smaller) { ans += smaller - heights[start]; start++; } } else { int smaller = heights[end]; while (start < end && heights[end] <= smaller) { ans += smaller - heights[end]; end--; } } } return ans; } }
2 灌水 407 leet
public int trapRainWater(int[][] heights) { if (heights == null || heights.length == 0 || heights[0].length == 0) return 0; int m = heights.length; int n = heights[0].length; PriorityQueue<Cell> pq = new PriorityQueue<>((v1,v2)->v1.h - v2.h); boolean[][] visited = new boolean[m][n]; for (int i = 0; i < n; i++){ visited[0][i] = true; pq.offer(new Cell(0, i, heights[0][i])); visited[m - 1][i] = true; pq.offer(new Cell(m - 1, i, heights[m-1][i])); } for (int i = 0; i < m; i++){ visited[i][0] = true; pq.offer(new Cell(i, 0, heights[i][0])); visited[i][n - 1] = true; pq.offer(new Cell(i, n - 1, heights[i][n-1])); } int[] dx = {0, 0, 1, -1}; int[] dy = {1, -1, 0, 0}; int res = 0; while (!pq.isEmpty()){ Cell cell = pq.poll(); for (int i = 0; i < 4; i++){ int newX = cell.x + dx[i]; int newY = cell.y + dy[i]; if (newX >= 0 && newX < m && newY >= 0 && newY < n && !visited[newX][newY]){ visited[newX][newY] = true; pq.offer(new Cell(newX, newY, Math.max(cell.h, heights[newX][newY]))); res += Math.max(0, cell.h - heights[newX][newY]); } } } return res; } class Cell{ int x, y , h; Cell(int x, int y, int h){ this.x = x; this.y = y; this.h = h; }
2 房子轮廓
public List<int[]> getSkyline(int[][] build) { List<int[]> res = new ArrayList<>(); List<int[]> points = new ArrayList<>(); for (int i = 0; i < build.length; i++){ points.add(new int[]{build[i][0], build[i][2]}); points.add(new int[]{build[i][1], -build[i][2]}); } Collections.sort(points, new Comparator<int[]>() { @Override public int compare(int[] a, int[] b) { if(a[0]!=b[0]) return a[0] - b[0]; else return b[1] - a[1]; } }); Queue<Integer> pq = new PriorityQueue<>((a, b) -> (b - a)); int cur = 0, pre = 0; for (int[] point : points){ if (point[1] > 0){ pq.offer(point[1]); cur = pq.peek(); } else { pq.remove(-point[1]); cur = pq.isEmpty()? 0 : pq.peek(); } if (cur != pre){ res.add(new int[]{point[0], cur}); pre = cur; } } return res; }
3 堆化
public void heapify(int[] A) { // write your code here for (int i = A.length / 2; i >= 0; i--){ shiftdown(A, i); } } public void shiftdown(int[] a, int k){ while (k < a.length){ int small = k; if (k * 2 + 1 < a.length && a[k * 2 + 1] < a[small]){ small = k * 2 + 1; } if (k * 2 + 2 < a.length && a[k * 2 + 2] < a[small]){ small = k * 2 + 2; } if (small == k){ break; } int temp = a[k]; a[k] = a[small]; a[small] = temp; k = small; } }
4 Data Stream Median
数据流中位数
public int[] medianII(int[] nums) { // write your code here Comparator<Integer> cmp = new Comparator<Integer>(){ public int compare(Integer left, Integer right){ return right.compareTo(left); } }; int len = nums.length; PriorityQueue<Integer> maxQ = new PriorityQueue(len, cmp); PriorityQueue<Integer> minQ = new PriorityQueue(len); int[] res = new int[len]; maxQ.add(nums[0]); res[0] = nums[0]; for (int i = 1; i < nums.length; i++){ int x = maxQ.peek(); if (x >= nums[i]){ maxQ.add(nums[i]); } else { minQ.add(nums[i]); } if (maxQ.size() > minQ.size() + 1){ minQ.add(maxQ.poll()); } else if (maxQ.size() < minQ.size()){ maxQ.add(minQ.poll()); } res[i] = maxQ.peek(); } return res; }
5 滑动窗口中位数
public ArrayList<Integer> medianSlidingWindow(int[] nums, int k) { // write your code here ArrayList<Integer> res = new ArrayList<>(); if (nums == null || nums.length == 0 || k <= 0) return res; int i = 0, j = 0, find = 0; List<Integer> list = new ArrayList<>(); for (; i < k - 1; i++){ list.add(nums[i]); } for (; i < nums.length; i++){ list.add(nums[i]); Collections.sort(list); res.add(list.get((k - 1 )/ 2)); for (int f = 0; f < list.size() && j < nums.length; f++){ if (list.get(f) == nums[j]){ find = f; break; } } list.remove(find); j++; } return res; }
6滑动窗口最大 239
public class Solution { public int[] maxSlidingWindow(int[] nums, int k) { if (nums == null || nums.length == 0 || k <= 0){ return new int[0]; } int[] res = new int[nums.length - k + 1]; int index = 0; Deque<Integer> q = new ArrayDeque<>(); for (int i = 0; i < nums.length; i++){ if (!q.isEmpty() && q.peek() == i - k){ q.poll(); } while (!q.isEmpty() && nums[q.peekLast()] < nums[i]){ q.pollLast(); } q.add(i); if (i >= k - 1){ res[index++] = nums[q.peek()]; } } return res; } }
第四周 双指针
Nuts 和 Bolts 的问题
public void sortNutsAndBolts(String[] nuts, String[] bolts, NBComparator compare) { // write your code here for (int i = 0; i < nuts.length; i++){ for (int j = i; j < bolts.length; j++){ if (compare.cmp(nuts[i], bolts[j]) == 0){ String item = bolts[j]; bolts[j] = bolts[i]; bolts[i] = temp; break; } } } }
public void sortNutsAndBolts(String[] nuts, String[] bolts, NBComparator compare) { // write your code here sort(nuts, bolts, 0, nuts.length - 1; compare); } public void sort(string[] nuts, String[] bolts, int l, int h, NBComparator compare){ if (l < h){ int p = partion(nuts, l, h, bolts[h], compare); partion(bolts, l, h, nuts[p]); sort(nuts, bolts, l, p, compare); sort(nuts, bolts, p + 1, h, compare); } } public int partion(String[] strs, int l, int h, String str, NBComparator compare){ for (int i = l; i <= h; i++){ if (compare(strs[i], str) == 0 || compare(str, strs[i]) == 0){ swap(strs, i, l); break; } } String now = strs[l]; int left = l, right = r; while (left < right){ while (compare(strs[right], str) == -1 || compare(str, strs[right]) == 1){ right--; } strs[left] = strs[right]; while (compare(strs[left], str) == -1 || compare(str, strs[left]) == 1){ left++; } } strs[left] = now; return left; } public void swap(String[] strs, int l, int r){ String item = strs[l]; strs[l] = strs[r]; strs[r] = item; }
leetCode 209 Minimum Size Subarray 双指针
public int minSubArrayLen(int s, int[] nums) { if (nums == null || nums.length == 0) return 0; int i = 0, j = 0, sum = 0, min = Integer.MAX_VALUE; while (j < nums.length) { sum += nums[j++]; while (sum >= s) { min = Math.min(min, j - i); sum -= nums[i]; i++; } } return min == Integer.MAX_VALUE ? 0 : min; }
3. Longest Substring Without Repeating Characters
public int lengthOfLongestSubstring(String s) { if (s == null || s.length() == 0) return 0; Set<Character> set = new HashSet<>(); int i = 0, j = 0, max = 0; while (j < s.length()) { char c = s.charAt(j); if (!set.contains(c)) { set.add(c); j++; max = Math.max(set.size(), max); }else { set.remove(s.charAt(i++)); } } return max; }
leetcode 304 最长的k个字符
public int lengthOfLongestSubstringKDistinct(String s, int k) { if (s == null || s.length() == 0 || k <= 0) return 0; HashMap<Character, Integer> map = new HashMap<>(); int j = 0; int max = Integer.MAX_VALUE; for (int i = 0; i < s.length(); i++) { while (j < s.length()) { char c = s.charAt(j); if (map.containsKey(c)) { map.put(c, map.get(c) + 1); } else { if (map.size() == k) { break; } map.put(c, 1); } j++; } if (map.size() <= k) { max = Math.max(max, j - i); } if (map.get(s.charAt(i)) == 1) { map.remove(s.charAt(i)); } else { map.put(s.charAt(i), map.get(s.charAt(i)) - 1); } } return max; }
第五周 动态规划
1 lintCode huose rabber 打劫房屋
public long houseRobber(int[] a) { // write your code here if (a == null || a.length == 0) return 0; if (a.length == 1) return (long)a[0]; if (a.length == 2) return (long)Math.max(a[0], a[1]); long[] res = new long[2]; res[0] = (long)a[0]; res[1] = (long)Math.max(a[0], a[1]); for (int i = 2; i < a.length; i++){ res[i%2] = Math.max(res[(i-1)%2], res[(i-2)%2] + a[i]); } return res[(a.lenth - 1) % 2]; }
2 lintCode 打劫房屋2
public int houseRobber2(int[] nums) { // write your code here if (nums == null || nums.length == 0) return 0; if (nums.length == 1) return nums[0]; if (nums.length == 2) return Math.max(nums[0], nums[1]); int[] a1 = new int[nums.length - 1]; int[] a2 = new int[nums.length - 1]; for (int i = 0; i < nums.length - 1; i++){ a1[i] = nums[i]; } for (int i = 1; i < nums.length; i++){ a2[i - 1] = nums[i]; } return Math.max(houseRobber(a1), houseRobber(a2)); } public int houseRobber(int[] nums){ int[] dp = new int[2]; dp[0] = nums[0]; dp[1] = Math.max(nums[0], nums[1]); for (int i = 2; i < nums.length; i++){ dp[i % 2] = Math.max(dp[(i - 1) % 2], dp[(i - 2) % 2] + nums[i]); } return dp[(nums.length - 1) % 2]; }
3 爬楼梯
4 221 Maximal Square
public int maximalSquare(char[][] matrix) { if(matrix.length == 0) return 0; int m = matrix.length, n = matrix[0].length, res = 0; int[][] dp = new int[m + 1][n + 1]; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (matrix[i - 1][j - 1] == '1') { dp[i][j] = Math.min(dp[i-1][j-1], Math.min(dp[i-1][j], dp[i][j-1])) + 1; res = Math.max(res, dp[i][j]); } } } return res*res; }
5 300 longest Increasng subseq
public int longestIncreasingSubsequence(int[] nums) { // write your code here if (nums == null || nums.length == 0) return 0; int[] dp = new int[nums.length]; int max = 0; for (int i = 0; i < nums.length; i++){ dp[i] = 1; for (int j = 0; j < i; j++){ if (nums[j] < nums[i]){ dp[i] = Math.max(dp[i], dp[j] + 1); } max = Math.max(max, dp[i]); } } return max; }
6 二位数组,最长上升子序列 329
public class Solution { int[] dx = {0, 0, 1, -1}; int[] dy = {1, -1, 0, 0}; int m = 0; int n = 0; public int longestIncreasingPath(int[][] matrix) { if (matrix.length == 0) return 0; m = matrix.length; n = matrix[0].length; int max = 0; int[][] cat = new int[m][n]; for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ cat[i][j] = dfs(matrix, cat, i, j); max = Math.max(cat[i][j], max); } } return max; } public int dfs(int[][] matrix, int[][] cat, int x, int y){ if (cat[x][y] != 0) return cat[x][y]; int max = 1; for (int i = 0; i < 4; i++){ int nx = dx[i] + x; int ny = dy[i] + y; if (nx >= 0 && nx < m && ny >= 0 && ny < n && matrix[nx][ny] > matrix[x][y]){ int len = 1 + dfs(matrix, cat, nx, ny); max = Math.max(max, len); } } cat[x][y] = max; return max; }
7 coins in a line
public boolean firstWillWin(int n) { // write your code here if (n % 3 == 0) return false; return true; }
8 coins in a line 2
public boolean firstWillWin(int[] values) { // write your code here if (values.length <= 2) return true; int m = values.length; int[] dp = new int[m + 1]; dp[m] = 0; dp[m - 1] = values[m - 1]; dp[m - 2] = values[m - 1] + values[m - 2]; dp[m - 3] = values[m - 3] + values[m - 2]; for (int i = m - 4; i >= 0; i--){ dp[i] = Math.max(values[i] + Math.min(dp[i+2],dp[i+3]), values[i] + values[i+1] + Math.min(dp[i+3], dp[i+4])); } int sum = 0; for (int i : values){ sum += i; } int sec = sum - dp[0]; return dp[0] > sec; }
9 coins in a line 3
10 374 375 guss number height ro low
11 最大子数组 53
public int maxSubArray(int[] nums) { int maxsum = nums[0], thissum = nums[0]; for (int i = 1; i < nums.length; i++) { thissum = Math.max(thissum + nums[i], nums[i]); maxsum = Math.max(thissum, maxsum); } return maxsum; }
12 最大连乘 152
public int maxProduct(int[] nums) { if (nums == null || nums.length == 0) return 0; if (nums.length == 1) return nums[0]; int max = nums[0], min = nums[0], maxg = nums[0]; for (int i = 1; i < nums.length; i++) { int a = max * nums[i]; int b = min * nums[i]; max = Math.max(nums[i], Math.max(a, b)); min = Math.min(nums[i], Math.min(a, b)); maxg = Math.max(max, maxg); } return maxg; }
第六周 动态规划
1 石头归并
2 爆炸气球
3 背包问题
第七周 、
寻找峰值
public int findPeak(int[] a) { // write your code here if (a == null && a.length < 3) return -1; int l = 1, r = a.length - 2; while (l <= r){ int m = (r - l) / 2 + l; if (a[m] > a[m - 1] && a[m] > a[m + 1]) return m; if (a[m - 1] > a[m]) r = m - 1; else l = m + 1; } return -1; }
寻找峰值2
public int findPeak(int[] a) { // write your code here if (a == null && a.length < 3) return -1; int l = 1, r = a.length - 2; while (l <= r){ int m = (r - l) / 2 + l; if (a[m] > a[m - 1] && a[m] > a[m + 1]) return m; if (a[m - 1] > a[m]) r = m - 1; else l = m + 1; } return -1; } public List<Integer> findPeakII(int[][] a) { List<Integer> res = new ArrayList<>(); int l = 1, r = a.length - 2; while (l <= r){ int mid = (l + r) / 2; int col = findPeak(a[m]); if (a[mid][col] > a[mid + 1][col] && a[mid][col] > a[mid - 1][col]){ res.add(mid); res.add(col); } if (a[mid][col] < a[mid+1][col]){ l = mid + 1; } else { r = mid - 1; } } return -1; }
子数组和 和为0 的全部数组
和为0 的子矩阵
public int[][] submatrixSum(int[][] matrix) { // Write your code here int[][] res = new int[2][2]; if (matrix == null || matrix.length == 0) return res; int m = matrix.length, n = matrix[0].length; int[][] sum = new int[m + 1][n + 1]; for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ sum[i + 1][j + 1] = matrix[i][j] + sum[i + 1][j] + sum[i][j + 1] - sum[i][j]; } } for (l = 0; l < m; l++){ for (h = l + 1; h <= m; h++){ HashMap<Integer, Integer> map = new HashMap<>(); for (int col = 0; col <= n; col++){ int diff = sum[h][col] - sum[l][col]; if (map.containsKey(diff)){ int k = map.get(diff); result[0][0] = l; result[0][1] = k; result[1][0] = h-1; result[1][1] = col-1; return res; }else { map.put(diff, col) } } } } return res; }