71
Title description:
Give the absolute path of a file in Unix style, you need to simplify it. Or in other words, convert it to a canonical path.
In a Unix-style file system, a dot (.) indicates the current directory itself; in addition, two dots (...) indicate that the directory is switched to the upper level (pointing to the parent directory); both can be composed of complex relative paths section. For more information, please refer to: Absolute Path vs Relative Path in Linux/Unix.
Please note that the returned canonical path must always start with a slash /, and there must be only one slash / between the two directory names. The last directory name (if it exists) cannot end with /. In addition, the canonical path must be the shortest string representing an absolute path.
Example:
Answer:
class Solution:
def simplifyPath(self, path: str) -> str:
stack=[]
for i in path.split('/'):
if i not in['','.','..']:
stack.append(i)
elif i=='..'and stack:
stack.pop()
return "/"+"/".join(stack)
94
Title description:
Given the root node root of a binary tree, return its mid-order traversal.
Example:
answer:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
stack,res=[],[]
cur=root
while stack or cur:
if cur:
stack.append(cur)
cur=cur.left
else:
cur=stack.pop()
res.append(cur.val)
cur=cur.right
return res
150
Title description:
Find the value of the expression according to the inverse Polish notation.
Valid operators include +, -, *, /. Each operand can be an integer or another inverse Polish expression.
Note:
Integer division only retains the integer part.
The given reverse Polish expression is always valid. In other words, the expression always yields a valid value and there is no case where the divisor is zero.
Example:
Answer:
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
f1 = lambda a,b:a+b
f2 = lambda a,b:a-b
f3 = lambda a,b:a*b
f4 = lambda a,b:int(a/b)
maps = {
'+':f1,'-':f2,'*':f3,'/':f4}
stack = []
for i in tokens:
if i in maps:
a = stack.pop()
b = stack.pop()
stack.append(maps[i](b,a))
else:
i = int(i)
stack.append(i)
return stack[-1]
978
Title description:
When the sub-array A[i], A[i+1], …, A[j] of A satisfies the following conditions, we call it a turbulent sub-array:
if i <= k <j, when k is When an odd number, A[k]> A[k+1], and when k is an even number, A[k] <A[k+1];
or if i <= k <j, when k is an even number , A[k] [k]> A[k+1], and when k is an odd number, A[k] <A[k+1].
That is, if the comparison sign flips between each pair of adjacent elements in the sub-array, the sub-array is a turbulent sub-array.
Returns the length of the maximum turbulence subarray of A.
Example:
Answer:
class Solution:
def maxTurbulenceSize(self, arr: List[int]) -> int:
N = len(arr)
up = [1] * N
down = [1] * N
res = 1
for i in range(1, N):
if arr[i - 1] < arr[i]:
up[i] = down[i - 1] + 1
elif arr[i - 1] > arr[i]:
down[i] = up[i - 1] + 1
res = max(res, max(up[i], down[i]))
return res
992
Title description:
Given a positive integer array A, if the number of different integers in a sub-array of A happens to be K, then this continuous, not necessarily independent sub-array of A is called a good sub-array.
(For example, there are 3 different integers in [1,2,3,1,2]: 1, 2, and 3.)
Returns the number of good subarrays in A.
Example:
Answer:
class Solution:
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
left, right = 0, 0
res = 0
N = len(A)
window = collections.defaultdict(int) # 用于保留K个不重复数字的窗口
count = 0 # 用于表示当前[left, right]有多少个不重复数字
while right < N:
if window[A[right]] == 0:
count += 1
window[A[right]] += 1
# 1.当窗口大于K个不重复数字时,left指针右移
while count > K:
window[A[left]] -= 1
if window[A[left]] == 0:
count -= 1
left += 1
temp = left # 临时指针,这个记录一下对于同一个right,有多少个候选的left也满足条件
# 2.正好有K个不重复数字时,记录结果
# 同时用temp指针从left向右移动,看是否依旧满足,满足则res+1
while count == K:
res += 1
window[A[temp]] -= 1
if window[A[temp]] == 0:
count -= 1
temp += 1
# 3.将指针和window以及count恢复成原样,以进一步向右移动right做准备
while temp > left:
if window[A[temp - 1]] == 0:
count += 1
window[A[temp - 1]] += 1
temp -= 1
right += 1
return res