LeetCode 1295.统计位数为偶数的数字:模拟

【LetMeFly】1295.统计位数为偶数的数字:模拟

力扣题目链接:https://leetcode.cn/problems/find-numbers-with-even-number-of-digits/

给你一个整数数组 nums,请你返回其中位数为 偶数 的数字的个数。

示例 1:

复制代码
输入:nums = [12,345,2,6,7896]
输出:2
解释:
12 是 2 位数字(位数为偶数) 
345 是 3 位数字(位数为奇数)  
2 是 1 位数字(位数为奇数) 
6 是 1 位数字 位数为奇数) 
7896 是 4 位数字(位数为偶数)  
因此只有 12 和 7896 是位数为偶数的数字

示例 2:

复制代码
输入:nums = [555,901,482,1771]
输出:1 
解释: 
只有 1771 是位数为偶数的数字。

提示:

  • 1 <= nums.length <= 500
  • 1 <= nums[i] <= 105

解题方法一:正常模拟

如何判断一个(非负)数在十进制下有多少位?

当这个数不为0时拿这个数不断除以10,并将"数字位数"加一。

依次计算每个元素的位数,判断是否是偶数。

时空复杂度分析

  • 时间复杂度 O ( l e n ( n u m s ) × log ⁡ n u m s [ i ] ) O(len(nums)\times \log nums[i]) O(len(nums)×lognums[i])
  • 空间复杂度 O ( 1 ) O(1) O(1)

AC代码

C++
cpp 复制代码
/*
 * @Author: LetMeFly
 * @Date: 2025-04-30 17:23:40
 * @LastEditors: LetMeFly.xyz
 * @LastEditTime: 2025-04-30 17:25:14
 */
#if defined(_WIN32) || defined(__APPLE__)
#include "_[1,2]toVector.h"
#endif

class Solution {
private:
    inline int getLength(int t) {
        int ans = 0;
        while (t) {
            ans++;
            t /= 10;
        }
        return ans;
    }
public:
    int findNumbers(vector<int>& nums) {
        int ans = 0;
        for (int t : nums) {
            ans += getLength(t) % 2 == 0;
        }
        return ans;
    }
};
Python
python 复制代码
'''
Author: LetMeFly
Date: 2025-04-30 17:24:34
LastEditors: LetMeFly.xyz
LastEditTime: 2025-04-30 17:26:24
'''
from typing import List

class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        return sum(len(str(t)) % 2 == 0 for t in nums)
Java
java 复制代码
/*
 * @Author: LetMeFly
 * @Date: 2025-04-30 17:24:37
 * @LastEditors: LetMeFly.xyz
 * @LastEditTime: 2025-04-30 17:27:07
 */
class Solution {
    private int getLength(int t) {
        int ans = 0;
        while (t > 0) {
            ans++;
            t /= 10;
        }
        return ans;
    }
    public int findNumbers(int[] nums) {
        int ans = 0;
        for (int t : nums) {
            if (getLength(t) % 2 == 0) {
                ans++;
            }
        }
        return ans;
    }
}
Go
go 复制代码
/*
 * @Author: LetMeFly
 * @Date: 2025-04-30 17:24:40
 * @LastEditors: LetMeFly.xyz
 * @LastEditTime: 2025-04-30 17:28:26
 */
func findNumbers(nums []int) (ans int) {
    for _, t := range nums {
		cnt := 0
		for t > 0 {
			cnt++
			t /= 10
		}
		if cnt % 2 == 0 {
			ans++
		}
	}
	return
}

解题方法二:一次移除两位

方法一中我们将元素一次除以10(移除元素的一位),但是问题求的是"元素位数是否为偶数",那么我们为什么不可以在元素位数大于等于2的时候,一次移除两位呢?最后看元素剩下一位还是零位不就知道元素十进制下的位数是奇数还是偶数了吗。

时空复杂度分析

  • 时间复杂度 O ( l e n ( n u m s ) × log ⁡ n u m s [ i ] ) O(len(nums)\times \log nums[i]) O(len(nums)×lognums[i])
  • 空间复杂度 O ( 1 ) O(1) O(1)
C++
cpp 复制代码
/*
 * @Author: LetMeFly
 * @Date: 2025-04-30 17:30:12
 * @LastEditors: LetMeFly.xyz
 * @LastEditTime: 2025-04-30 17:30:19
 */
#if defined(_WIN32) || defined(__APPLE__)
#include "_[1,2]toVector.h"
#endif

class Solution {
public:
    int findNumbers(vector<int>& nums) {
        int ans = 0;
        for (int t : nums) {
            while (t >= 10) {
                t /= 100;
            }
            ans += t == 0;
        }
        return ans;
    }
};

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源

相关推荐
小灰灰的FPGA2 小时前
9.9元奶茶项目:matlab+FPGA的cordic算法(向量模式)计算相位角
算法·matlab·fpga开发
2401_841495642 小时前
【数据结构】顺序表的基本操作
数据结构·c++·算法·顺序表·线性表·线性结构·顺序表的基本操作
自信的小螺丝钉3 小时前
Leetcode 138. 随机链表的复制 哈希 / 拼接+拆分
leetcode·链表·哈希算法
元亓亓亓3 小时前
LeetCode热题--207. 课程表--中等
算法·leetcode·职场和发展
坚持编程的菜鸟3 小时前
LeetCode每日一题——有效的字母异位词
c语言·算法·leetcode
未知陨落3 小时前
LeetCode:70.最小栈
数据结构·算法·leetcode
jikiecui3 小时前
信奥崔老师:常用编译命令g++的基本使用
算法
乌萨奇也要立志学C++4 小时前
【洛谷】二叉树专题全解析:概念、存储、遍历与经典真题实战
数据结构·c++·算法
小谢在学习4 小时前
旋转图像
算法
北京地铁1号线5 小时前
机器学习笔试选择题:题组2
人工智能·算法·机器学习