LeetCode 1295. 统计位数为偶数的数字

避开ns-3学习深坑:用sns3模块快速搭建GEO卫星通信仿真(附GitHub代码解读) 本文介绍了如何使用sns3模块快速搭建GEO卫星通信仿真环境,绕过复杂的ns-3底层细节。通过详细的Linux环境配置、sns3模块安装和示例代码解读,帮助开发者快速上手卫星通信仿真,并提供了常见问题的解决方案和性能优化技巧。 阅读详情

目录结构

1.题目

2.题解

2.1字符串

2.2数学


1.题目

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

示例:

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


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

 提示:

  • 1 <= nums.length <= 500
  • 1 <= nums[i] <= 10^5

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-numbers-with-even-number-of-digits
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2.题解

2.1字符串

遍历数组 nums 中的整数,将整数转换为字符串后,判断其长度是否为偶数。

public class Solution1295 {
    public int findNumbers(int[] nums) {
        int result = 0;
        for (int t : nums) {
            String str = String.valueOf(t);
            if (str.length() % 2 == 0) {
                result++;
            }
        }
        return result;
    }
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(1)

2.2数学

以 10 为底的对数函数 log10() 来得到整数 x 包含的数字个数。

  • k 位整数 x 满足不等式 10^{k-1}\leq x< 10^k。
  • 不等式取对数,得 k-1\leq log10(x)< k。

故可用k=\left \lfloor log10(x)+1 \right \rfloor得到 x 的位数。

public class Solution1295 {
    public int findNumbers(int[] nums) {
        int result = 0;
        for (int t : nums) {
            if ((int) (Math.log10(t) + 1) % 2 == 0) {
                result++;
            }
        }
        return result;
    }
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(1)
43、FMCW雷达高度计仿真 本文介绍了FMCW雷达高度计的波形级建模与仿真过程,重点模拟了着陆和倾斜两种飞行场景以评估其性能。通过结合高程数据和飞行轨迹构建雷达场景,使用自定义信号处理器分析IQ样本并估计高度。结果表明,该高度计在多数情况下满足性能要求,但也存在误差较大的点,提出了地面检测算法优化的潜在方向。 阅读详情

相关推荐

如何将COMSOL与MATLAB联合【超详细,一网打尽】

如何将COMSOL与MATLAB联合 文章目录如何将COMSOL与MATLAB联合前言一、背景二、使用步骤1. 找到 COMSOL Multiphysics 5.5 with MATLAB2. **双击COMSOL Multiphysics 5.5 with MATLAB** 前言 网上搜索教程,发现大部分教程讲得比较初略,或者信息比较分散。本着自己踩过坑就不愿意让别人再踩一遍的原则,写下这篇博文。 提示:以下是本篇文章正文内容,下面案例可供参考 一、背景 COMSOL 5.5 MATLAB 20

fendiman的博客 7万+

1295.统计位数为偶数的数字

原文链接:https://leetcode-cn.com/problems/find-numbers-with-even-number-of-digits/ 题目: 思路分析:这道题是给定我们一个整数数组,然后让我们返回其中位数为偶数的数字个数。 有两种方法,第一种就是遍历nums数组,给每一位元素加一个空字符将其转换成字符串,然后统计其长度并对2取余,如果等于0,说明其位数是偶...

Agonyq的博客 457

国内网络环境下Leaflet加载OpenStreetMap地图的5种替代方案(附可用瓦片地址)

本文针对国内网络环境下Leaflet加载OpenStreetMap地图的常见问题,提供了5种可用的瓦片替代方案,包括OpenStreetMap.fr HOT、OpenStreetMap.de、CartoDB、Stamen和Thunderforest,并附上详细的代码示例和选择建议,帮助开发者解决地图加载问题。

qsc901234的博客 1772

LeetCode.1295. 统计位数为偶数的数字

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

m0_62780794的博客 382

六维力传感器:原理、选型与应用全解析

在机器人、自动化与精密测量领域,力觉感知是实现智能控制与自适应操作的核心技术。其基本原理是通过传感器将物理世界的力学作用转化为可处理的电信号,从而为系统提供关键的交互信息。这项技术的核心价值在于,它使机器从简单的程序执行升级为具备环境感知与反馈能力的智能体,极大地提升了操作的精度、安全性与适应性。从技术实现上看,它依赖于精密的弹性体结构设计与高灵敏度的应变片传感,并通过复杂的信号调理与数据处理来保证测量精度。在实际工程中,六维力传感器的应用场景广泛,例如在机器人**柔顺装配**中实现微米级的精准对位,在**

weixin_29189003的博客 407

手把手教你逆向京东搜索接口:H5ST 556位参数生成与V8补环境技巧

本文深入解析了京东搜索接口中关键的H5ST 556位参数生成机制,并提供了详细的逆向实战指南。针对其复杂的反爬虫环境检测,文章重点介绍了通过链式补环境技巧,在Node.js中完整模拟浏览器环境(包括原型链检测和指纹验证)的方法,帮助开发者成功绕过防护并生成有效参数。

weixin_29315091的博客 494

python统计奇数和偶数的个数_Python 统计位数为偶数的数字代码详解

问题描述给出一个整数数组 nums,请返回其中位数为偶数的数字的个数。示例 1:输入:nums = [12,345,2,6,7896]输出:2解释:12 是 2 位数字(位数为偶数)345 是 3 位数字(位数为奇数)2 是 1 位数字(位数为奇数)6 是 1 位数字 位数为奇数)7896 是 4 位数字(位数为偶数)因此只有 12 和 7896 是位数为偶数的数字示例 2:输入:nums = [...

weixin_35924773的博客 1万+

dq轴坐标系下的电路方程

dq轴坐标系下的电路方程 dq轴坐标系是一种常见的电路方程,但是再dq轴坐标系下电路元件的电压电流dq轴存在一定的耦合,本文针对三相电路推导了dq轴坐标系下的电路电路方程进行一个推导,首先先列出来派克变换的公式 派克变换 [AdAqA0]=23[cos⁡(θ)cos⁡(θ−2π/3)cos⁡(θ+2π/3)−sin⁡(θ)−sin⁡(θ−2π/3)−sin⁡(θ+2π/3)1/21/21/2][AaAbAc]\left[\begin{array}{c}A_d\\A_q\\A_0 \end{array}\ri

Mr.Young的博客 1万+

每日一题——力扣——统计位数为偶数的数字

题目来源于力扣——1295. 统计位数为偶数的数字 - 力扣(LeetCode) (leetcode-cn.com) 给你一个整数数组nums,请你返回其中位数为偶数的数字的个数。 示例 1: 输入:nums = [12,345,2,6,7896] 输出:2 解释: 12 是 2 位数字(位数为偶数) 345 是 3 位数字(位数为奇数) 2 是 1 位数字(位数为奇数) 6 是 1 位数字 位数为奇数) 7896 是 4 位数字(位数为偶数) 因此只有 12 和 789...

爱编程的晖哥的博客 1114

【EMC专题】为什么芯片规格书ESD性能中看不到MM(机器放电模型)

器件级ESD包括三种类型,HBM人体放电模型、MM机械放电模型和CDM元件充电模型。但在芯片规格书中为什么只见到HBM模型与CDM模型,却没有看到MM模型的数据?

一点硬件 458

最新Python爬虫实战(入门爬虫篇)——案例8:图书网畅销榜数据采集(详细爬虫思路截图+抓包动图演示+完整爬虫代码+详细注释)

本次案例通过 Python 爬虫技术,成功实现了对图书网畅销榜多页数据的批量爬取与 Excel 保存。代码采用模块化设计,包含分页参数构造、API 接口请求、JSON 数据解析、Excel 数据持久化等完整流程;通过配置模拟浏览器的请求头,有效规避基础反爬策略,保证请求的稳定性。整个爬虫逻辑遵循 “分页遍历 → 接口请求 → JSON 解析 → 提取图书信息 → 保存到 Excel” 的标准流程,与电商类接口爬取范式高度一致。

努力让自己发光,对的人才能迎着光而来 29万+

leetcode之统计位数为偶数的数字(C++)

参考链接 https://leetcode-cn.com/problems/find-numbers-with-even-number-of-digits/ 题目描述 给你一个整数数组nums,请你返回其中位数为偶数的数字的个数。 解题思路 分两步,先求出数的位数,再判断位数是否为偶数。第一步可以用除10的方式统计出来,也可以转化为字符串,直接看字符串的长度;第二步可以对2取余进行判断,也可以用位运算,与1相与。 代码 朴素解法 class Solution { public: int fin

beilizhang的博客 831

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

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

LeetCode 1295.统计位数为偶数的数字:模拟 给你一个整数数组 nums,请你返回其中包含 偶数 个数位的数字的个数。

<font color="Pink">Tisfy的博客</font> 1349

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

LeetCode刷题之1295. 统计位数为偶数的数字

LeetCode刷题之1295. 统计位数为偶数的数字 我不知道将去向何方,但我已在路上! 时光匆匆,虽未曾谋面,却相遇于斯,实在是莫大的缘分,感谢您的到访 ! 题目: 给你一个整数数组 nums,请你返回其中位数为 偶数 的数字的个数。 示例: 示例 1 : 输入:nums = [12,345,2,6,7896] 输出:2 解释: 12 是 2 位数字(位数为偶数) 345 是 3 位数字(位数为奇数) 2 是 1 位数字(位数为奇数) 6 是 1 位数字 位

胡萝卜 402

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

力扣Leetcode第1295题,统计位数为偶数的数字的Python解法

Description 给你一个整数数组 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 解释: 只有 177

weixin_43955866的博客 173

#力扣 LeetCode1295. 统计位数为偶数的数字 @FDDLC

题目描述: https://leetcode-cn.com/problems/find-numbers-with-even-number-of-digits/ Java代码: class Solution { public boolean judge(int n){ //1 <= nums[i] <= 10^5 if(n<10)return false; else if(n<100)return true; els.

LiuXingchang的博客 356

C练题笔记之:Leetcode-1295. 统计位数为偶数的数字

题目: 给你一个整数数组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 解释: 只...

lingjinyue的博客 307

【算法leetcode】1295. 统计位数为偶数的数字(多语言实现)

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

二当家的白帽子 434

python-leetcode-1295. 统计位数为偶数的数字

是为了处理负数,确保我们只计算数字部分。将数字转为字符串,然后用。都是偶数位数的数字。),就将计数器加一。

Lucy_wzw的博客 259

[LeetCode] 1295. 统计位数为偶数的数字

1 题目描述 给你一个整数数组 nums,请你返回其中位数为 偶数 的数字的个数。 示例 1: 输入:nums = [12,345,2,6,7896] 输出:2 解释: 12 是 2 位数字(位数为偶数) 345 是 3 位数字(位数为奇数) 2 是 1 位数字(位数为奇数) 6 是 1 位数字 位数为奇数) 7896 是 4 位数字(位数为偶数) 因此只有 12 和 7896 是位数为偶数的数...

努力变得不菜的菜鸡的博客 476

leetcode 1295. 统计位数为偶数的数字

给你一个整数数组 nums,请你返回其中位数为 偶数 的数字的个数。计算每一个数的位数是否为偶数,如果是则计数,不是跳过到下一个数字。输入:nums = [555,901,482,1771]输入:nums = [12,345,2,6,7896]因此只有 12 和 7896 是位数为偶数的数字。7896 是 4 位数字(位数为偶数)345 是 3 位数字(位数为奇数)只有 1771 是位数为偶数的数字。12 是 2 位数字(位数为偶数)2 是 1 位数字(位数为奇数)6 是 1 位数字 位数为奇数)

dengjiayue的博客 443

LeetCode之1295. 统计位数为偶数的数字

概要 题目来源链接:https://leetcode-cn.com/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 位数字...

二木成林 657
上一篇: LeetCode 1299. 将每个元素替换为右侧最大元素
下一篇: LeetCode 1304. 和为零的N个唯一整数
HarvestWu
博客等级 码龄9年 90粉丝 527原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值