万字长文记录我的leetcode辛酸史

15 篇文章 2 订阅

#旋转180度值得就是，行的啊a[i][j]=a[n-i][n-j]
#旋转90度为：a[i][j]=> a[j][n-i]
#旋转270度： a[i][j]=> a[n-i]a[n-j]=>a[n-j][i]

(有点又丑又长)

class Solution(object):
def findRotation(self, mat, target):
"""
:type mat: List[List[int]]
:type target: List[List[int]]
:rtype: bool
"""
#旋转180度值得就是，行的i=n-i,j=n-j
#旋转90度为：a[i][j]=> a[j][n-i]
#旋转270度： a[i][j]=> a[n-i]a[n-j]=>a[n-j][i]
n=len(mat)-1
Flag1=True
Flag2=False
for i in range(n+1):
for j in range(n+1):
if mat[i][j]!=target[j][n-i]:
Flag2=True
Flag1=False
break
if Flag2:
break
if Flag1:
return Flag1
Flag2=False
Flag1=True
for i in range(n+1):
for j in range(n+1):
if mat[i][j]!=target[n-i][n-j]:
Flag2=True
Flag1=False
break
if Flag2:
break
if Flag1:
return Flag1

Flag1=True
Flag2=False
for i in range(n+1):
for j in range(n+1):
if mat[i][j]!=target[n-j][i]:
Flag2=True
Flag1=False
break
if Flag2:
break
if Flag1:
return Flag1
Flag1=True
Flag2=False
for i in range(n+1):
for j in range(n+1):
if mat[i][j]!=target[i][j]:
Flag2=True
Flag1=False
break
if Flag2:
break

return Flag1


