当前位置:网站首页>lc marathon 7.23
lc marathon 7.23
2022-07-23 16:05:00 【Yunxiachuan】
List of articles
- [81. Search rotation sort array II](https://leetcode.cn/problems/search-in-rotated-sorted-array-ii/)
- [452. Detonate the balloon with a minimum number of arrows ](https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/)
- [79. Word search Answer key - Power button (LeetCode)](https://leetcode.cn/problems/word-search/solution/)
- [101. Symmetric binary tree ](https://leetcode.cn/problems/symmetric-tree/)
81. Search rotation sort array II
This question is really great !!!
First of all, you can find it by dichotomy , There will always be one side Orderly
We first judge which side is orderly (nums[mid]>nums[left])
Then according to the orderly side Judge whether there is this number No, Then go to the other side
But because there is repetition , Therefore, we must strictly judge whether it is orderly (< >)
When nums[mid]==nums[left] or nums[mid] == nums[right]
Adopt a conservative strategy ,left+=1 right-=1
class Solution:
def search(self, nums: List[int], target: int) -> bool:
# The key is Find out which side is orderly
# Then use the orderly side to judge
left=0
right=len(nums)-1
while(left<=right):
mid=(right-left)//2 +left
if left==right and nums[mid]!=target: # Anti deadlock
return False
if nums[mid]==target:
return True
if nums[mid]>nums[left]: # Indicates that the left side is orderly , One side must be orderly
if target>=nums[left] and target<=nums[mid]: # On the left
right=mid-1
else:
left=mid+1
elif nums[mid]<nums[right]:
if target >= nums[mid] and target <= nums[right]: # On the right
left=mid+1
else:
right=mid-1
else:
if nums[left]==nums[mid]:
left+=1
else:
right-=1
return False
452. Detonate the balloon with a minimum number of arrows
Same as above
First sort the first digit
Then according to the following situation , Update the following 
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
def ls(x1,x2):
if x1<x2:
return -1
else:
return 1
points=sorted(points,key=cmp_to_key(ls))
ans=0
for index in range(1,len(points)):
if points[index][0]>points[index-1][1]:
ans+=1
else:
points[index][1]=min(points[index][1],points[index-1][1])
return ans+1
79. Word search Answer key - Power button (LeetCode)
This problem is really worth accumulating
There are two details
The first is recursive Enter the next round no need Judge
It is expression “ Try to enter the next round ” The meaning of
After entering , Then judge
Boundary judgment
Access judgment
Success judgment
Failure judgment
Access array for global access
After judging success , Then mark this position 1, Try again
After trying , Restore the position
Be careful not to copy , Just quote
from copy import deepcopy
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
visiteds=[[0 for i in range(len(board[0]))] for j in range(len(board))]
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j]==word[0]:
rs=dfs(i,j,word,board,visiteds)
if rs:
return True
return False
def dfs(i,j,word,boards,visiteds):
# Illegally cross the border
if not (i>=0 and i<len(boards) and j>=0 and j<len(boards[0])):
return False
# This one has visited
if visiteds[i][j]==1:
return False
# Successfully matched all strings
if len(word)==1:
if boards[i][j]==word[-1]:
return True
else:
return False
# Can't match
if boards[i][j]!=word[0]:
return False
# Description match , Setting this location has been accessed
visiteds[i][j]=1
rs1=dfs(i-1,j,word[1:],boards,visiteds)# Up
rs2=dfs(i+1,j,word[1:],boards,visiteds)# Down
rs3=dfs(i, j-1, word[1:],boards,visiteds)# towards the left
rs4=dfs(i,j+1,word[1:],boards,visiteds)# towards the right
visiteds[i][j]=0
return rs1 or rs2 or rs3 or rs4
101. Symmetric binary tree
I originally wanted to traverse with hierarchy , But recursion is more elegant ,
It actually detects The left node of a node and The right node of a node Whether it is equal or not ( Mirror image )
# 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 isSymmetric(self, root: Optional[TreeNode]) -> bool:
if root==None:
return True
return ismirror(root.left,root.right)
def ismirror(node1,node2):
if node1==None and node2==None:
return True
if node1!=None and node2!=None and node1.val == node2.val:
return ismirror(node1.left,node2.right) and ismirror(node1.right,node2.left)
return False
边栏推荐
- Pydensecrf installation
- harbor镜像仓库
- It's too hard! Tencent T4 boss was still staying up late at 4 a.m. and was actually sorting out the distributed transaction notes
- Safe and reasonable use of electricity to harvest a cool "summer"
- 适用于顺序磁盘访问的1分钟法则
- Software testing weekly (No. 81): what can resist negativity is not positivity, but concentration; What can resist anxiety is not comfort, but concrete.
- ECS remote monitoring
- “1+1>10”:无代码/低代码与RPA技术的潜在结合
- 云服务器ECS远程监控
- 《代码之丑》学习总结
猜你喜欢

Redis installation

2022最NB的JVM基础到调优笔记,吃透阿里P6小case

From the big guy baptism! 2022 headline first hand play MySQL advanced notes, and it is expected to penetrate P7

ORA-01654错误:表空间满了,插入失败

day14函数模块

xml-xxe漏洞之Fake XML cookbook

js过滤/替换敏感字符

超详细MP4格式分析

适用于顺序磁盘访问的1分钟法则
![[7.16] code source - [array division] [disassembly] [select 2] [maximum common divisor]](/img/fd/ffddb3ac35e946215a0582f09f278a.png)
[7.16] code source - [array division] [disassembly] [select 2] [maximum common divisor]
随机推荐
Guangzhou held a competition for quality and safety supervisors of agricultural products in the town and street
Comparison of functional characteristics and parameters of several solar panel battery charging management ICs cs5363, cs5350 and cs5328
(BFS) template + example (maze, eight digits)
W3C 推出去中心化标识符作为 Web 标准
10100
C # close current computer command
冒泡排序-看着一篇就够啦
Umijs - data transmission between main and sub applications of Qiankun
Redis installation
复现各种对抗攻击方法
On 'premature optimization'
It's too hard! Tencent T4 boss was still staying up late at 4 a.m. and was actually sorting out the distributed transaction notes
Learning summary of ugly code
js过滤/替换敏感字符
Axure advanced
基于Matlab的SSB信号调制和解调(内附源码)
黑马程序员-接口测试-四天学习接口测试-第三天-postman高级用法,newman例集导出导入,常用断言,断言json数据,工作原理,全局,环境变量,时间戳,请求前置脚本,关联,批量执行测试用例
Vim到底可以配置得多漂亮?
云原生(十一) | Kubernetes篇之Kubernetes原理与安装
SharedPreferences数据储存