Given a string num that contains only digits and an integer target, return all possibilities to insert the binary operators'+','-', and/or'*'between the digits ofnumso that the resultant expression evaluates to thetargetvalue.
Note that operands in the returned expressions should not contain leading zeros.
Example 1:
Input: num = “123”, target = 6 Output: [“123”,”1+2+3”] Explanation: Both “123” and “1+2+3” evaluate to 6.
Example 2:
Input: num = “232”, target = 8 Output: [“23+2”,”2+32”] Explanation: Both “23+2” and “2+32” evaluate to 8.
Example 3:
Input: num = “3456237490”, target = 9191 Output: [] Explanation: There are no expressions that can be created from “3456237490” to evaluate to 9191.
Constraints:
1 <= num.length <= 10num consists of only digits. * -2<sup>31</sup> <= target <= 2<sup>31</sup> - 1
Suppose you are at a party with n people labeled from 0 to n - 1 and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know the celebrity, but the celebrity does not know any of them.
Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is ask questions like: “Hi, A. Do you know B?” to get information about whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).
You are given a helper function bool knows(a, b) that tells you whether A knows B. Implement a function int findCelebrity(n). There will be exactly one celebrity if they are at the party.
Return the celebrity’s label if there is a celebrity at the party. If there is no celebrity, return -1.
Example 1:
Input: graph = [[1,1,0],[0,1,0],[1,1,1]] Output: 1 Explanation: There are three persons labeled with 0, 1 and 2. graph[i][j] = 1 means person i knows person j, otherwise graph[i][j] = 0 means person i does not know person j. The celebrity is the person labeled as 1 because both 0 and 2 know him but 1 does not know anybody.
Example 2:
Input: graph = [[1,0,1],[1,1,0],[0,1,1]] Output: -1 Explanation: There is no celebrity.
Constraints:
n == graph.lengthn == graph[i].length 2 <= n <= 100graph[i][j] is 0 or 1. graph[i][i] == 1
Follow up: If the maximum number of allowed calls to the API knows is `3 n`, could you find a solution without exceeding the maximum number of calls?
deffindCelebrity(self, n: int) -> int: # find potential candidate candidate = 0 for i in range(1, n): ifnot knows(i, candidate): candidate = i # validate for i in range(n): ifnot knows(i, candidate): return-1 for i in range(n): if candidate != i and knows(candidate, i): return-1 return candidate
Constraints: The number of nodes in the tree is in the range [1, 10<sup>4</sup>]. 0 <= Node.val <= 10<sup>9</sup> * -10<sup>9</sup> <= target <= 10<sup>9</sup>
题目大意:
求BST中最接近target的值
解题思路:
接近target的值在BST搜索路径上,越后搜索到的(越后入栈的)越接近,但最接近的可能大于或小于target(predecessors or successors),只能逐一比较. 类似于LeetCode 272 Closest Binary Search Tree Value II
解题步骤:
N/A
注意事项:
循环中用it,不能用root,注意检查
接近target的值在BST搜索路径上,逐一比较
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
defclosestValue(self, root: TreeNode, target: float) -> int: closest_vals = [] it = root while it: closest_vals.append(it.val) if target < it.val: it = it.left else: it = it.right min_val, res = float('inf'), 0 for n in closest_vals: if abs(target - n) < min_val: min_val = abs(target - n) res = n return res
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
Example 1:
Input: nums = [3,0,1] Output: 2 Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
Example 2:
Input: nums = [0,1] Output: 2 Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
Example 3:
Input: nums = [9,6,4,2,3,5,7,0,1] Output: 8 Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
Constraints:
n == nums.length1 <= n <= 10<sup>4</sup> 0 <= nums[i] <= n All the numbers of nums are unique.
Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
题目大意:
数组缺失一个数,所有数应该在[0, n]内,求缺失数
排序法解题思路:
N/A
解题步骤:
N/A
注意事项:
Python代码:
1 2 3
defcanPermutePalindrome(self, s: str) -> bool: char_to_count = collections.Counter(s) returnFalseif len([count for count in char_to_count.values() if count % 2 == 1]) > 1elseTrue
算法分析:
时间复杂度为O(nlogn),空间复杂度O(1)
异或法解题思路II:
高斯原理
Python代码:
1 2 3 4 5
defmissingNumber2(self, nums: List[int]) -> int: res = len(nums) # remember for i, n in enumerate(nums): res ^= i ^ n return res