class Solution(object):
def reductionOperations(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
d={}
sets=sorted(list(set(nums)))
if len(sets)==1:
return 0
for i in nums:
if i in d:
d[i]+=1
else:
d[i]=1
result=0
for i in range(1,len(sets)):
result+=d[sets[i]]*i
return result


def counts(s,s1):
res=0
for i in range(len(s)):
if s[i]!=s1[i]:
res+=1
return res
class Solution(object):
def minFlips(self, s):
"""
:type s: str
:rtype: int
"""
res=99999999
if len(s)%2==0:
tem="01"*(len(s)//2)
tem1="10"*(len(s)//2)
res=min(counts(tem,s),counts(tem1,s))
else:
tem="01"*(len(s)//2)
re=counts(tem+"0",s)
res=min(re,res)
re=counts(tem+"1",s)
res=min(re,res)
re=counts("1"+tem,s)
res=min(re,res)
re=counts("0"+tem,s)
res=min(re,res)
tem="10"*(len(s)//2)
re=counts(tem+"0",s)
res=min(re,res)
re=counts(tem+"1",s)
res=min(re,res)
re=counts("1"+tem,s)
res=min(re,res)
re=counts("0"+tem,s)
res=min(re,res)
return res


https://leetcode-cn.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/solution/shi-er-jin-zhi-zi-fu-chuan-zi-fu-jiao-ti-i52p/

def get_counts(pack,box):
if max(pack)>max(box):
return -1
res=0
# pack.sort()
box.sort()
for i in pack:
#二分查找最适合的箱子
if i<=box[0]:
bo=box[0]
else:
left=0
right=len(box)-1
while left<right-1:
mid=(left+right)//2
if box[mid]>=i:
right=mid
else:
left=mid
bo=box[right]
res=(res+bo-i)
res=res%(10**9+7)
return res

class Solution(object):
def minWastedSpace(self, packages, boxes):
"""
:type packages: List[int]
:type boxes: List[List[int]]
:rtype: int
"""
t=[0 for _ in range(len(boxes))]##这么多可以选择的供应商
for i in range(len(boxes)):
t[i]=get_counts(packages[:],boxes[i])

if max(t)==-1:
return -1
else:
return min([i for i in t if i!=-1])


https://leetcode-cn.com/problems/minimum-space-wasted-from-packaging/solution/zhuang-bao-guo-de-zui-xiao-lang-fei-kong-90lk/

ranges中是各种区间，left和ight也是一个区间，题目的要求是判断ranges中的所有区间是否可以覆盖left到right区间内所有的值。

class Solution:
def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:

for i in range(left,right+1):
Flag=False
for j in ranges:
if i>=j[0] and i<=j[1]:
Flag=True
if Flag:
break
if Flag is False:
break
return Flag


class Solution:
def chalkReplacer(self, chalk: List[int], k: int) -> int:
sum1=sum(chalk)
k=k%sum1
if k==0:
return 0
for i in range(len(chalk)):
k=k-chalk[i]
if k<0:
return i



def is_true(grid,x,y,l):
"""
grid:给定的矩阵
x:开始的纵坐标
y:开始的横坐标
l:选定幻方的长度
"""
sum1=sum(grid[x][y:y+l])
sum2=sum([grid[x+i][y+i] for i in range(l)])
sum3=sum([grid[x+l-1-i][y+i] for i in range(l)])
if sum1!=sum2:
return False
if sum1!=sum3:
return False

for k in range(x+1,x+l):
tem=sum(grid[k][y:y+l])
if tem!=sum1:
return False
for k in range(y,y+l):
tem=sum([grid[i][k] for i in range(x,x+l)])
if tem!=sum1:
return False
return True


class Solution:
def largestMagicSquare(self, grid: List[List[int]]) -> int:
m,n=len(grid),len(grid[0])
res=1
def getK_k(grid,x,y):
max1=min(m-x,n-y)
if max1==1:
return 1
for i in range(max1,res,-1):
if is_true(grid,x,y,i):
return i
return 1
for i in range(m):
for j in range(n):
res=max(getK_k(grid,i,j),res)
if res>=n-j:
break
if res>=m-i:
break
return res


from functools import lru_cache
@lru_cache(None)
def get_is(s):
a = []
b = []
for i in s:
if i == "(":
b.append(i)
elif i == ")":
num = b.pop()
b.pop()
b.append(num)
elif i == "1" or i == "0":
b.append(i)
elif i == "|":
b.append("|")
elif i == "&":
b.append("&")
if b[-1] == "0" or b[-1] == "1" and len(b) >= 3:
while len(b) >= 3 and (b[-2] == "&" or b[-2] == "|"):
if b[-2] == "&" and b[-1] == "1" and b[-3] == "1":
b.pop()
b.pop()
b.pop()
b.append("1")
elif b[-2] == "|" and (b[-1] == "1" or b[-3] == "1"):
b.pop()
b.pop()
b.pop()
b.append("1")
else:
b.pop()
b.pop()
b.pop()
b.append("0")
return b[0]


class Solution:
def minOperationsToFlip(self, expression: str) -> int:
while expression != dele(expression):
expression = dele(expression)
if expression=="":
return 0
result = get_is(expression)
a = []
f = ""
for i in range(len(expression) - 1, -1, -1):
if expression[i] == ")":
a.append(expression[i])
if expression[i] == "(":
a.pop()
if (expression[i] == "|" or expression[i] == "&") and len(a) == 0:
left, right = expression[:i], expression[i + 1:]
f = expression[i]
break
if f == "":
return 1
if f == "|" and result == "0":
tem = self.minOperationsToFlip(right)
if tem == 1:
return 1
else:
return min(self.minOperationsToFlip(left), tem)
elif f == "|" and result == "1":
if get_is(left) == "0" or get_is(right) == "0":
return 1
else:
tem = self.minOperationsToFlip(right)
if tem == 1:
return 2
else:
return min(self.minOperationsToFlip(left), tem) + 1
elif f == "&" and result == "0":
if get_is(left) == "0" and get_is(right) == "0":
tem = self.minOperationsToFlip(right)
if tem == 1:
return 2
else:
return min(self.minOperationsToFlip(left), tem) + 1
else:
return 1
elif f == "&" and result == "1":
tem = self.minOperationsToFlip(right)
if tem == 1:
return 1
else:
return min(self.minOperationsToFlip(left), tem)


1：切割符号是"|",最后的值为0，那么很明显，左右两边的值都是0，那么我们只要把左右某一个变成1就好了。然后就算计算把左边或者右边变成1的最小操作次数。这不就是一个完美的递归公式么？
2：切割符号是"|",最后的值为1，分两种情况，如果左右两边都是1，那么需要将左边或者右边变成0，再将符号变成&就好了，如果左右两边有一个0，直接把符号变成&就好。
3：切割符号是"&",最后的值为0，也分为两种情况，如果左右两边都是0，那么需要将左右两边都一个变为1，然后将符号变成"|"。如果左右两边有一个1，那么只要把符号变成"|“就好了。
4：切割符号是”&",最后的值为1，只要把左右两边某一边变成0就好。

1：进行符号切割的时候，需要从右边遍历，因为它逻辑计算的时候是从左边开始，顺序计算。对于以下这个算例：
((0&(0&0)&(0|(0)&1&0)))，将最左边的两个0&改成1|最终的结果还是0，并不能改变整体的值。从右边切割才能切割成两个可以影响整体的部分。
2：给定的字符串中有很多无效的括号，其中有一个算例(别问我为什么知道)，左边49999个1，右边49999个1，所以我们需要将一些无效的括号删除，以下就是代码：

def dele(x):
a=[]
for i in range(len(x)):
if x[i]=="(":
a.append(x[i])
else:
break
b=[]
for j in range(len(x)-1,-1,-1):
if x[j]==")":
b.append(x[j])
else:
break
l=min(len(a),len(b))

while l>0:
c = []
flag =True
for i in range(l,len(x)-l):
if x[i]=="(":
c.append(x[i])
elif x[i] == ")":
if len(c) > 0 and a[-1] == "(":
c.pop()
else:
l=l-1
flag=False
break
if flag:
break
return x[l:len(x)-l]


• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
03-10 7万+
11-23 858
04-03

“相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。