feat: add solutions to lc problem: No.3581 (#4501)

No.3581.Count Odd Letters from Number
This commit is contained in:
Libin YANG 2025-06-16 14:47:58 +08:00 committed by GitHub
parent 43a2a84ae2
commit 589c8dbb8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 417 additions and 2 deletions

View File

@ -65,32 +65,172 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3581.Co
<!-- solution:start -->
### 方法一
### 方法一:模拟 + 位运算
我们可以将每个数字转换为对应的英文单词,然后统计每个字母出现的次数。由于字母的数量有限,我们可以使用一个整数 $\textit{mask}$ 来表示每个字母的出现情况。具体地,我们可以将字母映射到整数的二进制位上,如果某个字母出现了奇数次,则对应的二进制位为 1否则为 0。最后我们只需要统计 $\textit{mask}$ 中为 1 的位数,即为答案。
时间复杂度 $O(\log n)$,其中 $n$ 是输入的整数。空间复杂度 $O(1)$。
<!-- tabs:start -->
#### Python3
```python
d = {
0: "zero",
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
}
class Solution:
def countOddLetters(self, n: int) -> int:
mask = 0
while n:
x = n % 10
n //= 10
for c in d[x]:
mask ^= 1 << (ord(c) - ord("a"))
return mask.bit_count()
```
#### Java
```java
class Solution {
private static final Map<Integer, String> d = new HashMap<>();
static {
d.put(0, "zero");
d.put(1, "one");
d.put(2, "two");
d.put(3, "three");
d.put(4, "four");
d.put(5, "five");
d.put(6, "six");
d.put(7, "seven");
d.put(8, "eight");
d.put(9, "nine");
}
public int countOddLetters(int n) {
int mask = 0;
while (n > 0) {
int x = n % 10;
n /= 10;
for (char c : d.get(x).toCharArray()) {
mask ^= 1 << (c - 'a');
}
}
return Integer.bitCount(mask);
}
}
```
#### C++
```cpp
class Solution {
public:
int countOddLetters(int n) {
static const unordered_map<int, string> d = {
{0, "zero"},
{1, "one"},
{2, "two"},
{3, "three"},
{4, "four"},
{5, "five"},
{6, "six"},
{7, "seven"},
{8, "eight"},
{9, "nine"}};
int mask = 0;
while (n > 0) {
int x = n % 10;
n /= 10;
for (char c : d.at(x)) {
mask ^= 1 << (c - 'a');
}
}
return __builtin_popcount(mask);
}
};
```
#### Go
```go
func countOddLetters(n int) int {
d := map[int]string{
0: "zero",
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
}
mask := 0
for n > 0 {
x := n % 10
n /= 10
for _, c := range d[x] {
mask ^= 1 << (c - 'a')
}
}
return bits.OnesCount32(uint32(mask))
}
```
#### TypeScript
```ts
function countOddLetters(n: number): number {
const d: Record<number, string> = {
0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
};
let mask = 0;
while (n > 0) {
const x = n % 10;
n = Math.floor(n / 10);
for (const c of d[x]) {
mask ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
}
}
return bitCount(mask);
}
function bitCount(i: number): number {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
```
<!-- tabs:end -->

View File

@ -65,32 +65,172 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3581.Co
<!-- solution:start -->
### Solution 1
### Solution 1: Simulation + Bit Manipulation
We can convert each number into its corresponding English word, then count the frequency of each letter. Since the number of letters is limited, we can use an integer $\textit{mask}$ to represent the occurrence of each letter. Specifically, we can map each letter to a binary bit of the integer. If a letter appears an odd number of times, the corresponding binary bit is 1; otherwise, it's 0. Finally, we only need to count the number of bits that are 1 in $\textit{mask}$, which is the answer.
The time complexity is $O(\log n)$, where $n$ is the input integer. And the space complexity is $O(1)$.
<!-- tabs:start -->
#### Python3
```python
d = {
0: "zero",
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
}
class Solution:
def countOddLetters(self, n: int) -> int:
mask = 0
while n:
x = n % 10
n //= 10
for c in d[x]:
mask ^= 1 << (ord(c) - ord("a"))
return mask.bit_count()
```
#### Java
```java
class Solution {
private static final Map<Integer, String> d = new HashMap<>();
static {
d.put(0, "zero");
d.put(1, "one");
d.put(2, "two");
d.put(3, "three");
d.put(4, "four");
d.put(5, "five");
d.put(6, "six");
d.put(7, "seven");
d.put(8, "eight");
d.put(9, "nine");
}
public int countOddLetters(int n) {
int mask = 0;
while (n > 0) {
int x = n % 10;
n /= 10;
for (char c : d.get(x).toCharArray()) {
mask ^= 1 << (c - 'a');
}
}
return Integer.bitCount(mask);
}
}
```
#### C++
```cpp
class Solution {
public:
int countOddLetters(int n) {
static const unordered_map<int, string> d = {
{0, "zero"},
{1, "one"},
{2, "two"},
{3, "three"},
{4, "four"},
{5, "five"},
{6, "six"},
{7, "seven"},
{8, "eight"},
{9, "nine"}};
int mask = 0;
while (n > 0) {
int x = n % 10;
n /= 10;
for (char c : d.at(x)) {
mask ^= 1 << (c - 'a');
}
}
return __builtin_popcount(mask);
}
};
```
#### Go
```go
func countOddLetters(n int) int {
d := map[int]string{
0: "zero",
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
}
mask := 0
for n > 0 {
x := n % 10
n /= 10
for _, c := range d[x] {
mask ^= 1 << (c - 'a')
}
}
return bits.OnesCount32(uint32(mask))
}
```
#### TypeScript
```ts
function countOddLetters(n: number): number {
const d: Record<number, string> = {
0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
};
let mask = 0;
while (n > 0) {
const x = n % 10;
n = Math.floor(n / 10);
for (const c of d[x]) {
mask ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
}
}
return bitCount(mask);
}
function bitCount(i: number): number {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
```
<!-- tabs:end -->

View File

@ -0,0 +1,26 @@
class Solution {
public:
int countOddLetters(int n) {
static const unordered_map<int, string> d = {
{0, "zero"},
{1, "one"},
{2, "two"},
{3, "three"},
{4, "four"},
{5, "five"},
{6, "six"},
{7, "seven"},
{8, "eight"},
{9, "nine"}};
int mask = 0;
while (n > 0) {
int x = n % 10;
n /= 10;
for (char c : d.at(x)) {
mask ^= 1 << (c - 'a');
}
}
return __builtin_popcount(mask);
}
};

View File

@ -0,0 +1,25 @@
func countOddLetters(n int) int {
d := map[int]string{
0: "zero",
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
}
mask := 0
for n > 0 {
x := n % 10
n /= 10
for _, c := range d[x] {
mask ^= 1 << (c - 'a')
}
}
return bits.OnesCount32(uint32(mask))
}

View File

@ -0,0 +1,27 @@
class Solution {
private static final Map<Integer, String> d = new HashMap<>();
static {
d.put(0, "zero");
d.put(1, "one");
d.put(2, "two");
d.put(3, "three");
d.put(4, "four");
d.put(5, "five");
d.put(6, "six");
d.put(7, "seven");
d.put(8, "eight");
d.put(9, "nine");
}
public int countOddLetters(int n) {
int mask = 0;
while (n > 0) {
int x = n % 10;
n /= 10;
for (char c : d.get(x).toCharArray()) {
mask ^= 1 << (c - 'a');
}
}
return Integer.bitCount(mask);
}
}

View File

@ -0,0 +1,23 @@
d = {
0: "zero",
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
}
class Solution:
def countOddLetters(self, n: int) -> int:
mask = 0
while n:
x = n % 10
n //= 10
for c in d[x]:
mask ^= 1 << (ord(c) - ord("a"))
return mask.bit_count()

View File

@ -0,0 +1,34 @@
function countOddLetters(n: number): number {
const d: Record<number, string> = {
0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
};
let mask = 0;
while (n > 0) {
const x = n % 10;
n = Math.floor(n / 10);
for (const c of d[x]) {
mask ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
}
}
return bitCount(mask);
}
function bitCount(i: number): number {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}