当前位置:网站首页>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;
}
};
边栏推荐
- 2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
- Native table - scroll - merge function
- Chapter 1: find the factorial n of n!
- 1.5 learn to find mistakes first
- Micro service knowledge sorting - asynchronous communication technology
- Virtual machine installation deepin system
- Chapter 1: recursively find the factorial n of n!
- PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
- unittest框架基本使用
- Chapter 1: find the algebraic sum of odd factors, find the same decimal sum s (D, n), simplify the same code decimal sum s (D, n), expand the same code decimal sum s (D, n)
猜你喜欢
Professional interpretation | how to become an SQL developer
[effective Objective-C] - block and grand central distribution
Chapter 1: King Shehan miscalculated
原生表格-滚动-合并功能
Upgrade PIP and install Libraries
Explore the internal mechanism of modern browsers (I) (original translation)
Xctf attack and defense world crypto master advanced area olddriver
04 -- QT OpenGL two sets of shaders draw two triangles
第一章:求奇因数代数和,求同吗小数和s(d, n),简化同码小数和s(d, n),拓广同码小数和s(d, n)
Chapter 1: simplify the same code decimal sum s (D, n)
随机推荐
第一章:求奇因数代数和,求同吗小数和s(d, n),简化同码小数和s(d, n),拓广同码小数和s(d, n)
IP address is such an important knowledge that it's useless to listen to a younger student?
Rad+xray vulnerability scanning tool
MPLS configuration
PR FAQ: how to set PR vertical screen sequence?
kubernetes集群搭建efk日志收集平台
Global and Chinese market of two in one notebook computers 2022-2028: Research Report on technology, participants, trends, market size and share
2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]
February 14-20, 2022 (osgear source code debugging +ue4 video +ogremain source code transcription)
Strict data sheet of new features of SQLite 3.37.0
Global and Chinese market of electrolyte analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
2022-06-28 advanced network engineering (XIII) IS-IS route filtering, route summary, authentication, factors affecting the establishment of Isis neighbor relations, other commands and characteristics
Class loading process
Day11 - my page, user information acquisition, modification and channel interface
How to improve data security by renting servers in Hong Kong
unittest框架基本使用
Nacos usage of micro services
Implementation of stack
2.6 formula calculation
Day10 -- forced login, token refresh and JWT disable