Skip to content

Commit 0b5a1ed

Browse files
committed
update: 添加问题“1295.统计位数为偶数的数字”的代码和题解(#907)
Signed-off-by: LetMeFly666 <[email protected]>
1 parent 45bc686 commit 0b5a1ed

8 files changed

+321
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-30 17:23:40
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-30 17:25:14
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
inline int getLength(int t) {
14+
int ans = 0;
15+
while (t) {
16+
ans++;
17+
t /= 10;
18+
}
19+
return ans;
20+
}
21+
public:
22+
int findNumbers(vector<int>& nums) {
23+
int ans = 0;
24+
for (int t : nums) {
25+
ans += getLength(t) % 2 == 0;
26+
}
27+
return ans;
28+
}
29+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-30 17:24:40
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-30 17:28:26
6+
*/
7+
func findNumbers(nums []int) (ans int) {
8+
for _, t := range nums {
9+
cnt := 0
10+
for t > 0 {
11+
cnt++
12+
t /= 10
13+
}
14+
if cnt % 2 == 0 {
15+
ans++
16+
}
17+
}
18+
return
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-30 17:24:37
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-30 17:27:07
6+
*/
7+
class Solution {
8+
private int getLength(int t) {
9+
int ans = 0;
10+
while (t > 0) {
11+
ans++;
12+
t /= 10;
13+
}
14+
return ans;
15+
}
16+
public int findNumbers(int[] nums) {
17+
int ans = 0;
18+
for (int t : nums) {
19+
if (getLength(t) % 2 == 0) {
20+
ans++;
21+
}
22+
}
23+
return ans;
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-30 17:24:34
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-30 17:26:24
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def findNumbers(self, nums: List[int]) -> int:
11+
return sum(len(str(t)) % 2 == 0 for t in nums)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-30 17:30:12
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-30 17:30:19
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int findNumbers(vector<int>& nums) {
14+
int ans = 0;
15+
for (int t : nums) {
16+
while (t >= 10) {
17+
t /= 100;
18+
}
19+
ans += t == 0;
20+
}
21+
return ans;
22+
}
23+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@
503503
|1282.用户分组|中等|<a href="https://leetcode.cn/problems/group-the-people-given-the-group-size-they-belong-to/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/12/LeetCode%201282.%E7%94%A8%E6%88%B7%E5%88%86%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126298148" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/group-the-people-given-the-group-size-they-belong-to/solution/letmefly-1282yong-hu-fen-zu-by-tisfy-9h94/" target="_blank">LeetCode题解</a>|
504504
|1287.有序数组中出现次数超过25%的元素|简单|<a href="https://leetcode.cn/problems/element-appearing-more-than-25-in-sorted-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/02/17/LeetCode%201287.%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E5%87%BA%E7%8E%B0%E6%AC%A1%E6%95%B0%E8%B6%85%E8%BF%8725%E7%9A%84%E5%85%83%E7%B4%A0" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145683090" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/element-appearing-more-than-25-in-sorted-array/solutions/3078136/letmefly-1287you-xu-shu-zu-zhong-chu-xia-wdat/" target="_blank">LeetCode题解</a>|
505505
|1289.下降路径最小和II|困难|<a href="https://leetcode.cn/problems/minimum-falling-path-sum-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/08/10/LeetCode%201289.%E4%B8%8B%E9%99%8D%E8%B7%AF%E5%BE%84%E6%9C%80%E5%B0%8F%E5%92%8CII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132201281" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-falling-path-sum-ii/solutions/2381173/letmefly-1289xia-jiang-lu-jing-zui-xiao-kt8cj/" target="_blank">LeetCode题解</a>|
506+
|1295.统计位数为偶数的数字|简单|<a href="https://leetcode.cn/problems/find-numbers-with-even-number-of-digits/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/30/LeetCode%201295.%E7%BB%9F%E8%AE%A1%E4%BD%8D%E6%95%B0%E4%B8%BA%E5%81%B6%E6%95%B0%E7%9A%84%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147637587" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-numbers-with-even-number-of-digits/solutions/3666390/letmefly-1295tong-ji-wei-shu-wei-ou-shu-bwhfr/" target="_blank">LeetCode题解</a>|
506507
|1299.将每个元素替换为右侧最大元素|简单|<a href="https://leetcode.cn/problems/replace-elements-with-greatest-element-on-right-side/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/02/16/LeetCode%201299.%E5%B0%86%E6%AF%8F%E4%B8%AA%E5%85%83%E7%B4%A0%E6%9B%BF%E6%8D%A2%E4%B8%BA%E5%8F%B3%E4%BE%A7%E6%9C%80%E5%A4%A7%E5%85%83%E7%B4%A0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145661909" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/replace-elements-with-greatest-element-on-right-side/solutions/3076497/letmefly-1299jiang-mei-ge-yuan-su-ti-hua-4y58/" target="_blank">LeetCode题解</a>|
507508
|1302.层数最深叶子节点的和|中等|<a href="https://leetcode.cn/problems/deepest-leaves-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/17/LeetCode%201302.%E5%B1%82%E6%95%B0%E6%9C%80%E6%B7%B1%E5%8F%B6%E5%AD%90%E8%8A%82%E7%82%B9%E7%9A%84%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126377912" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/deepest-leaves-sum/solution/letmefly-1302ceng-shu-zui-shen-xie-zi-ji-eyu0/" target="_blank">LeetCode题解</a>|
508509
|1328.破坏回文串|中等|<a href="https://leetcode.cn/problems/break-a-palindrome/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/05/LeetCode%201328.%E7%A0%B4%E5%9D%8F%E5%9B%9E%E6%96%87%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146046025" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/break-a-palindrome/solutions/3597648/letmefly-1328po-pi-hui-wen-chuan-tan-xin-ktbc/" target="_blank">LeetCode题解</a>|
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
---
2+
title: 1295.统计位数为偶数的数字:模拟
3+
date: 2025-04-30 17:33:51
4+
tags: [题解, LeetCode, 简单, 数组, 数学]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】1295.统计位数为偶数的数字:模拟
9+
10+
力扣题目链接:[https://leetcode.cn/problems/find-numbers-with-even-number-of-digits/](https://leetcode.cn/problems/find-numbers-with-even-number-of-digits/)
11+
12+
<p>给你一个整数数组&nbsp;<code>nums</code>,请你返回其中位数为&nbsp;<strong>偶数</strong>&nbsp;的数字的个数。</p>
13+
14+
<p>&nbsp;</p>
15+
16+
<p><strong>示例 1:</strong></p>
17+
18+
<pre>
19+
<strong>输入:</strong>nums = [12,345,2,6,7896]
20+
<strong>输出:</strong>2
21+
<strong>解释:
22+
</strong>12 是 2 位数字(位数为偶数)&nbsp;
23+
345 是 3 位数字(位数为奇数)&nbsp;&nbsp;
24+
2 是 1 位数字(位数为奇数)&nbsp;
25+
6 是 1 位数字 位数为奇数)&nbsp;
26+
7896 是 4 位数字(位数为偶数)&nbsp;&nbsp;
27+
因此只有 12 和 7896 是位数为偶数的数字
28+
</pre>
29+
30+
<p><strong>示例 2:</strong></p>
31+
32+
<pre>
33+
<strong>输入:</strong>nums = [555,901,482,1771]
34+
<strong>输出:</strong>1
35+
<strong>解释: </strong>
36+
只有 1771 是位数为偶数的数字。
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= nums.length &lt;= 500</code></li>
45+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
46+
</ul>
47+
48+
49+
50+
## 解题方法一:正常模拟
51+
52+
如何判断一个(非负)数在十进制下有多少位?
53+
54+
> 当这个数不为0时拿这个数不断除以10,并将“数字位数”加一。
55+
56+
依次计算每个元素的位数,判断是否是偶数。
57+
58+
### 时空复杂度分析
59+
60+
+ 时间复杂度$O(len(nums)\times \log nums[i])$
61+
+ 空间复杂度$O(1)$
62+
63+
### AC代码
64+
65+
#### C++
66+
67+
```cpp
68+
/*
69+
* @Author: LetMeFly
70+
* @Date: 2025-04-30 17:23:40
71+
* @LastEditors: LetMeFly.xyz
72+
* @LastEditTime: 2025-04-30 17:25:14
73+
*/
74+
#if defined(_WIN32) || defined(__APPLE__)
75+
#include "_[1,2]toVector.h"
76+
#endif
77+
78+
class Solution {
79+
private:
80+
inline int getLength(int t) {
81+
int ans = 0;
82+
while (t) {
83+
ans++;
84+
t /= 10;
85+
}
86+
return ans;
87+
}
88+
public:
89+
int findNumbers(vector<int>& nums) {
90+
int ans = 0;
91+
for (int t : nums) {
92+
ans += getLength(t) % 2 == 0;
93+
}
94+
return ans;
95+
}
96+
};
97+
```
98+
99+
100+
#### Python
101+
102+
```python
103+
'''
104+
Author: LetMeFly
105+
Date: 2025-04-30 17:24:34
106+
LastEditors: LetMeFly.xyz
107+
LastEditTime: 2025-04-30 17:26:24
108+
'''
109+
from typing import List
110+
111+
class Solution:
112+
def findNumbers(self, nums: List[int]) -> int:
113+
return sum(len(str(t)) % 2 == 0 for t in nums)
114+
```
115+
116+
#### Java
117+
118+
```java
119+
/*
120+
* @Author: LetMeFly
121+
* @Date: 2025-04-30 17:24:37
122+
* @LastEditors: LetMeFly.xyz
123+
* @LastEditTime: 2025-04-30 17:27:07
124+
*/
125+
class Solution {
126+
private int getLength(int t) {
127+
int ans = 0;
128+
while (t > 0) {
129+
ans++;
130+
t /= 10;
131+
}
132+
return ans;
133+
}
134+
public int findNumbers(int[] nums) {
135+
int ans = 0;
136+
for (int t : nums) {
137+
if (getLength(t) % 2 == 0) {
138+
ans++;
139+
}
140+
}
141+
return ans;
142+
}
143+
}
144+
```
145+
146+
#### Go
147+
148+
```go
149+
/*
150+
* @Author: LetMeFly
151+
* @Date: 2025-04-30 17:24:40
152+
* @LastEditors: LetMeFly.xyz
153+
* @LastEditTime: 2025-04-30 17:28:26
154+
*/
155+
func findNumbers(nums []int) (ans int) {
156+
for _, t := range nums {
157+
cnt := 0
158+
for t > 0 {
159+
cnt++
160+
t /= 10
161+
}
162+
if cnt % 2 == 0 {
163+
ans++
164+
}
165+
}
166+
return
167+
}
168+
```
169+
170+
## 解题方法二:一次移除两位
171+
172+
方法一中我们将元素一次除以10(移除元素的一位),但是问题求的是“元素位数是否为偶数”,那么我们为什么不可以在元素位数大于等于2的时候,一次移除两位呢?最后看元素剩下一位还是零位不就知道元素十进制下的位数是奇数还是偶数了吗。
173+
174+
### 时空复杂度分析
175+
176+
+ 时间复杂度$O(len(nums)\times \log nums[i])$
177+
+ 空间复杂度$O(1)$
178+
179+
#### C++
180+
181+
```cpp
182+
/*
183+
* @Author: LetMeFly
184+
* @Date: 2025-04-30 17:30:12
185+
* @LastEditors: LetMeFly.xyz
186+
* @LastEditTime: 2025-04-30 17:30:19
187+
*/
188+
#if defined(_WIN32) || defined(__APPLE__)
189+
#include "_[1,2]toVector.h"
190+
#endif
191+
192+
class Solution {
193+
public:
194+
int findNumbers(vector<int>& nums) {
195+
int ans = 0;
196+
for (int t : nums) {
197+
while (t >= 10) {
198+
t /= 100;
199+
}
200+
ans += t == 0;
201+
}
202+
return ans;
203+
}
204+
};
205+
```
206+
207+
208+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147637587)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/04/30/LeetCode%201295.%E7%BB%9F%E8%AE%A1%E4%BD%8D%E6%95%B0%E4%B8%BA%E5%81%B6%E6%95%B0%E7%9A%84%E6%95%B0%E5%AD%97/)哦~
209+
>
210+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ categories: [自用]
10991099
|||
11001100
|wordy|adj. 话多的,冗长的,啰嗦的|
11011101
|oceanography|n. 海洋学|
1102-
|stronghold|n. 要塞,据点,堡垒,有广泛支持的地方|
1102+
|<font color="#28bea0" title="二次复习">stronghold</font>|n. 要塞,据点,堡垒,有广泛支持的地方|
11031103
|mackintosh|n. 防水胶布,胶布雨衣(麦金托什)|
11041104
|||
11051105
|ferrous|adj. 含铁的,铁的|
@@ -1123,6 +1123,8 @@ categories: [自用]
11231123
|lining|n. 内衬,衬层,衬里|
11241124
|||
11251125
|cape|n. 岬,海角;披肩,披风,短斗篷|
1126+
|||
1127+
|nought|n. 无,零|
11261128

11271129
<p class="wordCounts">单词收录总数</p>
11281130

0 commit comments

Comments
 (0)