voidmergeSort(int[] nums, int l, int r){ if (l >= r) return; int m = l + (r - l) / 2; mergeSort(nums, l, m); mergeSort(nums, m+1, r); int[] tmp = newint[r - l + 1]; int left = l, right = m+1, index = 0; while (left <= m && right <= r) { if (nums[left] < nums[right]) { tmp[index++] = nums[left++]; } else { tmp[index++] = nums[right++]; } } while (left <= m) tmp[index++] = nums[left++]; while (right <= r) tmp[index++] = nums[right++]; for (int t : tmp) { nums[l++] = t; } }
voidinsertionSort(int[] nums){ for (int i=0; i<nums.length; ++i) { for (int j=i; j>0; --j) { if (nums[j] < nums[j-1]) { int tmp = nums[j]; nums[j] = nums[j-1]; nums[j-1] = tmp; } } } }
冒泡排序
每一轮元素都和相邻元素比较,将较大的往右交换,即每一轮遍历都将一个最大的数冒泡至右端。
1 2 3 4 5 6 7 8 9 10 11 12
voidbubbleSort(int[] nums){ int n = nums.length; for (int i=0; i<n-1; ++i) { for (int j=0; j<n-i-1; ++j) { if (nums[j] > nums[j+1]) { int tmp = nums[j]; nums[j] = nums[j+1]; nums[j+1] = tmp; } } } }
选择排序
左边维护排好序的列表,每次遍历都找出右边最小的数,放在左边列表的右端。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
voidselectionSort(int[] nums){ int n = nums.length; for (int i=0; i<n; ++i){ int min = i; for (int j=i+1; j<n; ++j) { if (nums[j] < nums[min]) { min = j; } } if (min != i) { int tmp = nums[min]; nums[min] = nums[i]; nums[i] = tmp; } } }
classSolution{ privateint[][] directions = newint[][]{{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; publicintmaxAreaOfIsland(int[][] grid){ 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 = Math.max(res, dfs(grid, i, j)); } } } return res; }
// 在 grid 中包含 row,col的岛屿最大面积 privateintdfs(int[][] grid, int row, int col){ if (grid[row][col] == 0) { return0; } int area = 1; // 路过的格子需要清零 grid[row][col] = 0; for (int[] dir : directions) { int x = row + dir[0]; int y = col + dir[1]; if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length) { area += dfs(grid, x, y); } } return area; } }
classSolution{ privateint[][] directions = newint[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; publicintshortestBridge(int[][] grid){ int m = grid.length; int n = grid[0].length; Queue<Node> queue = new LinkedList<Node>(); // 先深度优先搜索 boolean flipped = false; for (int i=0; i<m; ++i) { for (int j=0; j<n; ++j) { if (grid[i][j] == 1) { dfs(grid, i, j, queue); flipped = true; break; } } // 用于跳出两层循环 if (flipped) break; } // 对第一个岛屿中的点进行一圈圈扩大,也就是广度优先搜索 int level = 0; while (!queue.isEmpty()) { int sz = queue.size(); ++ level; while (--sz >= 0) { Node node = queue.poll(); for (int[] dir : directions) { int x = node.i + dir[0]; int y = node.j + dir[1]; if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length) { if (grid[x][y] == 0) { grid[x][y] = 2; queue.offer(new Node(x, y)); } elseif (grid[x][y] == 1) { return level; } } } } } return level; }
// 深度优先搜索,遍历完第一个岛屿,并将其周围的0入队 privatevoiddfs(int[][] grid, int i, int j, Queue<Node> queue){ if (grid[i][j] == 0) { grid[i][j] = 2; queue.offer(new Node(i, j)); return; } if (grid[i][j] == 2) { return ; } // 为了防止干扰,访问过的点改为 2 grid[i][j] = 2; for (int[] dir : directions) { int x = i + dir[0]; int y = j + dir[1]; if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length) { dfs(grid, x, y, queue); } } }
staticclassNode{ publicint i; publicint j; publicNode(int i, int j){ this.i = i; this.j = j; } } }