comments | difficulty | edit_url | tags | ||
---|---|---|---|---|---|
true |
Easy |
|
Given an array nums
of length n
, return an array answer
of length n - 1
such that answer[i] = nums[i] | nums[i + 1]
where |
is the bitwise OR
operation.
Example 1:
Input: nums = [1,3,7,15]
Output: [3,7,15]
Example 2:
Input: nums = [8,4,2]
Output: [12,6]
Example 3:
Input: nums = [5,4,9,11]
Output: [5,13,11]
Constraints:
2 <= nums.length <= 100
0 <= nums[i] <= 100
We iterate through the first
The time complexity is
class Solution:
def orArray(self, nums: List[int]) -> List[int]:
return [a | b for a, b in pairwise(nums)]
class Solution {
public int[] orArray(int[] nums) {
int n = nums.length;
int[] ans = new int[n - 1];
for (int i = 0; i < n - 1; ++i) {
ans[i] = nums[i] | nums[i + 1];
}
return ans;
}
}
class Solution {
public:
vector<int> orArray(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n - 1);
for (int i = 0; i < n - 1; ++i) {
ans[i] = nums[i] | nums[i + 1];
}
return ans;
}
};
func orArray(nums []int) (ans []int) {
for i, x := range nums[1:] {
ans = append(ans, x|nums[i])
}
return
}
function orArray(nums: number[]): number[] {
return nums.slice(0, -1).map((v, i) => v | nums[i + 1]);
}