记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
1.按照空格进行split
取前k个 合并
2.遍历字符串
遇到空格+1 一直到遍历结束或者遇到第k个空格
def truncateSentence1(s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
l = s.split(" ")
l = l[:k]
return " ".join(l)
def truncateSentence(s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
loc = 0
n = len(s)
while k>0:
if s[loc]==" ":
k-=1
loc+=1
if loc==n:
return s
return s[:loc-1]
BFS
从给顶点向四周广搜
mem记录已经到达过的连通分量
如果上下左右都是连通的 则这个点不在边界
def colorBorder(grid, row, col, color):
"""
:type grid: List[List[int]]
:type row: int
:type col: int
:type color: int
:rtype: List[List[int]]
"""
m,n = len(grid),len(grid[0])
oricolor = grid[row][col]
step = [(1,0),(0,1),(-1,0),(0,-1)]
l = [(row,col)]
change = []
mem={}
while l:
tmp = []
for x,y in l:
mem[(x,y)]=1
num = 0
for i,j in step:
nx,ny = x+i,y+j
if 0<=nx<m and 0<=ny<n and grid[nx][ny]==oricolor:
if (nx,ny) not in mem:
tmp.append((nx,ny))
num +=1
if num!=4:
change.append((x,y))
l = tmp[:]
for x,y in change:
grid[x][y] = color
return grid
从左到右三个滑动窗口
sum1,sum2,sum3分别记录当前三个滑动窗口各自的和
maxs1为第一个滑动窗口最大值
maxs2为前两个滑动窗口最大值
maxs3为三个滑动窗口最大值
maxs1loc为第一个滑动窗口最大值的起始位置
maxs2loc为前两个滑动窗口最大值的起始位置
maxs3loc及我们需要的答案ans
def maxSumOfThreeSubarrays(nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
ans = []
sum1,maxs1,maxs1loc = 0,0,0
sum2,maxs2,maxs2loc = 0,0,()
sum3,maxs3 = 0,0
n = len(nums)
for i in range(k*2,n):
sum1+=nums[i-k*2]
sum2+=nums[i-k]
sum3+=nums[i]
if i>=k*3-1:
if sum1>maxs1:
maxs1=sum1
maxs1loc = i-k*3+1
if maxs1+sum2>maxs2:
maxs2=maxs1+sum2
maxs2loc = (maxs1loc,i-k*2+1)
if maxs2+sum3>maxs3:
maxs3 = maxs2+sum3
ans = [maxs2loc[0],maxs2loc[1],i-k+1]
print(sum1,sum2,sum3)
print(maxs1,maxs2,maxs3)
print(maxs1loc,maxs2loc,ans)
sum1-=nums[i-k*3+1]
sum2-=nums[i-k*2+1]
sum3-=nums[i-k+1]
return ans
分类讨论
O的个数必定<=X的个数 并且|X-O|<=1
分别计算横,竖,斜 三种情况三个相同的个数
横竖每种情况只可能有一组共同 对角线可以两组都满足三个相同
满足条件必定是最后一步 X先走
如果X满足了三个一串 那么X个数必定>O个数
如果O满足了三个一串 那么O个数必定==X个数
def validTicTacToe(board):
"""
:type board: List[str]
:rtype: bool
"""
numO,numX = 0,0
for s in board:
for c in s:
if c=="O":
numO+=1
elif c=="X":
numX+=1
if numO>numX:
return False
if numX-numO>1:
return False
finish0,finish1,finish2 = 0,0,0
finishType = set()
for s in board:
if s=="XXX":
finish0+=1
finishType.add("X")
elif s=="OOO":
finish0+=1
finishType.add("O")
if finish0>1:
return False
for i in range(3):
s = board[0][i]+board[1][i]+board[2][i]
if s=="XXX":
finish1+=1
finishType.add("X")
elif s=="OOO":
finish1+=1
finishType.add("O")
if finish1>1:
return False
if board[0][0]==board[1][1]==board[2][2] and board[0][0]!=" ":
finish2+=1
finishType.add(board[1][1])
if board[0][2]==board[1][1]==board[2][0] and board[0][2]!=" ":
finish2+=1
finishType.add(board[1][1])
if len(finishType)>1:
return False
if finish0+finish1+finish2==0:
return True
if ("X"in finishType and numO==numX) or("O"in finishType and numO==numX-1):
return False
return True
check用来统计word中所有字符出现的次数
将words按照单词长度排序
从短到长与license中的字符次数比较 如果满足直接返回为最前的一个
def shortestCompletingWord(licensePlate, words):
"""
:type licensePlate: str
:type words: List[str]
:rtype: str
"""
from collections import defaultdict
licensePlate = licensePlate.lower()
words.sort(key=lambda x:len(x))
def check(word):
m = defaultdict(int)
for c in word:
m[c] +=1
return m
w = ""
for c in licensePlate:
if "a"<=c<="z":
w+=c
orim = check(w)
n = len(w)
for word in words:
if len(word)<n:
continue
curm = check(word)
ck = True
for k,v in orim.items():
if curm[k]<v:
ck=False
break
if ck:
return word
预处理 找到times内每个时间点的选举结果
二分查找不大于t的最大时间点结果
from collections import defaultdict
class TopVotedCandidate(object):
def __init__(self, persons, times):
"""
:type persons: List[int]
:type times: List[int]
"""
n = len(times)
self.times = times
self.person = [-1]*n
m = defaultdict(int)
curp,curv = -1,0
for i in range(n):
m[persons[i]]+=1
if curv<=m[persons[i]]:
curv = m[persons[i]]
curp = persons[i]
self.person[i] = curp
def q(self, t):
"""
:type t: int
:rtype: int
"""
if t<=self.times[0]:
return self.person[0]
if t>=self.times[-1]:
return self.person[-1]
l,r=0,len(self.times)
while l<=r:
mid = (l+r)//2
if self.times[mid]==t:
return self.person[mid]
elif t<self.times[mid]:
r = mid-1
else:
l = mid+1
return self.person[r]
可以直接使用提供的lower
也可以把大写ASCALL+32变为小写
def toLowerCase(self, s):
"""
:type s: str
:rtype: str
"""
return str.lower(str(s))