Counter Class

Easy~10 min

Implement a Counter class that tracks an integer count.

The Counter class should support the following operations:

  • Counter() — Initialize the counter with a count of 0.
  • increment() — Increase the count by 1.
  • decrement() — Decrease the count by 1.
  • getCount() — Return the current count.
  • reset() — Reset the count to 0.

Examples

Example 1
Input: ["Counter","increment","increment","increment","getCount","decrement","getCount","reset","getCount"] [[],[],[],[],[],[],[],[],[]]
Output: [null,null,null,null,3,null,2,null,0]
Explanation: Counter counter = new Counter(); counter.increment(); // count = 1 counter.increment(); // count = 2 counter.increment(); // count = 3 counter.getCount(); // return 3 counter.decrement(); // count = 2 counter.getCount(); // return 2 counter.reset(); // count = 0 counter.getCount(); // return 0

Constraints

  • At most 1000 calls will be made to increment, decrement, getCount, and reset
  • The count can be negative (decrementing below 0 is allowed)
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run