# LeetCode 每日一题 2021/11/22-2021/11/28

Exercise 专栏收录该内容
65 篇文章 0 订阅

### 11/22 384. 打乱数组

i为当前考虑位置
i位置的数随机从[i,n]中选取一个loc

import random
class Solution(object):

def __init__(self, nums):
"""
:type nums: List[int]
"""
self.n = len(nums)
self.ori = nums

def reset(self):
"""
:rtype: List[int]
"""
return self.ori

def shuffle(self):
"""
:rtype: List[int]
"""
ans = []
ans.extend(self.ori)
for i in range(self.n):
loc = random.randint(i,self.n-1)
ans[i],ans[loc]=ans[loc],ans[i]
return ans



### 11/23 859. 亲密字符串

def buddyStrings(s, goal):
"""
:type s: str
:type goal: str
:rtype: bool
"""
if len(s)!=len(goal):
return False
if s==goal:
if len(set(s))==len(s):
return False
return True
diff = 0
diffs = []
diffg = []
for i in range(len(s)):
if s[i]!=goal[i]:
diff+=1
diffs.append(s[i])
diffg.append(goal[i])
if diff>2:
return False
if diff==2:
if diffs[0]==diffg[1] and diffs[1]==diffg[0]:
return True
return False
if diff==1:
return False


### 11/24 423. 从英文中重建数字

zero-z,two-w,four-u,six-x,eight-g
one-o,three-r,five-f,seven-s
nine-n

def originalDigits(s):
"""
:type s: str
:rtype: str
"""
from collections import defaultdict
m = defaultdict(int)
for c in s:
m[c]+=1
l = [0]*10

l[0] = m['z']
l[2] = m['w']
l[4] = m['u']
l[6] = m['x']
l[8] = m['g']

l[1] = m['o'] - l[0]-l[2]-l[4]
l[3] = m['r'] - l[4]-l[0]
l[5] = m['f'] - l[4]
l[7] = m['s'] - l[6]
l[9] = m['i'] - l[5]-l[6]-l[8]

ans = ""
for i in range(10):
ans += str(i)*l[i]
return ans


### 11/25 458. 可怜的小猪

m = minutesToTest/minutesToDie
m表示在没有喝到毒药的情况下 小猪最多能和几桶水

x x x
x x x
x x x

def poorPigs(buckets, minutesToDie, minutesToTest):
"""
:type buckets: int
:type minutesToDie: int
:type minutesToTest: int
:rtype: int
"""
n = minutesToTest/minutesToDie+1
num = 0
tmp = 1
while tmp<buckets:
tmp *= n
num +=1
return num


### 11/26 700. 二叉搜索树中的搜索

class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def searchBST(root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
node = root
while node:
if node.val==val:
return node
elif node.val<val:
node = node.right
else:
node = node.left
return node


### 11/27 519. 随机翻转矩阵

total记录当前拥有的可选择位置个数

import random
class Solution(object):

def __init__(self, m, n):
"""
:type m: int
:type n: int
"""
self.m=m
self.n=n
self.total = m*n
self.map = {}

def flip(self):
"""
:rtype: List[int]
"""
tmp = random.randint(0,self.total-1)
self.total-=1
idx = self.map.get(tmp,tmp)
self.map[tmp] = self.map.get(self.total,self.total)
return [idx//self.n,idx%self.n]

def reset(self):
"""
:rtype: None
"""
self.map={}
self.total = self.n*self.m


### 11/28 438. 找到字符串中所有字母异位词

need记录需要的字符个数
window记录当前窗口内的字符个数
left,right记录滑动窗口左右边界
allvalid代表需要的字符种类
valid代表当前窗口满足个数的字符种类

def findAnagrams(s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
from collections import defaultdict
need=defaultdict(int)
window = defaultdict(int)
for c in p:
need[c]+=1
left,right=0,0
valid=0
allvalid = len(need)
ret = []
while right<len(s):
c = s[right]
right+=1
if need[c]>0:
window[c]+=1
if window[c]==need[c]:
valid+=1
while right-left>=len(p):
if valid==allvalid:
ret.append(left)
d = s[left]
left+=1
if need[d]>0:
if window[d]==need[d]:
valid-=1
window[d]-=1
return ret


• 0
点赞
• 0
评论
• 0
收藏
• 打赏
• 扫一扫，分享海报

alphaTao

¥2 ¥4 ¥6 ¥10 ¥20

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