388. Longest Absolute File Path:很巧妙的答案,把"\t"做为当前深度,然后计算当前深度的前缀长度=上一个深度+文件名长度+1
class Solution(object):
def lengthLongestPath(self, input):
"""
:type input: str
:rtype: int
"""
maxlen = 0
pathlen = {0: 0}
for line in input.split("\n"):
name = line.lstrip('\t')
depth = len(line) - len(name)
if '.' in name:
maxlen = max(maxlen, pathlen[depth] + len(name))
else:
pathlen[depth + 1] = pathlen[depth] + len(name) + 1
return maxlen
390. Elimination Game: 这种题目,直接暴力求解的话TLE了,不过这样的题目,除了多做几遍,把答案背下来,真不知道怎么去理解:https://leetcode.com/problems/elimination-game/#/discuss 基本的想法是维护左边的head的值,从左朝右的时候,head = head + step 从右朝左的时候,如果l % 2 == 1也就是奇数,也就是能删掉当前的head,那么就是head = head + step。
392. Is Subsequence: 难一点的在于followup问题,可以用binary search 和trie来做
393. UTF-8 Validation:这题基本思路是会的,只是写法有点问题,太繁琐,再重写一遍
394. Decode String: 虽然磕磕绊绊的,也算是用stack做出来了吧
395. Longest Substring with At Least K Repeating Characters: 想了半天的前向型双指针,后来发现不对。只要简单的递归就可以了
396. Rotate Function: 又是一道没想出来的,感觉思路接近了,就是没想出来
397. Integer Replacement: 又是一题不会做,今天状态又变差了吗?
class Solution(object):
def integerReplacement(self, n):
"""
:type n: int
:rtype: int
"""
rtn = 0
while n > 1:
rtn += 1
if n % 2 == 0:
n //= 2
elif n % 4 == 1 or n == 3:
n -= 1
else:
n += 1
return rtn
398. Random Pick Index: 又一道神奇的Reservoir Sampling。
399. Evaluate Division:创建一个带权重的有向图,但是状态不好,今天不想做了,先标记下来,明天再做吧。