随笔分类 - Problem Archive
摘要:128 最长连续序列 public class Solution { public int longestConsecutive(int[] nums) { if (nums == null || nums.length == 0) return 0; int ans = 1; HashMap<In
阅读全文
摘要:CD79 一种消息接收并打印的结构设计 public class CD79_1 { public static class Node { public int num; public Node next; public Node(int num) { this.num = num; } } publ
阅读全文
摘要:CD66 并查集的实现 public class CD66_1 { public static class Solution { int[] f; public Solution(int n) { f = new int[n]; Arrays.fill(f, -1); } private int f
阅读全文
摘要:CDxxx 从 5 随机到 7 随机及其扩展 /* rand1To5实现等概率随机产生1~7的随机函数rand1To7 */ public int rand1To5() { return (int) (Math.random() * 5) + 1; } public int rand1To7() {
阅读全文
摘要:CD26 子数组的最大累加和问题 public class CD26_1 { public static int solution(int[] arr) { int ans = -1, sum = 0; for (int num : arr) { if (sum + num > 0) { sum +
阅读全文
摘要:CD149 转圈打印矩阵 public class CD149_1 { public static void solution(int[][] arr) { int up = 0, down = arr.length - 1, left = 0, right = arr[0].length - 1;
阅读全文
摘要:CD142 不用额外变量交换两个整数的值 /* 模拟 */ public class CD142_1 { public static void solution(int a, int b) { a = a ^ b; b = a ^ b; a = a ^ b; System.out.println(a
阅读全文
摘要:CD126 括号字符串的有效性 /* 模拟 */ public class CD126_1 { public static String solution(String s) { int l = 0, r = 0; for (char ch : s.toCharArray()) { if (ch =
阅读全文
摘要:CD95 判断两个字符串是否互为变形词 /* 模拟 */ public class CD95_1 { public static boolean solution(String s1, String s2) { if (s1.length() != s2.length()) return false
阅读全文
摘要:CD42 子数组异或和为 0 的最多划分⭐ /*⭐DP⭐*/ public class CD42_1 { public static int solution(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); int[] dp
阅读全文
摘要:CD183 斐波那契数列问题的递归和动态规划1 /* * 矩阵快速幂 * [f(n), f(n-1)] = [1, 1] x [[1, 1], [1, 0]]^(n-2) */ public class CD183_1 { public static long solution(long n) {
阅读全文
摘要:CD172 判断二叉树是否为平衡二叉树 /* 递归 */ public class CD172_1 { public static class TreeNode { public int val; public TreeNode left; public TreeNode right; public
阅读全文
摘要:CD161 用递归和非递归方式实现二叉树先序、中序和后序遍历❗ public class CD161_1 { public static class TreeNode { public int val; public TreeNode left; public TreeNode right; pub
阅读全文
摘要:CDxxx 两个单链表相交的一系列问题⭐ 剑指offer 链表篇 JZ52 两个链表的第一个公共结点 剑指offer 链表篇 JZ23 链表中环的入口结点 public Node getIntersectNode(Node head1, Node head2) { if (head1 == null
阅读全文
摘要:CD48 打印两个有序链表的公共部分 /* 归并 */ public class CD48_1 { public static class ListNode { public int val; public ListNode next = null; public ListNode(int val)
阅读全文
摘要:CD5 设计一个有 getMin 功能的栈 /* * 维护一个最小栈minStack * dataStack每压入一个数, minStack也压入一个当前状态的最小值 */ public class CD5_1 { public static class Solution { public Stac
阅读全文
摘要:JZ42 连续子数组的最大和 /* 贪心 */ public class JZ42_1 { public static int FindGreatestSumOfSubArray(int[] array) { int sum = 0, res = Integer.MIN_VALUE; for (in
阅读全文
摘要:JZ58 左旋转字符串 /* 模拟 */ public class JZ58_1 { public static String LeftRotateString(String str, int n) { if (str.length() == 0) return ""; n %= str.lengt
阅读全文
摘要:JZ66 构建乘积数组 /* 暴力 */ public class JZ66_1 { public static int[] multiply(int[] A) { int[] res = new int[A.length]; Arrays.fill(res, 1); for (int i = 0;
阅读全文
摘要:JZ36 二叉搜索树与双向链表⭐ 1 /* 中序递归 */ 2 public class JZ36_1 3 { 4 public static TreeNode Convert(TreeNode pRootOfTree) 5 { 6 inOrder(pRootOfTree); 7 TreeNode
阅读全文