返回介绍

solution / 2100-2199 / 2166.Design Bitset / README_EN

发布于 2024-06-17 01:03:09 字数 8108 浏览 0 评论 0 收藏 0

2166. Design Bitset

中文文档

Description

A Bitset is a data structure that compactly stores bits.

Implement the Bitset class:

  • Bitset(int size) Initializes the Bitset with size bits, all of which are 0.
  • void fix(int idx) Updates the value of the bit at the index idx to 1. If the value was already 1, no change occurs.
  • void unfix(int idx) Updates the value of the bit at the index idx to 0. If the value was already 0, no change occurs.
  • void flip() Flips the values of each bit in the Bitset. In other words, all bits with value 0 will now have value 1 and vice versa.
  • boolean all() Checks if the value of each bit in the Bitset is 1. Returns true if it satisfies the condition, false otherwise.
  • boolean one() Checks if there is at least one bit in the Bitset with value 1. Returns true if it satisfies the condition, false otherwise.
  • int count() Returns the total number of bits in the Bitset which have value 1.
  • String toString() Returns the current composition of the Bitset. Note that in the resultant string, the character at the ith index should coincide with the value at the ith bit of the Bitset.

 

Example 1:

Input
["Bitset", "fix", "fix", "flip", "all", "unfix", "flip", "one", "unfix", "count", "toString"]
[[5], [3], [1], [], [], [0], [], [], [0], [], []]
Output
[null, null, null, null, false, null, null, true, null, 2, "01010"]

Explanation
Bitset bs = new Bitset(5); // bitset = "00000".
bs.fix(3);   // the value at idx = 3 is updated to 1, so bitset = "00010".
bs.fix(1);   // the value at idx = 1 is updated to 1, so bitset = "01010". 
bs.flip();   // the value of each bit is flipped, so bitset = "10101". 
bs.all();    // return False, as not all values of the bitset are 1.
bs.unfix(0);   // the value at idx = 0 is updated to 0, so bitset = "00101".
bs.flip();   // the value of each bit is flipped, so bitset = "11010". 
bs.one();    // return True, as there is at least 1 index with value 1.
bs.unfix(0);   // the value at idx = 0 is updated to 0, so bitset = "01010".
bs.count();  // return 2, as there are 2 bits with value 1.
bs.toString(); // return "01010", which is the composition of bitset.

 

Constraints:

  • 1 <= size <= 105
  • 0 <= idx <= size - 1
  • At most 105 calls will be made in total to fix, unfix, flip, all, one, count, and toString.
  • At least one call will be made to all, one, count, or toString.
  • At most 5 calls will be made to toString.

Solutions

Solution 1

class Bitset:
  def __init__(self, size: int):
    self.a = ['0'] * size
    self.b = ['1'] * size
    self.cnt = 0

  def fix(self, idx: int) -> None:
    if self.a[idx] == '0':
      self.a[idx] = '1'
      self.cnt += 1
    self.b[idx] = '0'

  def unfix(self, idx: int) -> None:
    if self.a[idx] == '1':
      self.a[idx] = '0'
      self.cnt -= 1
    self.b[idx] = '1'

  def flip(self) -> None:
    self.a, self.b = self.b, self.a
    self.cnt = len(self.a) - self.cnt

  def all(self) -> bool:
    return self.cnt == len(self.a)

  def one(self) -> bool:
    return self.cnt > 0

  def count(self) -> int:
    return self.cnt

  def toString(self) -> str:
    return ''.join(self.a)


# Your Bitset object will be instantiated and called as such:
# obj = Bitset(size)
# obj.fix(idx)
# obj.unfix(idx)
# obj.flip()
# param_4 = obj.all()
# param_5 = obj.one()
# param_6 = obj.count()
# param_7 = obj.toString()
class Bitset {
  private char[] a;
  private char[] b;
  private int cnt;

  public Bitset(int size) {
    a = new char[size];
    b = new char[size];
    Arrays.fill(a, '0');
    Arrays.fill(b, '1');
  }

  public void fix(int idx) {
    if (a[idx] == '0') {
      a[idx] = '1';
      ++cnt;
    }
    b[idx] = '0';
  }

  public void unfix(int idx) {
    if (a[idx] == '1') {
      a[idx] = '0';
      --cnt;
    }
    b[idx] = '1';
  }

  public void flip() {
    char[] t = a;
    a = b;
    b = t;
    cnt = a.length - cnt;
  }

  public boolean all() {
    return cnt == a.length;
  }

  public boolean one() {
    return cnt > 0;
  }

  public int count() {
    return cnt;
  }

  public String toString() {
    return String.valueOf(a);
  }
}

/**
 * Your Bitset object will be instantiated and called as such:
 * Bitset obj = new Bitset(size);
 * obj.fix(idx);
 * obj.unfix(idx);
 * obj.flip();
 * boolean param_4 = obj.all();
 * boolean param_5 = obj.one();
 * int param_6 = obj.count();
 * String param_7 = obj.toString();
 */
class Bitset {
public:
  string a, b;
  int cnt = 0;

  Bitset(int size) {
    a = string(size, '0');
    b = string(size, '1');
  }

  void fix(int idx) {
    if (a[idx] == '0') a[idx] = '1', ++cnt;
    b[idx] = '0';
  }

  void unfix(int idx) {
    if (a[idx] == '1') a[idx] = '0', --cnt;
    b[idx] = '1';
  }

  void flip() {
    swap(a, b);
    cnt = a.size() - cnt;
  }

  bool all() {
    return cnt == a.size();
  }

  bool one() {
    return cnt > 0;
  }

  int count() {
    return cnt;
  }

  string toString() {
    return a;
  }
};

/**
 * Your Bitset object will be instantiated and called as such:
 * Bitset* obj = new Bitset(size);
 * obj->fix(idx);
 * obj->unfix(idx);
 * obj->flip();
 * bool param_4 = obj->all();
 * bool param_5 = obj->one();
 * int param_6 = obj->count();
 * string param_7 = obj->toString();
 */
type Bitset struct {
  a   []byte
  b   []byte
  cnt int
}

func Constructor(size int) Bitset {
  a := bytes.Repeat([]byte{'0'}, size)
  b := bytes.Repeat([]byte{'1'}, size)
  return Bitset{a, b, 0}
}

func (this *Bitset) Fix(idx int) {
  if this.a[idx] == '0' {
    this.a[idx] = '1'
    this.cnt++
  }
  this.b[idx] = '0'
}

func (this *Bitset) Unfix(idx int) {
  if this.a[idx] == '1' {
    this.a[idx] = '0'
    this.cnt--
  }
  this.b[idx] = '1'
}

func (this *Bitset) Flip() {
  this.a, this.b = this.b, this.a
  this.cnt = len(this.a) - this.cnt
}

func (this *Bitset) All() bool {
  return this.cnt == len(this.a)
}

func (this *Bitset) One() bool {
  return this.cnt > 0
}

func (this *Bitset) Count() int {
  return this.cnt
}

func (this *Bitset) ToString() string {
  return string(this.a)
}

/**
 * Your Bitset object will be instantiated and called as such:
 * obj := Constructor(size);
 * obj.Fix(idx);
 * obj.Unfix(idx);
 * obj.Flip();
 * param_4 := obj.All();
 * param_5 := obj.One();
 * param_6 := obj.Count();
 * param_7 := obj.ToString();
 */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文