leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字
1.原题:
https://leetcode.com/problems/find-numbers-with-even-number-of-digits/
Given an array nums of integers, return how many of them contain an even number of digits.
翻译:给定一个整数数组,输出拥有偶数数位的数字的数量。
理论上的输入输出:
Input: nums = [555,901,482,1771]
Output: 1
2.解题思路:
遇到这种需要区别偶奇数的情况,通常都应该想到要用 % 运算符,不过这次我们的目的不是查看数字本身是不是偶数,所以需要用到 to_string(int) 这个函数,可以把 int 转换成string。
然后再用size()来看string的长度,再把长度 % 2 就可以得知是否是偶数。
class Solution {
public:
int findNumbers(vector<int>& nums) {
return count_if(nums.begin(), nums.end(), [](const auto& a) {
return to_string(a).size() % 2 == 0;
});
}
};
leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字的更多相关文章
- leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法
1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...
- leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石
1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...
- leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字
1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...
- leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)
Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...
- leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping
1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...
- leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST
1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...
- leetcode菜鸡斗智斗勇系列(8)--- Find N Unique Integers Sum up to Zero
1.原题: https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/ Given an integer n, retur ...
- leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点
1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...
- leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段
1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...
随机推荐
- python 爬取图片
使用python的requests库爬取网页时,获取文本一般使用text方法,如果要获取图片并保存要用content 举个栗子,爬煎蛋网的图: #!/usr/bin/env python #-*- c ...
- phpstorm 断点调试
1.设置php的xdebug 在php.ini中设置 [XDebug] xdebug.profiler_output_dir="H:\phystudy\PHPTutorial\tmp\xde ...
- 计算机二级-C语言-程序填空题-190117记录-对文件的处理,复制两个文件,往新文件中写入数据。
//给定程序的功能是,调用函数fun将指定源文件中的内容赋值到指定目标文件中,复制成功时函数返回1,失败时返回0,把复制的内容输出到终端屏幕.主函数中源文件名放在变量sfname中,目标文件名放在变量 ...
- Centos610安装Nexus
1.下载Nexus 地址: https://pan.baidu.com/s/1D5AI6zmuRBSMK0k7j41VuQ 提取码: q50j 选择02-nexus 2.新建nexus账号 u ...
- 新建maven工程运行出现Intellij idea 报错:Error : java 不支持发行版本5
Step1点击: 点击 保持一致: Step2点击 这样就可以了. 主要是运行时jdk版本不一致的问题
- TreeGrid分页树形表格
先展示效果图: 加载treegrid的json数据格式有两种: (1)基本的数据结构 [{ , "name":"C", "size":&qu ...
- Spring Boot Mybatis 使用教程
Mybatis 在当下互联网开发环境,十分重要.本章主要讲述 Mybatis 如何使用. 从本系列开始,都需要用到 mysql 数据库 和其他一些参考的数据库.请准备相关环节.本章需要以下环境支撑: ...
- Golang核心编程
源码地址: https://github.com/mikeygithub/GoCode 第1章 1Golang 的学习方向 Go 语言,我们可以简单的写成 Golang 1.2Golang 的应用领域 ...
- es 搜索功能简介
DSL 语法介绍 语法 范围 /_search 集群上搜索所有的索引 /index1/_search index1 /index1,index2/_search index1和index2 /inde ...
- MariaDB-Galera部署
Galera Cluster:集成了Galera插件的MySQL集群,是一种新型的,数据不共享的,高度冗余的高可用方案,目前Galera Cluster有两个版本,分别是Percona Xtradb ...