+关注继续查看

# 1. 二叉搜索树的最近公共祖先

所有节点的值都是唯一的。

p、q 为不同节点且均存在于给定的二叉搜索树中。

https://edu.csdn.net/practice/26945721

import java.util.*;
public class lowestCommonAncestor {
public final static int NULL = Integer.MIN_VALUE; //用NULL来表示空节点
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public static class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (p.val < root.val && q.val < root.val)
return lowestCommonAncestor(root.left, p, q);
else if (p.val > root.val && q.val > root.val)
return lowestCommonAncestor(root.right, p, q);
else
return root;
}
}
public static TreeNode createBinaryTree(Vector<Integer> vec) {
if (vec == null || vec.size() == 0) {
return null;
}
TreeNode root = new TreeNode(vec.get(0));
queue.offer(root);
int i = 1;
while (!queue.isEmpty()) {
int size = queue.size();
for (int j = 0; j < size; j++) {
TreeNode node = queue.poll();
if (i < vec.size() && vec.get(i) != NULL) {
node.left = new TreeNode(vec.get(i));
queue.offer(node.left);
}
i++;
if (i < vec.size() && vec.get(i) != NULL) {
node.right = new TreeNode(vec.get(i));
queue.offer(node.right);
}
i++;
}
}
return root;
}
public static void main(String[] args) {
Solution s = new Solution();
Integer[] nums = {6,2,8,0,4,7,9,NULL,NULL,3,5};
Vector<Integer> vec = new Vector<Integer>(Arrays.asList(nums));
TreeNode root = createBinaryTree(vec);
TreeNode p = new TreeNode(2);
TreeNode q = new TreeNode(8);
System.out.println(s.lowestCommonAncestor(root, p, q).val);
Integer[] nums2 = {6,2,8,0,4,7,9,NULL,NULL,3,5};
vec = new Vector<Integer>(Arrays.asList(nums2));
root = createBinaryTree(vec);
p = new TreeNode(2);
q = new TreeNode(4);
System.out.println(s.lowestCommonAncestor(root, p, q).val);
}
}

6

2

# 2. 随机分组问题

https://edu.csdn.net/practice/26945722

import java.util.*;
class StringToDateDemo {
public static void main(String args[]) {
ArrayList<String> teams = new ArrayList<String>() {
{
}
};
Collections.shuffle(teams);
ArrayList<String> group1 = new ArrayList<String>();
ArrayList<String> group2 = new ArrayList<String>();
ArrayList<String> group3 = new ArrayList<String>();
ArrayList<String> group4 = new ArrayList<String>();
group1.addAll(teams.subList(0, teams.size() / 4 + teams.size() % 4));
group2.addAll(teams.subList(teams.size() / 4 + teams.size() % 4, 2 * teams.size() / 4 + teams.size() % 4));
group3.addAll(teams.subList(2*teams.size() / 4 + teams.size() % 4, 3 * teams.size() / 4 + teams.size() % 4));
group4.addAll(teams.subList(3*teams.size() / 4 + teams.size() % 4, teams.size()));
}
}

[h, a, i, n]

[j, l, k, e]

[b, p, g, c]

[o, d, f, m]

Tips： 因为是连续字母，ArrayList<String> teams 用循环赋值即可。

ArrayList<String> teams = new ArrayList<>();
for (int i = 0; i < 16; i++) {
}

# 3. K 个一组翻转链表

• 你可以设计一个只使用常数额外空间的算法来解决此问题吗？
• 你不能只是单纯的改变节点内部的值，而是需要实际进行节点交换。

列表中节点的数量在范围 sz 内

1 <= sz <= 5000

0 <= Node.val <= 1000

1 <= k <= sz

Java

public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
return null;
}
for (int i = 0; i < k; i++) {
if (b == null) {
return a;
}
b = b.next;
}
a.next = reverseKGroup(b, k);
}
public ListNode reverse(ListNode a, ListNode b) {
ListNode pre, cur, nxt;
pre = null;
cur = a;
nxt = a;
while (nxt != b) {
__________________;
}
return pre;
}
}




https://edu.csdn.net/practice/26945723

import java.util.*;
public class reverseKGroup {
public static class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
public static class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
return null;
}
for (int i = 0; i < k; i++) {
if (b == null) {
return a;
}
b = b.next;
}
a.next = reverseKGroup(b, k);
}
public ListNode reverse(ListNode a, ListNode b) {
ListNode pre, cur, nxt;
pre = null;
cur = a;
nxt = a;
while (nxt != b) {
nxt = cur.next;
cur.next = pre;
pre = cur;
cur = nxt;
}
return pre;
}
}
public static ListNode createLinkedList(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
for (int i = 1; i < nums.length; i++) {
cur.next = new ListNode(nums[i]);
cur = cur.next;
}
}
while (cur != null) {
System.out.print(cur.val + "->");
cur = cur.next;
}
System.out.println("null");
}
public static void main(String[] args) {
Solution s = new Solution();
int[] nums = {1,2,3,4,5};
}
}

1->2->3->4->5->null

2->1->4->3->5->null

3->2->1->4->5->null

1->2->3->4->5->null

|
4月前
|
Java
Java每日一练(20230518) 移除元素、跳跃游戏II、复原IP地址
Java每日一练(20230518) 移除元素、跳跃游戏II、复原IP地址
45 0
|
4月前
|

Java每日一练(20230517) 重复元素、链表重复元素、旋转数组
Java每日一练(20230517) 重复元素、链表重复元素、旋转数组
34 0
|
4月前
|
Java
Java每日一练(20230516) 最小栈、组合总和II、相同的树
Java每日一练(20230516) 最小栈、组合总和II、相同的树
37 0
|
4月前
|

Java每日一练(20230515) 阶乘后的零、矩阵置零、两数相除
Java每日一练(20230515) 阶乘后的零、矩阵置零、两数相除
59 0
|
4月前
|
Java
Java每日一练(20230514) 滑动窗、最大子序和、转罗马数字
Java每日一练(20230514) 滑动窗、最大子序和、转罗马数字
50 0
|
4月前
|

Java每日一练(20230513) 输出最值、盛水容器、旋转数组II
Java每日一练(20230513) 输出最值、盛水容器、旋转数组II
46 0
|
4月前
|
Java 索引
Java每日一练(20230512) 最大间距、串联子串、最长回文子串
Java每日一练(20230512) 最大间距、串联子串、最长回文子串
40 0
|
4月前
|
Java 索引
Java每日一练(20230511) 有效数字、重复元素II、类和子类
Java每日一练(20230511) 有效数字、重复元素II、类和子类
53 0
|
4月前
|
Java 容器
Java每日一练(20230510) 生成器类、螺旋矩阵II、删除链表的重复元素II
Java每日一练(20230510) 生成器类、螺旋矩阵II、删除链表的重复元素II
48 0
|
4月前
|

Java每日一练(20230509) 下一个排列、分隔链表、随机指针链表
Java每日一练(20230509) 下一个排列、分隔链表、随机指针链表
35 0