为了方便无法fanqiang的同学们,特地开通了B站,欢迎关(chong)注(dian)
视频上(shen)传(he)中
September 27, 2025
We have an array A
of non-negative integers.
For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]]
(with i <= j
), we take the bitwise OR of all the elements in B
, obtaining a result A[i] | A[i+1] | ... | A[j]
.
Return the number of possible results. (Results that occur more than once are only counted once in the final answer.)
Example 1:
Input: [0] Output: 1 Explanation: There is only one possible result: 0.
Example 2:
Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3.
Example 3:
Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.
Note:
1 <= A.length <= 50000
0 <= A[i] <= 10^9
dp[i][j] := A[i] | A[i + 1] | … | A[j]
dp[i][j] = dp[i][j – 1] | A[j]
ans = len(set(dp))
Time complexity: O(n^2)
Space complexity: O(n^2) -> O(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Author: Huahua // Running time: TLE (73/83 passed). class Solution { public: int subarrayBitwiseORs(vector<int>& A) { int n = A.size(); vector<vector<int>> dp(n, vector<int>(n)); unordered_set<int> ans(begin(A), end(A)); for (int l = 1; l <= n; ++l) for (int i = 0; i <= n - l; ++i) { int j = i + l - 1; if (l == 1) { dp[i][j] = A[i]; continue; } dp[i][j] = dp[i][j - 1] | A[j]; ans.insert(dp[i][j]); } return ans.size(); } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Author: Huahua // Running time: TLE (75/83 passed) class Solution { public: int subarrayBitwiseORs(vector<int>& A) { int n = A.size(); vector<int> dp(A); unordered_set<int> ans(begin(A), end(A)); for (int l = 2; l <= n; ++l) for (int i = 0; i <= n - l; ++i) ans.insert(dp[i] |= A[i + l - 1]); return ans.size(); } }; |
dp[i] := {A[i], A[i] | A[i – 1], A[i] | A[i – 1] | A[i – 2], … , A[i] | A[i – 1] | … | A[0]}, bitwise ors of all subarrays end with A[i].
|dp[i]| <= 32
Proof: all the elements (in the order of above sequence) in dp[i] are monotonically increasing by flipping 0 bits to 1 from A[i].
There are at most 32 0s in A[i]. Thus the size of the set is <= 32.
证明: dp[i] = {A[i], A[i] | A[i – 1], A[i] | A[i – 1] | A[i – 2], … , A[i] | A[i – 1] | … | A[0]},这个序列单调递增,通过把A[i]中的0变成1。A[i]最多有32个0。所以这个集合的大小 <= 32。
e.g. 举例:Worst Case 最坏情况 A = [8, 4, 2, 1, 0] A[i] = 2^(n-i)。
A[5] = 0,dp[5] = {0, 0 | 1, 0 | 1 | 2, 0 | 1 | 2 | 4, 0 | 1 | 2 | 4 | 8} = {0, 1, 3, 7, 15}.
Time complexity: O(n*log(max(A))) < O(32n)
Space complexity: O(n*log(max(A)) < O(32n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Author: Huahua // Running time: 456 ms class Solution { public: int subarrayBitwiseORs(vector<int>& A) { unordered_set<int> ans; unordered_set<int> cur; unordered_set<int> nxt; for (int a : A) { nxt.clear(); nxt.insert({a}); for (int b : cur) { nxt.insert(a | b); } cur.swap(nxt); ans.insert(begin(cur), end(cur)); } return ans.size(); } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Author: Huahua // Running time: 434 ms class Solution { public int subarrayBitwiseORs(int[] A) { Set<Integer> ans = new HashSet<>(); Set<Integer> cur = new HashSet<>(); for (int a : A) { Set<Integer> nxt = new HashSet<>(); nxt.add(a); for (int b : cur) nxt.add(a | b); ans.addAll(nxt); cur = nxt; } return ans.size(); } } |
1 2 3 4 5 6 7 8 9 10 |
# Author: Huahua # Running time: 812 ms class Solution: def subarrayBitwiseORs(self, A): cur = set() ans = set() for a in A: cur = {a | b for b in cur} | {a} ans |= cur return len(ans) |
A string S
of lowercase letters is given. Then, we may make any number of moves.
In each move, we choose one of the first K
letters (starting from the left), remove it, and place it at the end of the string.
Return the lexicographically smallest string we could have after any number of moves.
Example 1:
Input: S = "cba", K = 1 Output: "acb" Explanation: In the first move, we move the 1st character ("c") to the end, obtaining the string "bac". In the second move, we move the 1st character ("b") to the end, obtaining the final result "acb".
Example 2:
Input: S = "baaca", K = 3 Output: "aaabc" Explanation: In the first move, we move the 1st character ("b") to the end, obtaining the string "aacab". In the second move, we move the 3rd character ("c") to the end, obtaining the final result "aaabc".
Note:
1 <= K <= S.length <= 1000
S
consists of lowercase letters only.if \(k =1\), we can only rotate the string.
if \(k > 1\), we can bubble sort the string.
Time complexity: O(n^2)
Space complexity: O(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Author: Huahua // Running time: 4 ms class Solution { public: string orderlyQueue(string S, int K) { if (K > 1) { sort(begin(S), end(S)); return S; } string ans = S; for (int i = 1; i < S.size(); ++i) ans = min(ans, S.substr(i) + S.substr(0, i)); return ans; } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Author: Huahua class Solution { public String orderlyQueue(String S, int K) { if (K > 1) { char[] chars = S.toCharArray(); Arrays.sort(chars); return new String(chars); } String ans = S; for (int i = 1; i < S.length(); ++i) { String tmp = S.substring(i) + S.substring(0, i); if (tmp.compareTo(ans) < 0) ans = tmp; } return ans; } } |
1 2 3 4 5 |
# Author: Huahua 48 ms class Solution: def orderlyQueue(self, S, K): if K > 1: return ''.join(sorted(S)) return min([S[i:] + S[0:i] for i in range(0, len(S))]) |
1 2 3 4 5 6 7 8 |
# Author: Huahua 36 ms class Solution: def orderlyQueue(self, S, K): if K > 1: return ''.join(sorted(S)) ans = S for i in range(0, len(S)): ans = min(ans, S[i:] + S[0:i]) return ans |
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.
Example 1: Input: [5,3,6,2,4,null,8,1,null,null,null,7,9] 5 / \ 3 6 / \ \ 2 4 8 / / \ 1 7 9 Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9] 1 \ 2 \ 3 \ 4 \ 5 \ 6 \ 7 \ 8 \ 9
Note:
Time complexity: O(n)
Space complexity: O(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Author: Huahua // Running time: 64 ms class Solution { public: TreeNode* increasingBST(TreeNode* root) { TreeNode dummy(0); prev_ = &dummy; inorder(root); return dummy.right; } private: TreeNode* prev_; void inorder(TreeNode* root) { if (root == nullptr) return; inorder(root->left); prev_->right = root; prev_ = root; prev_->left = nullptr; inorder(root->right); } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Solution { private TreeNode prev; public TreeNode increasingBST(TreeNode root) { TreeNode dummy = new TreeNode(0); this.prev = dummy; inorder(root); return dummy.right; } private void inorder(TreeNode root) { if (root == null) return; inorder(root.left); this.prev.right = root; this.prev = root; this.prev.left = null; inorder(root.right); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Solution: def increasingBST(self, root): dummy = TreeNode(0) self.prev = dummy def inorder(root): if not root: return None inorder(root.left) root.left = None self.prev.right = root self.prev = root inorder(root.right) inorder(root) return dummy.right |
An array is monotonic if it is either monotone increasing or monotone decreasing.
An array A
is monotone increasing if for all i <= j
, A[i] <= A[j]
. An array A
is monotone decreasing if for all i <= j
, A[i] >= A[j]
.
Return true
if and only if the given array A
is monotonic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Author: Huahua class Solution { public: bool isMonotonic(vector<int>& A) { bool inc = true; bool dec = true; for (int i = 1; i < A.size(); ++i) { inc &= A[i] >= A[i - 1]; dec &= A[i] <= A[i - 1]; } return inc || dec; } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Solution { public boolean isMonotonic(int[] A) { boolean inc = true; boolean dec = true; for (int i = 1; i < A.length; ++i) { inc &= A[i] >= A[i - 1]; dec &= A[i] <= A[i - 1]; } return inc || dec; } } |
1 2 3 4 5 6 7 8 9 10 11 |
# Author: Huahua class Solution: def isMonotonic(self, A): inc = True; dec = True; for i in range(1, len(A)): inc = inc and A[i] >= A[i - 1] dec = dec and A[i] <= A[i - 1] return inc or dec; |