摘要:
JAVA DFS 标记解法: public final int countBattleshipsDFS(char[][] board) { if (board.length == 0 || board[0].length == 0) return 0; int res = 0; for (int i 阅读全文
摘要:
最优解法为借助贝祖定理计算最大公约数,但本次只拿出 BFS 解法。个人觉着虽然效率低下,但BFS 解法更接近计算机思维,我们告诉计算机如何去做,其余的由来计算机完成。更符合刷题的初衷。 JAVA: private final boolean canMeasureWaterBFS(int x, int 阅读全文
摘要:
设计逻辑运算,真值表达式为: public final int singleNumber0(int[] nums) { int x = 0, y = 0; for (int i = 0; i < nums.length; i++) { int z = nums[i]; x = (x & ~y & ~ 阅读全文
摘要:
常规计数: public final int singleNumber0(int[] nums) { Arrays.sort(nums); for (int i = 0; i < nums.length; i++) { if (i > 0) { if (nums[i] == nums[i - 1]) 阅读全文
摘要:
JAVA: public final int rob(TreeNode root) { Map<String, Integer> cache = new HashMap<String, Integer>(); return Math.max(rob(root, true, cache), rob(r 阅读全文
摘要:
概率问题,蓄水池抽样与生成随机数都可以解决。蓄水池抽样适合数据量巨大且不知道数组集合大小时的随机抽样,因为是线性扫描每一个元素进行抽样,空间复杂度为 O(1) ,时间复杂度为O(n)。 蓄水池抽样算法: class Solution { ListNode head; Random random; p 阅读全文
摘要:
JAVA: public final int findPoisonedDuration(int[] timeSeries, int duration) { if (timeSeries.length == 0 || duration == 0) { return 0; } int cur = 1; 阅读全文
摘要:
JAVA 剪枝前: public final int findContentChildren(int[] g, int[] s) { if (s.length == 0 || g.length == 0) { return 0; } Arrays.sort(s); int re = 0; int s 阅读全文
摘要:
JAVA: public final int maxNonOverlapping(int[] nums, int target) { int re = 0; int point = 0; while (point < nums.length) { int sum = 0; Set<Integer> 阅读全文
摘要:
JAVA DFS 解法: public final int numIslands(char[][] grid) { int re = 0; for (int x = 0; x < grid.length; x++) { for (int y = 0; y < grid[0].length; y++) 阅读全文