cyy的垃圾堆


  • 首页
  • 归档
  • 标签
  • 关于CYY
  • 友链

© 2025 CYY

Theme Typography by Makito

Proudly published with Hexo

数组相关的一些

发布于 2023-05-28 数组 

太弱了,每次遇到数组都要查一遍,稍微写下,希望能加深下吧


一个数组题

6440. 对角线上不同值的数量差

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution:
def differenceOfDistinctValues(self, grid: List[List[int]]) -> List[List[int]]:
l=[]
r=[]
n=len(grid)
m=len(grid[0])
ans = [[0 for i in range(m)] for j in range(n)]
for i in range(0,n):
for j in range(0,m):
l=[]
r=[]
tempx=j-1
tempy=i-1
while tempx>=0 and tempy>=0:
if grid[tempy][tempx] not in l:
l.append(grid[tempy][tempx])
tempx-=1
tempy-=1
tempx=j+1
tempy=i+1
while tempx<m and tempy<n:
if grid[tempy][tempx] not in r:
r.append(grid[tempy][tempx])
tempx+=1
tempy+=1
ans[i][j]=abs(len(l)-len(r))
return ans
创建
1
2
3
4
ans=[]
ans1=[0]*n ##一维
ans2=[[0]*n]*n ##虚假的二维,改动一个元素的值,实际会带动n个值得改变,浅拷贝
ans3=[[0 for i in range(n)] for j in range(n)] ##产生实际可用的二维,确定长度的数组
加入和删除
1
2
ans.append("add")
del ans[n] ##删去索引为n的元素
排序
1
2
ans.sort()  ##reverse = True 降序, reverse = False 升序(默认)
##可以利用lambda,自定义排序方式
遍历
1
2
3
4
5
6
7
8
for i in ans:
for i in range(0,len(ans)):
for i,x in enumerate(ans):##索引,元素
##二维
n=len(grid)
m=len(grid[0])
for i in range(0,n):
for j in range(0,m):
字符串
1
2
3
s="qqqqqqqqqqqqq"##字符串操作类似,但不能进行删改,需转化
s1=list(s)
s2="".join(s1)##回到字符串
查找
1
2
ans[-1]##为数组最后一个元素

分享到 

 上一篇: 如何成为solidwork拉伸切除大师 下一篇: vhdl 

© 2025 CYY

Theme Typography by Makito

Proudly published with Hexo