
数据结构与算法
xiaoxianlv
这个作者很懒,什么都没留下…
展开
-
543. 二叉树的直径
class Solution0 { static int ans; public int diameterOfBinaryTree(TreeNode root) { if(root==null) return 0; ans = 1; depth(root); return ans - 1; } public int depth(TreeNode node) { if (node == null)原创 2022-03-02 17:00:27 · 226 阅读 · 0 评论 -
347. 前 K 个高频元素
使用堆class Solution { public int[] topKFrequent(int[] nums, int k) { Map<Integer, Integer> occurrences = new HashMap<Integer, Integer>(); for (int num : nums) { occurrences.put(num, occurrences.getOrDefault(num, 0原创 2022-03-02 16:39:00 · 91 阅读 · 0 评论 -
279. 完全平方数
public static void main(String[] args) { System.out.println(numSquares(2)); } // 13 4 9 public static int numSquares(int n) { int[] f = new int[n + 1]; for (int i = 1; i <= n; i++) { int minn = Integer...原创 2022-03-02 15:42:18 · 98 阅读 · 0 评论 -
239.滑动窗口最大值
class Solution2 { public static void main(String[] args) { int[] nums = {1, 3, -1, -3, 5}; int[] numd = {0, 1, 2, 3, 4}; int[] ints = maxSlidingWindow(nums, 3); System.out.println(Arrays.toString(ints)); } publ原创 2022-03-02 14:52:11 · 86 阅读 · 0 评论 -
打家劫舍 leetcode 198
class Solution1 { public int rob2(int[] nums) { if (nums == null || nums.length == 0) { return 0; } int length = nums.length; if (length == 1) { return nums[0]; } int[] dp = new in原创 2022-03-02 11:57:25 · 160 阅读 · 0 评论 -
实现前缀树-leetcode 208
class Trie { private Trie[] children; private boolean isEnd; public Trie() { children = new Trie[26]; isEnd = false; }// word work public void insert(String word) { Trie node = this; for (int i = 0; .原创 2022-03-02 11:56:01 · 133 阅读 · 0 评论 -
归并算法模板
https://www.cnblogs.com/of-fanruice/p/7678801.html原创 2022-02-11 13:16:24 · 90 阅读 · 0 评论 -
快排算法模板
原创 2022-02-11 13:15:33 · 121 阅读 · 0 评论