|
| 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>给你一个整数数组 <code>nums</code>,请你返回其中位数为 <strong>偶数</strong> 的数字的个数。</p> |
| 13 | + |
| 14 | +<p> </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 位数字(位数为偶数) |
| 23 | +345 是 3 位数字(位数为奇数) |
| 24 | +2 是 1 位数字(位数为奇数) |
| 25 | +6 是 1 位数字 位数为奇数) |
| 26 | +7896 是 4 位数字(位数为偶数) |
| 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> </p> |
| 40 | + |
| 41 | +<p><strong>提示:</strong></p> |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li><code>1 <= nums.length <= 500</code></li> |
| 45 | + <li><code>1 <= nums[i] <= 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) |
0 commit comments