当前位置:网站首页>LeetCode 每日一题 2022/3/7-2022/3/13
LeetCode 每日一题 2022/3/7-2022/3/13
2022-07-22 09:30:00 【alphaTao】
记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
目录
3/7 504. 七进制数
7进制 用7取余
def convertToBase7(num):
""" :type num: int :rtype: str """
if num==0:
return "0"
minus =False
if num<0:
minus = True
num = -num
ans = ""
while num>0:
ans = str(num%7)+ans
num //=7
if minus:
ans = "-"+ans
return ans
3/8 2055. 蜡烛之间的盘子
left,right记录往左往右最近的蜡烛位置
plate记录从左往右 当前位置有多少盘子
根据查询条件[l,r]找到l右边最近的蜡烛 r左边最近的蜡烛
如果存在这个区间 plate[r]-plate[l]就是区间内的盘子
def platesBetweenCandles(s, queries):
""" :type s: str :type queries: List[List[int]] :rtype: List[int] """
n = len(s)
left,right = [0]*n,[0]*n
plate = [0]*n
l,r=-1,-1
for loc,c in enumerate(s):
if c=="*":
if loc==0:
plate[loc]=1
else:
plate[loc]=plate[loc-1]+1
else:
l = loc
plate[loc]=plate[loc-1]
left[loc]=l
for i in range(n-1,-1,-1):
c = s[i]
if c=="|":
r = i
right[i] = r
ans = []
for l,r in queries:
num = 0
if s[l]!="|":
l = right[l]
if s[r]!="|":
r = left[r]
if l<r and l>=0 and r>=0:
num = plate[r]-plate[l]
ans.append(num)
return ans
3/9 798. 得分最高的最小轮调
nums有n个数
对于nums[loc]=v进行了k的轮调
0<=k<n
如果k<=loc:
轮调后的位置为loc-k 需要v<=loc-k
得到 0<=k<=min(loc,loc-v)
如果k>loc:
轮调后位置为n-k+loc 需要v<=n-k+loc
得到 loc<k<=n+loc-v
对于loc位置的数 有这两个区间的k可以满足条件
对这些区间内的k加1
区间内整体改动 使用差分数组
差分数组li长度为n+1 记录k的状态
如果区间[x,y]+1 只需要将li[x]+=1 li[y+1]-=1
最后从头累加到每个位置的数 即为这个位置的个数
def bestRotation(nums):
""" :type nums: List[int] :rtype: int """
n = len(nums)
li = [0]*(n+1)
for loc,v in enumerate(nums):
nl = min(loc,loc-v)
if nl>=0:
li[0]+=1
li[nl+1]-=1
nl = min(n-1,n+loc-v)
if nl<n and loc<n:
li[loc+1]+=1
li[nl+1]-=1
cur = 0
maxnum = 0
ans = 0
for k,v in enumerate(li):
cur+=v
if maxnum<cur:
maxnum = cur
ans = k
return ans
3/10 589. N 叉树的前序遍历
1.递归
2.迭代 存入栈中每次取栈顶元素 并将子节点倒序压入栈中
class Node(object):
def __init__(self, val=None, children=None):
self.val = val
self.children = children
def preorder(root):
""" :type root: Node :rtype: List[int] """
ans = []
def dfs(node):
if not node:
return
ans.append(node.val)
for c in node.children:
dfs(c)
dfs(root)
return ans
def preorder2(root):
""" :type root: Node :rtype: List[int] """
stack = [root]
ans = []
while stack:
node = stack.pop()
if node :
ans.append(node.val)
for c in node.children[::-1]:
stack.append(c)
return ans
3/11 2049. 统计最高分的节点数目
使用child[p]用来记录节点p的子节点
深搜 对于节点node 初始size = n-1 除去自身后个数
在搜索完叶子树后减去叶子树个数sz
此时size个数为node子树以外的节点个数
将子树sz与size相乘可以得到当前node得分
如果与最大值相同则将cnt+1
如果大于最大值 则更新最大值cnt=1
def countHighestScoreNodes(parents):
""" :type parents: List[int] :rtype: int """
n = len(parents)
from collections import defaultdict
child = defaultdict(list)
for node,p in enumerate(parents):
if p!=-1:
child[p].append(node)
maxnum,cnt = 0,0
def dfs(node):
global maxnum,cnt
num = 1
size = n-1
for c in child[node]:
sz = dfs(c)
num *= sz
size -=sz
if node!=0:
num *= size
if num==maxnum:
cnt+=1
elif num>maxnum:
maxnum = num
cnt = 1
return n-size
dfs(0)
return cnt
3/12 590. N 叉树的后序遍历
1.递归 先子后根
2.迭代 栈stack 如果节点包含子节点将子节点倒序压入栈中
如果节点无子节点或者子节点已经被处理 即该节点在mem中 则将结果加入
class Node(object):
def __init__(self, val=None, children=None):
self.val = val
self.children = children
def postorder(root):
""" :type root: Node :rtype: List[int] """
ans = []
def dfs(node):
if not node:
return
for c in node.children:
dfs(c)
ans.append(node.val)
dfs(root)
return ans
def postorder2(root):
""" :type root: Node :rtype: List[int] """
if not root:
return []
ans = []
stack = [root]
mem=set()
while stack:
node = stack[-1]
if len(node.children)==0 or node in mem:
ans.append(node.val)
stack.pop()
continue
stack.extend(reversed(node.children))
mem.add(node)
return ans
3/13 393. UTF-8 编码验证
check通过位运算来得到数值第一个0前包含有几个1
最多四个如果超过四个则说明该数值不符合条件返回-1
依次从头校验 need表示当前需要几个10开头的值
need==0时 不能出现check为1的数值
need>0时 只能接check为1的数值
def validUtf8(data):
""" :type data: List[int] :rtype: bool """
def check(v):
if v>>7==0:
return 0
off = 7
ans = 0
while off >0:
if ans >4:
return -1
if (v>>off)&1==1:
ans +=1
off-=1
else:
break
return ans
need = 0
for v in data:
num = check(v)
if num<0:
return False
if need==0 and num==1:
return False
if need>0 and num!=1:
return False
if need==0 and num==0:
continue
if need==0:
need= num-1
else:
need-=1
return need==0
边栏推荐
- Leetcode notes
- Leetcode:196. delete duplicate email
- 【YOLOv5实战4】基于YOLOv5的交通标志识别系统-模型测试与评估
- 程序员面试金典面试题 01.01. 判定字符是否唯一
- Mysql5.7 decompression configuration steps
- Huawei mobile phone locking application
- LeetCode 每日一题 2022/2/14-2022/2/20
- Leetcode: 184. the highest paid employee in the Department
- MNIST手写数字识别案例TensorFlow 2.0 实践
- LeetCode 每日一题 2022/1/10-2022/1/16
猜你喜欢
写作单词积累
OSI seven layer network model
Kindling the Darkness: A Practical Low-light Image Enhancer
paper - A Physics-based Noise Formation Model for Extreme Low-light Raw Denoising
Idea construit le projet jfinal + génération automatique de code + test de fonctionnement de la base de données (trois méthodes)
Rongyun x Xingzhi: exclusive Post-00 self social plot (including lottery)
Swagger-UI介绍及常用注解说明
逻辑回归中的损失函数
[QT source code reuse] simulate the pop-up mode of qcompleter
Cross entropy loss function
随机推荐
卧式单面多轴钻孔组合机床动力滑台液压系统的设计
米哈游大量招募新同学,校招提前批最后一天!
Transformer再下一城!low-level多个任务榜首被占领,北大华为等联合提出预训练模型IPT
Wechat scans the QR code of the website, but only displays the link address, and cannot jump to the web page
Behind the explosion of collaborative office market: cloud communication capability is the focus of demand
1. Access JSON in a way similar to the window path
PTA basic question 7-23 currency conversion (20 points) (true)
When pytorch customizes the dataloder, it returns parameters
Flink learning notes (VI) Flink's time and window
1. Closeable of qtablewidget, 2.pro/build_ pass、member,3.QString&&
融云办政事: “小网格”也能实现“大治理”
WGet downloads all files in the directory
[yolov5 practice 4] traffic sign recognition system based on yolov5 - model test and evaluation
1. Package and release procedure of QT (NSIS);
Date operation in shell script
二、IDEA搭建JFinal項目+代碼自動生成+數據庫操作測試(三種方式)
【链表技巧汇总】141.环形链表(简单)
Numpy.reshape finish image cutting
PTA exercise 8-8 judging palindrome string
Flink learning notes (III) Flink installation and deployment