当前位置:网站首页>2166. Design bit set
2166. Design bit set
2022-07-03 20:02:00 【_ Cauchy】
subject
BitSet Bitset It is a data structure that can store bits in a compact form .
Please realize Bitset class .Bitset(int size) use size Bit initialization Bitset , All the seats are 0 .
void fix(int idx) Mark the subscript as idx The value on the bit of is updated to 1 . If the value is already 1 , Nothing will change .
void unfix(int idx) Mark the subscript as idx The value on the bit of is updated to 0 . If the value is already 0 , Nothing will change .
void flip() Flip Bitset The value on each bit in . let me put it another way , All values are 0 The bit will become 1 , vice versa .
boolean all() Check Bitset in every Whether the values of are all 1 . If this condition is met , return true ; otherwise , return false .
boolean one() Check Bitset in whether At least one The value of is 1 . If this condition is met , return true ; otherwise , return false .
int count() return Bitset The median is 1 A bit of total .
String toString() return Bitset The current composition of . Be careful , In the result string , The first i Characters at subscripts should be the same as Bitset No i It's the same .
Code
class Bitset {
public:
int n; // Total length
int sum; // 1 The number of
bool fp; // reverse
vector<bool> st;
Bitset(int size) {
st.resize(size);
n = size;
fp = false;
sum = 0;
}
void fix(int idx) {
// idx Set up 1
// fp==false: st[idx]==false, sum++
// fp==true: st[idx]==true, sum++
if(!(st[idx] ^ fp)) sum++, st[idx] = !st[idx];
}
void unfix(int idx) {
// idx Set up 0
if(st[idx] ^ fp) sum--, st[idx] = !st[idx];
}
void flip() {
fp = !fp;
sum = n - sum;
}
bool all() {
return sum == n;
}
bool one() {
return sum >= 1;
}
int count() {
return sum;
}
string toString() {
string res = "";
for (int i = 0; i < n; i++)
if(st[i] ^ fp) res += '1';
else res += '0';
return res;
}
};
边栏推荐
- Nacos usage of micro services
- Implementation of stack
- Global and Chinese markets for medical temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
- About callback function and hook function
- 2022-06-30 网工进阶(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
- PR 2021 quick start tutorial, how to create a new sequence and set parameters?
- Chapter 1: King Shehan miscalculated
- WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
- PR FAQ: how to set PR vertical screen sequence?
- How to improve data security by renting servers in Hong Kong
猜你喜欢
Detailed and not wordy. Share the win10 tutorial of computer reinstallation system
NFT without IPFs and completely on the chain?
Chapter 1: extend the same code decimal sum s (D, n)
BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
Chapter 1: find all factorial sums, Grand Prix site unified programming, three factorial sums, graphic point scanning, recursive factorial n of n!, Find the factorial n of n!, King Shehan miscalculate
2.7 format output of values
Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
Xctf attack and defense world crypto master advanced area olddriver
Chapter 20: y= sin (x) /x, rambling coordinate system calculation, y= sin (x) /x with profile graphics, Olympic rings, ball rolling and bouncing, water display, rectangular optimization cutting, R que
Xctf attack and defense world crypto advanced area best_ rsa
随机推荐
PR 2021 quick start tutorial, how to create a new sequence and set parameters?
2.5 conversion of different data types (2)
Day10 ---- 强制登录, token刷新与jwt禁用
Typora charges, WTF? Still need support
Realize user registration and login
2022 Xinjiang latest road transportation safety officer simulation examination questions and answers
Global and Chinese market of electrolyte analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Native table - scroll - merge function
The simplicity of laravel
2022-06-27 网工进阶(十二)IS-IS-开销类型、开销计算、LSP的处理机制、路由撤销、路由渗透
MPLS configuration
Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
Global and Chinese market of full authority digital engine control (FADEC) 2022-2028: Research Report on technology, participants, trends, market size and share
Derivation of decision tree theory
kubernetes集群搭建efk日志收集平台
2022-06-28 网工进阶(十三)IS-IS-路由过滤、路由汇总、认证、影响ISIS邻居关系建立的因素、其他命令和特性
4. Data splitting of Flink real-time project
Make a simple text logo with DW
Leetcode daily question solution: 540 A single element in an ordered array
unittest框架基本使用