Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 53e0be3

Browse files
committedMar 16, 2025
Add solution #810
1 parent e72ff81 commit 53e0be3

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@
618618
806|[Number of Lines To Write String](./0806-number-of-lines-to-write-string.js)|Easy|
619619
808|[Soup Servings](./0808-soup-servings.js)|Medium|
620620
809|[Expressive Words](./0809-expressive-words.js)|Medium|
621+
810|[Chalkboard XOR Game](./0810-chalkboard-xor-game.js)|Hard|
621622
819|[Most Common Word](./0819-most-common-word.js)|Easy|
622623
821|[Shortest Distance to a Character](./0821-shortest-distance-to-a-character.js)|Easy|
623624
824|[Goat Latin](./0824-goat-latin.js)|Easy|

‎solutions/0810-chalkboard-xor-game.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 810. Chalkboard XOR Game
3+
* https://leetcode.com/problems/chalkboard-xor-game/
4+
* Difficulty: Hard
5+
*
6+
* You are given an array of integers nums represents the numbers written on a chalkboard.
7+
*
8+
* Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting
9+
* first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to
10+
* become 0, then that player loses. The bitwise XOR of one element is that element itself, and
11+
* the bitwise XOR of no elements is 0.
12+
*
13+
* Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard
14+
* equal to 0, then that player wins.
15+
*
16+
* Return true if and only if Alice wins the game, assuming both players play optimally.
17+
*/
18+
19+
/**
20+
* @param {number[]} nums
21+
* @return {boolean}
22+
*/
23+
var xorGame = function(nums) {
24+
const total = nums.reduce((xor, num) => xor ^ num, 0);
25+
if (total === 0) return true;
26+
return nums.length % 2 === 0;
27+
};

0 commit comments

Comments
 (0)
Please sign in to comment.