力扣题86~90
题86(中等):https://i-blog.csdnimg.cn/direct/11e0f1771e134d208d8be5b2c55649a5.png
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, x: int) -> Optional:
#分组,分两丢
small_node=ListNode()
high_node=ListNode()
p_small=small_node
p_high=high_node
p_h=head
while p_h!=None:
#如果小于,应该放到smaller
print(p_h.val)
if p_h.val<x:
p_small.next=p_h
p_small=p_small.next
p_h=p_h.next
else:
p_high.next=p_h
p_high=p_high.next
p_h=p_h.next
p_small.next=high_node.next
p_high.next=None
head=small_node.next
return head
题87(困难):
https://i-blog.csdnimg.cn/direct/ec64a5fd60cf4168b11400d4348dad14.png
python代码:
class Solution:
def isScramble(self, s1: str, s2: str) -> bool:
if len(s1) != len(s2) or sorted(s1) != sorted(s2):
return False
n = len(s1)
dp = [[ * (n + 1) for _ in range(n)] for _ in range(n)]
# 初始化长度为1的子串
for i in range(n):
for j in range(n):
dp = s1 == s2
# 遍历所有可能的子串长度
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 and dp:
dp = True
break
# 交换的情况
if dp and dp:
dp = True
break
return dp 题88(简单):
https://i-blog.csdnimg.cn/direct/8a2d2ccf7c1a42719f9c7de177158d8e.png
python代码:
class Solution:
def merge(self, nums1: List, m: int, nums2: List, n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
for i in range(m,m+n):
nums1=nums2
nums1.sort()
题89(中等):
https://i-blog.csdnimg.cn/direct/6a7b7c5a8de9495ca648593316d4a915.png
分析:
找规律00 01 11 10-----> 000 001 011 010(前4+0) / 110 111 101 100 (后4加1并倒序排列)
class Solution:
def grayCode(self, n: int) -> List:
def call_back(n,bin_list):
if n == 1:
bin_list.extend(['0', '1'])
return bin_list
bin_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_list
bin_list=call_back(n,[])
return 题90(中等):
https://i-blog.csdnimg.cn/direct/3a770b5183ba4aeca96b0f6f09ba4d3f.png
python代码:
class Solution:
def subsetsWithDup(self, nums: List) -> List]:
n_list=sorted(nums)
res=[[]]
def call_back(nums,k,now_list):
#判断出递归条件,当
if nums==[]:
return
for i in range(0,len(nums)):
if i>0 and nums==nums:
continue
#加一个i处的值
new_now=now_list.copy()
new_now.append(nums)
#把这个去了
new_num=nums
res.append(new_now)
call_back(new_num,k+1,new_now)
call_back(n_list,0,[])
return res
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]