0ms C++ Py3 1 line 解 Leetcode 3702 sequence With Non Zero Bitwise XOR
迴圈解不稀奇,Python一行解也不稀奇,C++一行解就很少了!
[C++ Py3 一行解請進 👇]
Loop solutions are not uncommon, nor are one-line Python solutions, but one-line C++ solutions are rare!
Test whether XOR sum of all x in nums is 0 or not &
test whether all x in nums are 0 in a single loop.
[codes on Leetcode]https://leetcode.com/problems/longest-subsequence-with-non-zero-bitwise-xor/solutions/8461505/branchless-loopbeats-100-by-anwendeng-6wkw/
#anwendeng
00:00 – Problem Introduction: Leetcode 3702
00:10 – Understanding the Objective: Longest Subsequence with Non-Zero XOR
00:25 – Logic Breakdown: Handling All-Zero vs. Mixed Cases
01:00 – Python Solution: The 1-Line Implementation
01:32 – C++ Solution: 1-Line Logic and Operator Implementation
02:08 – Final Results and 0ms Performance Summary
class Solution {
public:
int longestSubsequence(vector& nums) {
return ranges::all_of(nums, [](int x){ return x==0;})? 0:
nums.size()-(reduce(nums.begin(), nums.end(), 0, bit_xor<>())==0);
}
};
class Solution:
def longestSubsequence(self, nums: List[int]) -> int:
return 0 if all(x==0 for x in nums) else len(nums)-(reduce(xor, nums, 0)==0)
沒有留言:
張貼留言