defk_sum(self, nums, target, k): if k == 2: returnself.two_sum(nums, target) # assume 3 sum res = [] for i inrange(len(nums)): if i >= 1and nums[i - 1] == nums[i]: # remember continue sub_res = self.k_sum(nums[i + 1:], target - nums[i], k - 1) for li in sub_res: res.append([nums[i]] + li) return res
deftwo_sum(self, nums, target): i, j, res = 0, len(nums) - 1, [] while i < j: if (i >= 1and nums[i - 1] == nums[i]) or nums[i] + nums[j] < target: i += 1 elif (j + 1 < len(nums) and nums[j] == nums[j + 1]) or nums[i] + nums[j] > target: j -= 1 else: res.append([nums[i], nums[j]]) # remember to use list rather than tuple i += 1# remember j -= 1 return res
Given the root of a binary tree, the value of a target node target, and an integer k, return an array of the values of all nodes that have a distancekfrom the target node.
You can return the answer in any order.
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2 Output: [7,4,1] Explanation: The nodes that are a distance 2 from the target node (with value 5) have values 7, 4, and 1.
Example 2:
Input: root = [1], target = 1, k = 3 Output: []
Constraints:
The number of nodes in the tree is in the range [1, 500].
0 <= Node.val <= 500 All the values Node.val are unique.
target is the value of one of the nodes in the tree. * 0 <= k <= 1000
Given a string s, find the length of the longest substring without repeating characters.
Example 1:
**Input:** s = "abcabcbb"
**Output:** 3
**Explanation:** The answer is "abc", with the length of 3.
Example 2:
**Input:** s = "bbbbb"
**Output:** 1
**Explanation:** The answer is "b", with the length of 1.
Example 3:
**Input:** s = "pwwkew"
**Output:** 3
**Explanation:** The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Example 4:
**Input:** s = ""
**Output:** 0
Constraints:
0 <= s.length <= 5 * 10<sup>4</sup>
s consists of English letters, digits, symbols and spaces.
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
defthreeSum(self, nums: List[int]) -> List[List[int]]: nums.sort() res = [] for i inrange(len(nums)): if i > 0and nums[i] == nums[i - 1]: continue sub_res = self.two_sum(nums, i + 1, -nums[i]) for li in sub_res: res.append([nums[i]] + li) return res
deftwo_sum(self, nums, start, target): j, k, res = start, len(nums) - 1, [] while j < k: if (j > start and nums[j] == nums[j - 1]) or nums[j] + nums[k] < target: j += 1 elif (k < len(nums) - 1and nums[k] == nums[k + 1]) or nums[j] + nums[k] > target: k -= 1 elif nums[j] + nums[k] == target: res.append([nums[j], nums[k]]) j += 1 k -= 1 return res
Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path.
In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names.
The canonical path should have the following format:
The path starts with a single slash '/'.
Any two directories are separated by a single slash '/'. The path does not end with a trailing '/'.
The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period '.' or double period '..')
Return the simplified canonical path.
Example 1:
Input: path = “/home/“ Output: “/home” Explanation: Note that there is no trailing slash after the last directory name.
Example 2:
Input: path = “/../“ Output: “/“ Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.
Example 3:
Input: path = “/home//foo/“ Output: “/home/foo” Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.
Constraints:
1 <= path.length <= 3000path consists of English letters, digits, period '.', slash '/' or '_'. * path is a valid absolute Unix path.
题目大意:
简化路径,遇.表示当前目录不做事,遇..表示到上一个目录
解题思路:
路径类似于括号题,利用括号题模板
解题步骤:
N/A
注意事项:
edge case /../ 表示若stack为空,就不pop。if stack不能加到elif token == ‘..’中
遇到..返回到上层目录
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
defsimplifyPath(self, path: str) -> str: path += '/' token, stack = '', [] for c in path: if c == '/': if token == '.': pass elif token == '..': if stack: # remember stack.pop() elif token: stack.append(token) token = '' else: token += c return'/' + '/'.join(stack)