/* 自定義代碼塊樣式 */

herrDeng網內搜尋

自訂搜尋

Ads

2026年8月15日 星期六

0ms C++ Py3 1 line 解 Leetcode 3702 sequence With Non Zero Bitwise XOR


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.

 #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)

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章