You are given a 0-indexed integer array nums and a target element target.
A target index is an index i such that nums[i] == target.
Return a list of the target indices ofnums after sortingnumsin non-decreasing order. If there are no target indices, return an empty list. The returned list must be sorted in increasing order.
Example 1:
Input: nums = [1,2,5,2,3], target = 2 Output: [1,2] Explanation: After sorting, nums is [1,2,2,3,5]. The indices where nums[i] == 2 are 1 and 2.
Example 2:
Input: nums = [1,2,5,2,3], target = 3 Output: [3] Explanation: After sorting, nums is [1,2,2,3,5]. The index where nums[i] == 3 is 3.
Example 3:
Input: nums = [1,2,5,2,3], target = 5 Output: [4] Explanation: After sorting, nums is [1,2,2,3,5]. The index where nums[i] == 5 is 4.
for i in range(target_index - 1, -1, -1): if sorted_nums[i] == target: res.append(i) res = res[::-1] for i in range(target_index, len(sorted_nums)): if sorted_nums[i] == target: res.append(i) return res
deftargetIndices(self, nums: List[int], target: int) -> List[int]: sorted_nums = sorted(nums) target_upper_index = self.last_position(sorted_nums, target) target_lower_index = self.first_position(sorted_nums, target) res = [i for i in range(target_lower_index, target_upper_index + 1)] return [] if target_upper_index == -1else res
deffirst_position(self, nums: List[int], target: int) -> int: ifnot nums: return-1 start, end = 0, len(nums) - 1 while start + 1 < end: mid = start + (end - start) // 2 if target < nums[mid]: end = mid elif target > nums[mid]: start = mid else: end = mid
if nums[start] == target: return start elif nums[end] == target: return end else: return-1
deflast_position(self, nums: List[int], target: int) -> int: ifnot nums: return-1 start, end = 0, len(nums) - 1 while start + 1 < end: mid = start + (end - start) // 2 if target < nums[mid]: end = mid elif target > nums[mid]: start = mid else: # Depends on the target on the right side or left side. For fist pos, use end = mid start = mid
if nums[end] == target: return end elif nums[start] == target: return start else: return-1
You are given two strings order and s. All the words of order are unique and were sorted in some custom order previously.
Permute the characters of s so that they match the order that order was sorted. More specifically, if a character x occurs before a character y in order, then x should occur before y in the permuted string.
Return any permutation ofsthat satisfies this property.
Example 1:
Input: order = “cba”, s = “abcd” Output: “cbad” Explanation: “a”, “b”, “c” appear in order, so the order of “a”, “b”, “c” should be “c”, “b”, and “a”. Since “d” does not appear in order, it can be at any position in the returned string. “dcba”, “cdba”, “cbda” are also valid outputs.
Example 2:
Input: order = “cbafg”, s = “abcd” Output: “cbad”
Constraints:
1 <= order.length <= 261 <= s.length <= 200 order and s consist of lowercase English letters.
All the characters of order are unique.
defcustomSortString(self, order: str, s: str) -> str: char_to_count = collections.Counter(s) res = '' for c in order: if c in char_to_count: res += c * char_to_count[c] char_to_count.pop(c) for c, count in char_to_count.items(): res += c * count return res
Convert a Binary Search Tree to a sorted Circular Doubly-Linked List in place.
You can think of the left and right pointers as synonymous to the predecessor and successor pointers in a doubly-linked list. For a circular doubly linked list, the predecessor of the first element is the last element, and the successor of the last element is the first element.
We want to do the transformation in place. After the transformation, the left pointer of the tree node should point to its predecessor, and the right pointer should point to its successor. You should return the pointer to the smallest element of the linked list.
Example 1:
Input: root = [4,2,5,1,3]
Output: [1,2,3,4,5]
Explanation: The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor relationship.
Example 2:
Input: root = [2,1,3] Output: [1,2,3]
Constraints:
The number of nodes in the tree is in the range [0, 2000].
-1000 <= Node.val <= 1000 All the values of the tree are *unique.
题目大意:
将二叉树变成双向链表,left为父节点,right为儿子节点
解题思路:
类似于LeetCode 114 Flatten Binary Tree to Linked List,先假设左右儿子,已经是双向LL,下面就是将root这个节点插入到两个LL之间其将它们首尾相连
A string can be abbreviated by replacing any number of non-adjacent, non-empty substrings with their lengths. The lengths should not have leading zeros.
For example, a string such as "substitution" could be abbreviated as (but not limited to):
"s10n" ("s <u>ubstitutio</u> n")
"sub4u4" ("sub <u>stit</u> u <u>tion</u>") "12" ("<u>substitution</u>")
"su3i1u2on" ("su <u>bst</u> i <u>t</u> u <u>ti</u> on") "substitution" (no substrings replaced)
The following are not valid abbreviations:
"s55n" ("s <u>ubsti</u> <u>tutio</u> n", the replaced substrings are adjacent) "s010n" (has leading zeros)
"s0ubstitution" (replaces an empty substring)
Given a string word and an abbreviation abbr, return whether the string matches the given abbreviation.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: word = “internationalization”, abbr = “i12iz4n” Output: true Explanation: The word “internationalization” can be abbreviated as “i12iz4n” (“i nternational iz atio n”).
Example 2:
Input: word = “apple”, abbr = “a2e” Output: false Explanation: The word “apple” cannot be abbreviated as “a2e”.
Constraints:
1 <= word.length <= 20word consists of only lowercase English letters. 1 <= abbr.length <= 10abbr consists of lowercase English letters and digits. * All the integers in abbr will fit in a 32-bit integer.
It is the empty string,
It can be written as AB (A concatenated with B), where A and B are valid strings, or It can be written as (A), where A is a valid string.
You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.
For example, if s = "()))", you can insert an opening parenthesis to be "(**(**)))" or a closing parenthesis to be "())**)**)".
Return the minimum number of moves required to makesvalid.
defminAddToMakeValid2(self, s: str) -> int: left, res = 0, 0 for char in s: if char == '(': left += 1 else: left -= 1 if left < 0: res += 1 left = 0 return res + abs(left)