Find Numbers with Even Number of Digits using the Reduce Functio

  • 时间:2020-09-12 10:17:13
  • 分类:网络文摘
  • 阅读:135 次

Given an array nums of integers, return how many of them contain an even number of digits.

Example 1:
Input: nums = [12,345,2,6,7896]
Output: 2
Explanation:
12 contains 2 digits (even number of digits).
345 contains 3 digits (odd number of digits).
2 contains 1 digit (odd number of digits).
6 contains 1 digit (odd number of digits).
7896 contains 4 digits (even number of digits).
Therefore only 12 and 7896 contain an even number of digits.

Example 2:
Input: nums = [555,901,482,1771]
Output: 1
Explanation:
Only 1771 contains an even number of digits.

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

Hints:
How to compute the number of digits of a number ?
Divide the number by 10 again and again to get the number of digits.

How to Compute the Number of Digits given a Integer?

You could, however, convert the number into string, then obtain its length. If you want to do it mathematically, you could do the following division by ten, until it is smaller than 10.

1
2
3
4
5
6
7
8
int getNumberOfDigits(n) {
   int len = 1;
   while (n >= 10) {
      n /= 10;
      len ++;
   }
   return len;
}
int getNumberOfDigits(n) {
   int len = 1;
   while (n >= 10) {
      n /= 10;
      len ++;
   }
   return len;
}

Using Reduce In Python to Find Numbers with Even Number of Digits

The Reduce() helps to accumulate a value based on a lambda function, known as the reducer function.

1
2
3
4
5
from functools import reduce
 
class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        return reduce(lambda s, x: s + (1 - len(str(x)) % 2), nums, 0)
from functools import reduce

class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        return reduce(lambda s, x: s + (1 - len(str(x)) % 2), nums, 0)

Using Reduce In Javascript to Find Numbers with Even Number of Digits

The reduce() can be invoked on the array in Javascript.

1
2
3
4
5
6
7
/**
 * @param {number[]} nums
 * @return {number}
 */
var findNumbers = function(nums) {
    return nums.reduce((s, x) => s + (1 - ('' + x).length % 2), 0);
};
/**
 * @param {number[]} nums
 * @return {number}
 */
var findNumbers = function(nums) {
    return nums.reduce((s, x) => s + (1 - ('' + x).length % 2), 0);
};

Using std::accumulate() to Find Numbers with Even Number of Digits

In C++, the std::accumulate() is the reduce method.

1
2
3
4
5
6
7
8
class Solution {
public:
    int findNumbers(vector<int>& nums) {
        return std::accumulate(begin(nums), end(nums), 0, [](auto &s, auto &n) {
            return s + 1 - std::to_string(n).size() % 2;
        });
    }
};
class Solution {
public:
    int findNumbers(vector<int>& nums) {
        return std::accumulate(begin(nums), end(nums), 0, [](auto &s, auto &n) {
            return s + 1 - std::to_string(n).size() % 2;
        });
    }
};

Using std::count_if() to Find Numbers with Even Number of Digits

Alternatively, you can count_if based on an (anonymous) function:

1
2
3
4
5
6
7
8
class Solution {
public:
    int findNumbers(vector<int>& nums) {
        return std::count_if(begin(nums), end(nums), [](auto &num) {
            return std::to_string(num).size() % 2 == 0;
        });
    }
};
class Solution {
public:
    int findNumbers(vector<int>& nums) {
        return std::count_if(begin(nums), end(nums), [](auto &num) {
            return std::to_string(num).size() % 2 == 0;
        });
    }
};

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
达兰倍尔错在哪里  古罗马人的遗嘱  将10毫升酒装入一个圆锥容器中  为 WordPress 设置评论模块登录可见  仅允许游客浏览wordpress指定分类的文章  指定 wordpress 默认用户角色后台登陆权限  为 WordPress 主题添加大红灯笼特效  为 wordpress 文章作者在评论留言时显示“本文作者”提示  为 WordPress 主题添加花瓣飘落特效  wordpress插件:WP-China-Yes 切换WP站点与官方通信至国内节点解决后台更新429错误 
评论列表
添加评论