当前位置:网站首页>lc marathon 7.21
lc marathon 7.21
2022-07-22 11:27:00 【Yunxiachuan】
List of articles
814. Two fork tree pruning
Use... Cleverly Binary tree reconstruction + Suffix traversal
Judge a little bit from the bottom up , Delete a little bit
too strong It's so beautiful
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root==None:
return None
root.left=self.pruneTree(root.left)
root.right=self.pruneTree(root.right)
if root.left==None and root.right==None and root.val==0:
return None
return root
86. Separate the list
It's still a scalpel with words , Accurate coding
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
dump1 = ListNode(-1) # Connection ratio x Small value
dump2 = ListNode(-2) # Connection ratio x Big value
q1 = dump1 # finger dump1 End of linked list
q2 = dump2 # finger dump2 End of linked list
p = head # Used to traverse the original linked list
# about x, Add to by default 2 chain
while p != None:
if p.val < x:
q1.next = p # Connect this node
q1 = q1.next # q1 To the end of the list
elif p.val > x:
q2.next = p # Connect this node
q2 = q2.next # q2 To the end of the list
else: # When equal
q2.next = p
q2 = q2.next # q2 To the end of the list
p = p.next # p turn back and proceed
# Last The chain must be broken
q1.next = None
q2.next = None
q1.next = dump2.next
return dump1.next
//C++ The simplest dichotomy classification discussion
// Two minutes at a time , At least one side of the left half and the right half is orderly , This condition can be divided into two situations :
//1、 The left half is orderly
//(1) target Fall on the left half
//(2) otherwise
//2、 The right half is orderly
//(1) target Fall on the right half
//(2) otherwise
// in summary , There are two possibilities , These two situations have two possibilities respectively , The code is as follows :
bool search(vector<int>& nums, int target) {
int l = 0, r = nums.size()-1;
while(l<=r){
// Dealing with duplicate numbers
while(l<r&&nums[l]==nums[l+1]) ++l;
while(l<r&&nums[r]==nums[r-1]) --r;
int mid = l+(r-l)/2;
if(nums[mid]==target) return true;
// The left half is ordered
if(nums[mid]>=nums[l]){
if(target<nums[mid]&&target>=nums[l]) r = mid-1;//target Fall on the left half
else l = mid+1;
}
else{// The right half is orderly
if(target>nums[mid]&&target<=nums[r]) l = mid+1;//target Fall on the right half
else r = mid-1;
}
}
return false;
}
435. No overlap
Record the thoughts of the boss : Greedy Algorithm , Sort by starting point : Choose the shortest ending , Only later can more intervals be connected ( If two intervals overlap , You should keep the ending small ) Turn the question into how many intervals can be reserved at most , Make them not repeat each other , Then sort by the end , The end of each interval is important , The smaller the ending , The more likely it is to accommodate more intervals .
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
def so(x1,x2):
if x1[0]<=x2[0]:
return -1
else:
return 1
intervals=sorted(intervals,key=cmp_to_key(so))
ans=0
nums=intervals
for index in range(1,len(intervals)):
if nums[index][0] >=nums[index-1][1]:
continue
if nums[index][0]<nums[index-1][1]:
if nums[index][1]<=nums[index-1][1]:
ans+=1
continue
else:
nums[index][0]=nums[index-1][0]
nums[index][1]=nums[index-1][1]
ans+=1
continue
return ans
边栏推荐
- 网络安全竞赛C模块批量拿值脚本
- Software Testing Engineer | can you enter a big factory without a degree?
- 背会这些面试题,任何技术面能成功一半
- _TensorBase(45个张量基础后置函数总结) && Pytorch官方文件 && NOTEBOOK(NINE)
- Is it safe to open an account on flush? How to buy REITs fund
- TX2显存与内存之间数据传递原理
- 模型微调(fine-tuning)
- Download datasets using kaggle's API
- AcWing 1184. Euler circuit problem solving (Euler circuit)
- AcWing 1124. Solution to the problem of repairing fences on horseback (Euler circuit)
猜你喜欢
向量化引擎对HTAP的价值与技术思考
Restful URL design specification
调用“抱抱脸团队打造的Transformers pipeline API” && 通过预训练模型,快速训练和微调自己的模型
LeetCode刷题--点滴记录020
你为什么会做测试/开发程序员?各路伙伴描述......
SYSTEMd management blackbox exporter
响应式织梦模板智能家居类网站
《Service Worker 指南-1》
嵌入式分享合集18
AcWing 1124. Solution to the problem of repairing fences on horseback (Euler circuit)
随机推荐
AcWing 1185. Word game solution (Euler circuit)
同花顺的账户安全吗 中信证券开户佣金是多少
猜数字+随机数
Custom type: structure (II) bit segment implementation
Is it safe to open an account on tonghuashun online? How to buy national debt reverse repurchase
Is it safe to open an account on flush? How to buy REITs fund
即看即用 && 序列化和并行化 ( Serialization and Parallelism) && Pytorch官方文档总结 && 笔记 (四)
同花顺开通华泰证券账户安全吗?
标签平滑(LabelSmoothing)介绍与代码实现
AcWing 1184. 欧拉回路 题解(欧拉回路)
Introduction to nodes
二分查找/折半查找
(HR面试)最常见的面试问题和技巧性答复
Opencv && 把视频裁剪成指定帧率的图像集
(PC+WAP)织梦模板情感资讯类网站
Pytorch训练模型固定随机种子(seed),保证精度可复现
Software Testing Engineer | can you enter a big factory without a degree?
Android 面试题:说一下 PendingIntent 和 Intent 的区别
Is the account of flush safe? How much is the Commission for opening an account at CITIC Securities
调用“抱抱脸团队打造的Transformers pipeline API” && 通过预训练模型,快速训练和微调自己的模型