当前位置:网站首页>leetcode1863_ 2021-10-14
leetcode1863_ 2021-10-14
2022-06-24 21:53:00 【Programming rookie】
leetcode1863 Find the sum of all your XORs and sum them up
Law 1 :
Each number in the array has two states: select and deselect , Set the array size to n. We use the first of an integer n Bit to simulate the selection state of each subset , The size of this integer is determined by 0( An empty set ) To (1 << n) - 1( The complete ). Then we iterate over the array , At the same time, check the bit of this integer , If 1, Just XOR on that bit ; by 0 Then skip directly .
class Solution {
public:
int subsetXORSum(vector<int>& nums) {
int n = nums.size();
int ans = 0;
for(int i = 0; i < (1 << n); ++i){
// Every integer i It represents a subset
int ret = 0;
for(int j = 0; j < n; ++j){
if((i >> j) & 1) // If i Of j Position as 1, On XOR
ret ^= nums[j];
}
ans += ret; // Plus the XOR and of this subset
}
return ans;
}
};
Law two :
We use dfs. Set the array length to n. function dfs There are three parameters ,dfs(int val, int index, nums);
val representative [0, index - 1] Exclusive or values , It is known. . and [index, n - 1] It is unknown. . Consider the index
position , There are two states: select and deselect , If selected , that val It becomes val^nums[index], If you don't select , that val = val unchanged . We make use of index == n To judge the end . Use res To maintain the sum of exclusive or subsets .
class Solution {
public:
int n;
int res;
void dfs(int val, int index, vector<int>& nums){
if(index == n){
//index == n, Represents that you have reached the head of the array
res += val;
return;
}
// For the two states dfs
dfs(val^nums[index], index + 1, nums);
dfs(val, index + 1, nums);
}
int subsetXORSum(vector<int>& nums) {
res = 0;
n = nums.size();
dfs(0, 0, nums);
return res;
}
};
边栏推荐
猜你喜欢

VSCode无网环境快速迁移开发环境(VIP典藏版)

Introduce the overall process of bootloader, PM, kernel and system startup

降低pip到指定版本(通过PyCharm升级pip,在降低到原来版本)

Transport layer UDP & TCP

SAP接口debug设置外部断点

ping: www.baidu. Com: unknown name or service
![[camera Foundation (I)] working principle and overall structure of camera](/img/5d/c29d636a90d01e5c3852df2a0dd833.png)
[camera Foundation (I)] working principle and overall structure of camera

为什么生命科学企业都在陆续上云?

Multi view function in blender

A deep learning model for urban traffic flow prediction with traffic events mined from twitter
随机推荐
传输层 udp && tcp
socket done
02--- impossible phenomenon of longitudinal wave
how to install clustershell
TDengine可通过数据同步工具 DataX读写
socket(2)
TypeScript快速入门
Volcano becomes spark default batch scheduler
How to achieve energy conservation and environmental protection of full-color outdoor LED display
leetcode_191_2021-10-15
一文理解OpenStack网络
Network layer & IP
That is to say, "live broadcast" is launched! One stop live broadcast service with full link upgrade
Li Kou daily question - day 26 -496 Next larger element I
Slider controls the playback progress of animator animation
suspense组件和异步组件
Several classes of manual transactions
【Camera基础(二)】摄像头驱动原理和开发&&V4L2子系统驱动架构
#国企央企结构化面试#国企就业#墨斗互动就业服务管家
socket(1)