力扣题86~90

打印 上一主题 下一主题

主题 1843|帖子 1843|积分 5529

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题86(中等):


python代码

  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. #     def __init__(self, val=0, next=None):
  4. #         self.val = val
  5. #         self.next = next
  6. class Solution:
  7.     def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
  8.         #分组,分两丢
  9.         small_node=ListNode()
  10.         high_node=ListNode()
  11.         p_small=small_node
  12.         p_high=high_node
  13.         p_h=head
  14.         while p_h!=None:
  15.             #如果小于,应该放到smaller
  16.             print(p_h.val)
  17.             if p_h.val<x:
  18.                 p_small.next=p_h
  19.                 p_small=p_small.next
  20.                 p_h=p_h.next
  21.             else:
  22.                 p_high.next=p_h
  23.                 p_high=p_high.next
  24.                 p_h=p_h.next
  25.         p_small.next=high_node.next
  26.         p_high.next=None
  27.         head=small_node.next
  28.         return head
  29.         
复制代码

题87(困难):


python代码:

  1. class Solution:
  2.     def isScramble(self, s1: str, s2: str) -> bool:
  3.         if len(s1) != len(s2) or sorted(s1) != sorted(s2):
  4.             return False
  5.         n = len(s1)
  6.         dp = [[[False] * (n + 1) for _ in range(n)] for _ in range(n)]
  7.         
  8.         # 初始化长度为1的子串
  9.         for i in range(n):
  10.             for j in range(n):
  11.                 dp[i][j][1] = s1[i] == s2[j]
  12.         
  13.         # 遍历所有可能的子串长度
  14.         for k in range(2, n + 1):
  15.             for i in range(n - k + 1):
  16.                 for j in range(n - k + 1):
  17.                     for p in range(1, k):
  18.                         # 不交换的情况
  19.                         if dp[i][j][p] and dp[i + p][j + p][k - p]:
  20.                             dp[i][j][k] = True
  21.                             break
  22.                         # 交换的情况
  23.                         if dp[i][j + k - p][p] and dp[i + p][j][k - p]:
  24.                             dp[i][j][k] = True
  25.                             break
  26.         return dp[0][0][n]
复制代码
题88(简单):


python代码:

  1. class Solution:
  2.     def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
  3.         """
  4.         Do not return anything, modify nums1 in-place instead.
  5.         """
  6.         for i in range(m,m+n):
  7.             nums1[i]=nums2[i-m]
  8.         nums1.sort()
  9.         
复制代码
题89(中等):


分析:

找规律00 01 11 10-----> 000 001 011 010(前4+0) / 110 111 101 100 (后4加1并倒序排列)
  1. class Solution:
  2.     def grayCode(self, n: int) -> List[int]:
  3.         def call_back(n,bin_list):
  4.             if n == 1:
  5.                 bin_list.extend(['0', '1'])
  6.                 return bin_list
  7.             bin_list=call_back(n - 1,bin_list)
  8.             new = ['1' + i for i in bin_list][::-1]
  9.             bin_list=['0' + i for i in bin_list]
  10.             print(new)
  11.             bin_list.extend(new)
  12.             return bin_list
  13.         bin_list=call_back(n,[])
  14.         return [int(i, 2) for i in bin_list]
复制代码
题90(中等):


python代码:

  1. class Solution:
  2.     def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
  3.         n_list=sorted(nums)
  4.         res=[[]]
  5.         def call_back(nums,k,now_list):
  6.             #判断出递归条件,当
  7.             if nums==[]:
  8.                 return
  9.             for i in range(0,len(nums)):
  10.                 if i>0 and nums[i]==nums[i-1]:
  11.                     continue
  12.                 #加一个i处的值
  13.                 new_now=now_list.copy()
  14.                 new_now.append(nums[i])
  15.                 #把这个去了
  16.                 new_num=nums[i+1:]
  17.                 res.append(new_now)
  18.                 call_back(new_num,k+1,new_now)
  19.         call_back(n_list,0,[])
  20.         return res
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

欢乐狗

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表