当前位置:网站首页>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;
}
};
边栏推荐
- leetcode_191_2021-10-15
- leetcode:1504. 统计全 1 子矩形的个数
- Unity about conversion between local and world coordinates
- Multiplexer select
- [camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture
- 02---纵波不可能产生的现象
- [精选] 多账号统一登录,你如何设计?
- leetcode_1365
- 01---两列波在相遇处发生干涉的条件
- socket(1)
猜你喜欢

CondaValueError: The target prefix is the base prefix. Aborting.

Volcano成Spark默认batch调度器

A deep learning model for urban traffic flow prediction with traffic events mined from twitter

cv2导包时报Could not find a version that satisfies the requirement cv2 (from versions: none)

【Camera基础(一)】Camera摄像头工作原理及整机架构

网络层 && IP

面试官:你说你精通Redis,你看过持久化的配置吗?

Memcached comprehensive analysis – 3 Deletion mechanism and development direction of memcached

Why are life science enterprises on the cloud in succession?

SAP接口debug设置外部断点
随机推荐
Vscode netless environment rapid migration development environment (VIP collection version)
心楼:华为运动健康的七年筑造之旅
MySQL optimizes query speed
AntDB数据库在线培训开课啦!更灵活、更专业、更丰富
[notes of wuenda] fundamentals of machine learning
VSCode无网环境快速迁移开发环境(VIP典藏版)
如何做到全彩户外LED显示屏节能环保
Suspend component and asynchronous component
leetcode_1470_2021.10.12
Jianmu continuous integration platform v2.5.0 release
socket(1)
XTransfer技术新人进阶秘诀:不可错过的宝藏Mentor
Blender FAQs
【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
[featured] how do you design unified login with multiple accounts?
(to be added) games101 job 7 improvement - knowledge you need to know to realize micro surface model
TKKC round#3
2022 international women engineers' Day: Dyson design award shows women's design strength
EasyBypass
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)