当前位置: 首页 > news >正文

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++
/** @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"
#endifclass 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
'''
Author: LetMeFly
Date: 2025-04-30 17:24:34
LastEditors: LetMeFly.xyz
LastEditTime: 2025-04-30 17:26:24
'''
from typing import Listclass Solution:def findNumbers(self, nums: List[int]) -> int:return sum(len(str(t)) % 2 == 0 for t in nums)
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
/** @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 := 0for 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++
/** @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"
#endifclass 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和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源

http://www.xdnf.cn/news/229087.html

相关文章:

  • Arduino IDE中更新esp32 3.2.0版本的办法
  • 开源协议全解析:类型、选择与法律风险规避指南
  • Sigmoid函数简介及其Python实现
  • uv安装及使用
  • 在pycharm中创建Django项目并启动
  • TIME_WAIT状态+UDP概念及模拟实现服务器和客户端收发数据
  • 决策树在电信客户流失分析中的实战应用
  • 126. 单词接龙 II
  • 数学建模论文手的学习日常01
  • 牛客:AB5 点击消除
  • 【已解决】TensorRT安装好后加载不了或者转换不了engine模型,或者加载时报错
  • LeetCode392_判断子序列
  • 基于PHP的在线编程课程学习系统
  • [特殊字符] 开发工作高内存占用场景下,Windows 内存压缩机制是否应该启用?实测分析与优化建议
  • 涨薪技术|0到1学会性能测试第44课-apachetop模块监控
  • MCU片上存储器的类型与特性
  • 【学习 python day5】
  • 3.2goweb框架GORM
  • kotlin 过滤 filter 函数的作用和使用场景
  • MATLAB小试牛刀系列(3)
  • linux系统加固
  • 基于 Rancher 部署 Kubernetes 集群的工程实践指南
  • StarRocks Lakehouse 如何重构大数据架构?
  • 基于标注数据的情感分析模型研究
  • 使用 Spring Data Redis 实现 Redis 数据存储详解
  • 【数据结构】——顺序表刷题
  • 论文阅读:2024 EMNLP User Inference Attacks on Large Language Models
  • MySQL表的内外连接
  • 黑群晖Moments视频无缩略图,安装第三方ffmpeg解决
  • kivy android打包buildozer.spec GUI配置