当前位置: 首页 > news >正文

力扣题86~90

题86(中等):

python代码

# 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: Optional[ListNode], x: int) -> Optional[ListNode]:#分组,分两丢small_node=ListNode()high_node=ListNode()p_small=small_nodep_high=high_nodep_h=headwhile p_h!=None:#如果小于,应该放到smallerprint(p_h.val)if p_h.val<x:p_small.next=p_hp_small=p_small.nextp_h=p_h.nextelse:p_high.next=p_hp_high=p_high.nextp_h=p_h.nextp_small.next=high_node.nextp_high.next=Nonehead=small_node.nextreturn head

题87(困难):

python代码:

class Solution:def isScramble(self, s1: str, s2: str) -> bool:if len(s1) != len(s2) or sorted(s1) != sorted(s2):return Falsen = len(s1)dp = [[[False] * (n + 1) for _ in range(n)] for _ in range(n)]# 初始化长度为1的子串for i in range(n):for j in range(n):dp[i][j][1] = s1[i] == s2[j]# 遍历所有可能的子串长度for k in range(2, n + 1):for i in range(n - k + 1):for j in range(n - k + 1):for p in range(1, k):# 不交换的情况if dp[i][j][p] and dp[i + p][j + p][k - p]:dp[i][j][k] = Truebreak# 交换的情况if dp[i][j + k - p][p] and dp[i + p][j][k - p]:dp[i][j][k] = Truebreakreturn dp[0][0][n]

题88(简单):

python代码:

class Solution:def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:"""Do not return anything, modify nums1 in-place instead."""for i in range(m,m+n):nums1[i]=nums2[i-m]nums1.sort()

题89(中等):

分析:

找规律00 01 11 10-----> 000 001 011 010(前4+0) / 110 111 101 100 (后4加1并倒序排列)

class Solution:def grayCode(self, n: int) -> List[int]:def call_back(n,bin_list):if n == 1:bin_list.extend(['0', '1'])return bin_listbin_list=call_back(n - 1,bin_list)new = ['1' + i for i in bin_list][::-1]bin_list=['0' + i for i in bin_list]print(new)bin_list.extend(new)return bin_listbin_list=call_back(n,[])return [int(i, 2) for i in bin_list]

题90(中等):

python代码:

class Solution:def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:n_list=sorted(nums)res=[[]]def call_back(nums,k,now_list):#判断出递归条件,当if nums==[]:returnfor i in range(0,len(nums)):if i>0 and nums[i]==nums[i-1]:continue#加一个i处的值new_now=now_list.copy()new_now.append(nums[i])#把这个去了new_num=nums[i+1:]res.append(new_now)call_back(new_num,k+1,new_now)call_back(n_list,0,[])return res

http://www.lryc.cn/news/471949.html

相关文章:

  • 【JavaEE】【多线程】定时器
  • CI/CD 的原理
  • 进一步认识ICMP协议
  • NUUO网络视频录像机upload.php任意文件上传漏洞复现
  • WebGL 3D基础
  • Docker 部署MongoDb
  • 【Hadoop】hadoop的路径分不清?HDFS路径与本地文件系统路径的区别
  • 倪师学习笔记-天纪-易经八卦
  • 自动驾驶性能分析时,非常有用的两个信息
  • 数据结构 - 并查集
  • canvas基础+应用+实例
  • Linux命令 用户操作简介
  • 大语言模型的Scaling Law【Power Low】
  • windows环境下,使用docker搭建redis集群
  • Python(pandas库3)
  • WPF+MVVM案例实战(十)- 水波纹按钮实现与控件封装
  • 数据结构————map,set详解
  • fdisk - Linux下的磁盘分区利器
  • or-tools优化库记录
  • M1 Pro MacBook Pro 上的奇遇:Rust 构建失败,SIGKILL 惊魂记
  • 重构商业生态:DApp创新玩法与盈利模式的深度剖析
  • 2024首届亚洲国际电影节圆满落下帷幕
  • 【Mybatis】动态SQL+配置文件+数据库连接池+企业规范(10)
  • layui扩展组件之----右键菜单
  • ue5实现数字滚动增长
  • Flink(一)
  • kaggle 数据集下载
  • Linux shell编程学习笔记87:blkid命令——获取块设备信息
  • wireshark筛选条件整理
  • 基于现代 C++17 的模块化视频质量诊断处理流程设计