public class LinkedList{
public static void main(String[] args) {
LinkList list = new LinkList();
list.insert(“xiao”, “head”);
list.insert(“lu”, “xiao”);
list.insert(“ni”, “head”);
list.insert(“hellow”, “head”);
list.print();
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
class Solution {
ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA null || headB null) {
return null;
}
ListNode a = headA;
ListNode b = headB;
int na = 1;
int nb = 1;
while (a != b) {
if (a.next != null) {
a = a.next;
na++;
}
if (b.next != null) {
b = b.next;
nb++;
}
}
int last = Math.abs(na - nb);
if (na > nb) {
for (int i = 0; i < last; i++) {
headA = headA.next;
}
} else {
for (int i = 0; i < last; i++)
headB = headB.next;
}
while (headA != headB) {
if (headA == null || headB == null)
return null;
headA = headA.next;
headB = headB.next;
}
return headA;
}
帅地哥 这个python的答案会超出时间限制 过不了 下面这个答案也是递归 原理一样
class Solution:
def lowestCommonAncestor(self, root: ‘TreeNode’, p: ‘TreeNode’, q: ‘TreeNode’) -> ‘TreeNode’:
# 基准情况
if root is None or root p or root q:
return root
# 递归查找左子树和右子树
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
# 如果左右子树都有结果,说明当前节点是最近公共祖先
if left and right:
return root
# 否则返回非空的那个结果
return left if left else right
# def find(self, root, p):
# if root is None:
# return False
# if root p:
# return True
# return self.find(root.left, p) or self.find(root.right, p)
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
int n = 0;
for (ListNode cur = head; cur != null; cur = cur.next)
++n; // 统计节点个数
ListNode dummy = new ListNode(0, head), p0 = dummy;
ListNode pre = null, cur = head;
for (; n >= k; n -= k) {
for (int i = 0; i < k; ++i) { // 同 92 题
ListNode nxt = cur.next;
cur.next = pre; // 每次循环只修改一个 next,方便大家理解
pre = cur;
cur = nxt;
}
// 见视频
ListNode nxt = p0.next;
p0.next.next = cur;
p0.next = pre;
p0 = nxt;
}
return dummy.next;
}
class Solution:
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
p0 = dummy = ListNode(next=head)
for _ in range(left - 1):
p0 = p0.next
pre = None
cur = p0.next
for _ in range(right - left + 1):
nxt = cur.next
cur.next = pre # 每次循环只修改一个 next,方便大家理解
pre = cur
cur = nxt
# 见视频
p0.next.next = cur
p0.next = pre
return dummy.next
先化为前right项,再让left逐渐为1,采用递归的思想
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
last = None
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
if left == 1:
return self.reversethN(head,right)
head.next = self.reverseBetween(head.next,left-1,right-1)
return head
def reversethN(self,head,n):
if n == 1:
self.last = head.next
return head
newhead = self.reversethN(head.next,n-1)
head.next.next = head
head.next = self.last
return newhead
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# 递归1
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
def recur(cur, per):
if not cur: return per
res = recur(cur.next, cur)
cur.next = per
return res
return recur(head, None)
# 递归2
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return head
newhead = self.reverseList(head.next)
head.next.next = head
head.next = None
return newhead
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
A, B = headA, headB
while A != B:
A = A.next if A else headB
B = B.next if B else headA
return A
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# former比latter多走了cnt步
class Solution:
def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:
latter = former = head
for _ in range(cnt):
if not former: return
former = former.next
while former:
latter = latter.next
former = former.next
return latter
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# 双指针
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
class Solution:#滑动窗口
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
l, r, s = 0, 0, 0
n = len(nums)
ans = float('inf')
while r < n:
s += nums[r]
while s >= target:
ans = min(ans, r - l + 1)
s -= nums[l]
l += 1
r += 1
return ans if ans <= n else 0
”’
class Solution:
def maxArea(self, height: List[int]) -> int:
l = 0
r = len(height) – 1
s =0
while l <= r:
tmp = min(height[l], height[r]) #记录容器的高
s = max(s, tmp*(r-l))
if height[l] < height[r]:
l += 1
else:
r -= 1
return s
”’
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
l = 0
r = len(nums) – 1
if not nums:
return 0
while l <= r:
if nums[l] val:
nums[l], nums[r] = nums[r], nums[l]
r -= 1
else:
l += 1
return l
+单指针
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
l = 0
for i in range(len(nums)):
if nums[i] val:
nums[i] = ‘_’
else:
tmp = nums[l]
nums[l] = nums[i]
nums[i] = tmp
l += 1
return l
*双指针法
”’
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
l = 0
r = len(nums) – 1
if not nums:
return 0
while l <= r:
if nums[l] val:
nums[l], nums[r] = nums[r], nums[l]
r -= 1
else:
l += 1
return l
”’
*单指针
”’
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
l = 0
for i in range(len(nums)):
if nums[i] val:
nums[i] = ‘_’
else:
tmp = nums[l]
nums[l] = nums[i]
nums[i] = tmp
l += 1
return l
”’
使得保证hh永远大于x
class Solution:
def mySqrt(self, x: int) -> int:
l = 0
h = x
while l <= h:
if h – l <= 1:
return l
mid = l + (h – l)//2
if midmid > x:
h = mid
elif mid*mid < x:
l = mid
else:
return mid
return -1
2.建表语句
CREATE TABLE disallow_word ( id int NOT NULL AUTO_INCREMENT, value varchar(255) NOT NULL, type int NOT NULL DEFAULT ‘0’,
PRIMARY KEY (id)
) ENGINE=InnoDB
CREATE TABLE user ( id int NOT NULL AUTO_INCREMENT, username varchar(20) NOT NULL, password int NOT NULL, salt varchar(5) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB
提交地址:https://github.com/CurryisCoat/community/tree/dev
打卡
class Solution {> res=new ArrayList<>(); temp=new ArrayList<>(); > combinationSum3(int k, int n) {
List<List
List
public List<List
backtrace(1,k,n);
return res;
}
void backtrace(int l,int k,int n){//l表示遍历到的数,n表示距离相差之和还有多远
if(temp.size()k){
if(n0){
res.add(new ArrayList<>(temp));
}
return;
}
for(int i=l;i<=9;i++){
temp.add(i);
backtrace(i+1,k,n-i);
temp.remove(temp.size()-1);
}
return;
}
}
困难题现在都卷成中等题了。。。
打卡
堆这节讲的很清晰,赞
class Node{
String data;
Node next;
public Node(String data){
this.data = data;
this.next = null;
}
}
class LinkList {
private Node head;
}
public class LinkedList{
public static void main(String[] args) {
LinkList list = new LinkList();
list.insert(“xiao”, “head”);
list.insert(“lu”, “xiao”);
list.insert(“ni”, “head”);
list.insert(“hellow”, “head”);
list.print();
} Java版本
打卡
打卡
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
class Solution {
ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA null || headB null) {
return null;
}
ListNode a = headA;
ListNode b = headB;
int na = 1;
int nb = 1;
}
时间复杂度高了
帅地写的真的好好啊,很客观!而且量化到了每一年,这样就可以做出来规划了。赞!
class Solution {
public int findRepeatDocument(int[] documents) {
if(documents null || documents.length 0) return -1;
}
帅地哥 这个python的答案会超出时间限制 过不了 下面这个答案也是递归 原理一样
class Solution:
def lowestCommonAncestor(self, root: ‘TreeNode’, p: ‘TreeNode’, q: ‘TreeNode’) -> ‘TreeNode’:
# 基准情况
if root is None or root p or root q:
return root
# def find(self, root, p):
# if root is None:
# return False
# if root p:
# return True
# return self.find(root.left, p) or self.find(root.right, p)
我采用session监听器来统计网站在线人数,每次创建session时就会自动加一,销毁session时就会自动减一;
先初始化一个类实现HttpSessionlistener,然后定义count,创建session,count+1,销毁session,count-1;然后在类上加注解webListener,启动类上再加@ServletComponentScan(“com.example.demo.listener”)扫描包才有用。
优点是:
统计在线人数结果相对准确;
缺点是:
当用户关闭浏览器时并不会触发session监听,当下一次登录时仍然会让count加一
或者在session过期时,session监听并不能做一个实时的响应去将在线数减一
当用户再次登陆,由于cookie中含有的session_id不同而导致session监听器记录下session创建,而使count加一。
对服务器性能影响较大,用户每次访问网站时,服务端都会创建一个session,并将该session与用户关联起来,这样会增加服务器的负担,特别是在高并发的时候,导致服务器压力过大
https://github.com/CurryisCoat/community/tree/dev
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
int n = 0;
for (ListNode cur = head; cur != null; cur = cur.next)
++n; // 统计节点个数
}
怎么突然要会员了,跟了一半还没做完QAQ
已完成阅读
我的实现逻辑是用redis存储随机数,key为用户id,只保留最后一个登录的用户生成的token,在用户登录时进行判断,如果redis里面存了该用户的token,我们就将其删除,然后设置一个新的token值,将其挤下线;
https://github.com/CurryisCoat/community/tree/dev
为什么网页端要会员而手机端不需要
使用session保持用户登录态
https://github.com/CurryisCoat/community/tree/dev
public String login(HttpServletRequest request,HttpServletResponse response) throws IOException {
String username = request.getParameter(“username”);
User user = userService.login(username);
response.setContentType(“text/html;charset=utf-8”);
PrintWriter writer = response.getWriter();
已阅读完成
加密方式:MD5,随机加盐
public class Md5Memory {
}
已完成阅读
讲解里的公式v[i](体积)和w[i](价值)写反了,如果拿的时候公式应该是:dp[i][v] = dp[i-1][v-v[i]] + w[i]
打卡
先化为前right项,再让left逐渐为1,采用递归的思想
打卡
打卡
打卡
拿掉栈顶元素,反转:1,2,3,4,5 => 4,3,2,1
拿掉栈顶元素,top放到栈底,将tmp放到栈顶:4,3,2,1 => 5,4,3,2 => 5,4,3,2,1
”’
class Solution:
def maxArea(self, height: List[int]) -> int:
l = 0
r = len(height) – 1
s =0
while l <= r:
tmp = min(height[l], height[r]) #记录容器的高
s = max(s, tmp*(r-l))
if height[l] < height[r]:
l += 1
else:
r -= 1
return s
”’
+双指针法
+单指针
*双指针法
”’
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
l = 0
r = len(nums) – 1
if not nums:
return 0
while l <= r:
if nums[l] val:
nums[l], nums[r] = nums[r], nums[l]
r -= 1
else:
l += 1
return l
”’
*单指针
”’
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
l = 0
for i in range(len(nums)):
if nums[i] val:
nums[i] = ‘_’
else:
tmp = nums[l]
nums[l] = nums[i]
nums[i] = tmp
l += 1
return l
”’
class Solution:
def mySqrt(self, x: int) -> int:
l = 0
h = x
while l <= h:
if h – l <= 1:
return l
mid = l + (h – l)//2
if midmid > x:
h = mid
elif mid*mid < x:
l = mid
else:
return mid
return -1
github仓库:https://github.com/CurryisCoat/community/tree/dev
2.建表语句
CREATE TABLE
disallow_word
(id
int NOT NULL AUTO_INCREMENT,value
varchar(255) NOT NULL,type
int NOT NULL DEFAULT ‘0’,PRIMARY KEY (
id
)) ENGINE=InnoDB
github仓库:https://github.com/CurryisCoat/community/tree/dev
1.在数据库设计表时给用户名字段添加唯一索引;
2:前缀树
CREATE TABLE
user
(id
int NOT NULL AUTO_INCREMENT,username
varchar(20) NOT NULL,password
int NOT NULL,salt
varchar(5) NOT NULL,PRIMARY KEY (
id
)) ENGINE=InnoDB
提交地址:https://github.com/CurryisCoat/community/tree/dev
对于网站首次打开网页时的性能优化,通常建议将一些关键逻辑放在前端进行判断,以加快页面加载速度。这样可以让用户更快地看到页面内容,并提升用户体验。同时,在服务端进行一些必要的校验和安全性检查也是很重要的,以确保用户输入的数据安全可靠。综合来看,前端和服务端各有其优势,合理分配逻辑判断和处理任务可以提高网站性能和用户体验。
1.key = “first” value= “encodedValue”
2.C:\Users\zouliang\AppData\Local\Microsoft\Edge\User Data\Default\Network
3.https://github.com/CurryisCoat/community/tree/dev
使用SpringBoot创建
GitHUb地址:https://github.com/CurryisCoat/community/tree/dev
反转链表 + 字符串相加
打卡:https://github.com/CurryisCoat/community
求倒数第cnt个结点,用双指针,先让前一个指针跑cnt步,随后再一起跑,前一个指针跑到终点时,后一个指针就正好到了倒数第cnt个结点
时间复杂度:O(n)
空间复杂度: O(1)
class Solution {
public ListNode trainingPlan(ListNode head, int cnt) {
ListNode p=head,q=head;
while(q!=null){
while(cnt>0){//先跑cnt步
q=q.next;
cnt–;
}
while(cnt0&&q!=null){
p=p.next;
q=q.next;
}
}
return p;
}
}
打卡,简单复习