随笔分类 - 数据结构与算法
摘要:概率问题,蓄水池抽样与生成随机数都可以解决。蓄水池抽样适合数据量巨大且不知道数组集合大小时的随机抽样,因为是线性扫描每一个元素进行抽样,空间复杂度为 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++)
阅读全文
摘要:回溯遍历去重题。 JAVA: List<List<Integer>> reList = new LinkedList<List<Integer>>(); public List<List<Integer>> findSubsequences(int[] nums) { search(nums, -1
阅读全文
摘要:JAVA: public final boolean isHappy(int n) { int next = next(n); while (n != 1 && n != next) { n = next(n); next = next(next(next)); } return n == 1; }
阅读全文
摘要:朴素解法,逻辑的渐进。 JAVA: public final int[][] merge(int[][] intervals) { sort(intervals); Stack<int[]> reStack = new Stack<int[]>(); for (int i = 0; i < inte
阅读全文
摘要:思路比较简单,可以采用计数法,不知为何会被分为中等。 JAVA: public final void sortColors(int[] nums) { int num0 = 0; int num1 = 0; int num2 = 0; for (int i = 0; i < nums.length;
阅读全文
摘要:思路很明显,折半查找。 JAVA: public final boolean searchMatrix(int[][] matrix, int target) { if (matrix.length == 0 || matrix[0].length == 0) { return false; } r
阅读全文
摘要:JAVA 哈希去重: final Set<String> reSet = new HashSet<String>(); public final String[] permutation(String S) { search(S, new HashSet<Integer>(), new String
阅读全文
摘要:JAVA: int an = 0; public final int countArrangement(int N) { search(N, new HashSet<Integer>(), 0); return an; } private final void search(int n, Set<I
阅读全文
摘要:JAVA 分治: public final int mincostTickets(int[] days, int[] costs) { return minCost(days, costs, days.length - 1, new int[days.length]); } private fina
阅读全文
摘要:JAVA : List<String> reList = new LinkedList<String>(); public List<String> generateParenthesis(int n) { search(0, n, n, new StringBuilder()); return r
阅读全文
摘要:Kadnae 解法: var kConcatenationMaxSum = function (arr, k) { let sum = arr[0]; let newK = k > 2 ? 2 : k; let pre = 0; let an = 0; for (let i = 0; i < new
阅读全文
摘要:暴力: final int mod = 1000000007; int an = 0; public final int kConcatenationMaxSum(int[] arr, int k) { int length = arr.length; int newLength = length
阅读全文
摘要:JAVA 回溯解法: public final String[] permutation(String s) { Set<String> reSet = new HashSet<String>(); search(s, 0, reSet, new StringBuilder(), new HashS
阅读全文
摘要:回溯解法 JAVA: public final List<Integer> splitIntoFibonacci(String S) { List<Integer> reList = new LinkedList<Integer>(); search(S, 0, reList, 0); return
阅读全文
摘要:回溯题目,JAVA 实现: public final String[] permutation(String S) { Set<String> reSet = new HashSet<String>(); permutation(S.toCharArray(), new StringBuilder(
阅读全文
摘要:回溯解法,java: public final List<List<String>> partition(String s) { List<List<String>> reList = new LinkedList<List<String>>(); partition(s, 0, new Stack
阅读全文