diff options
Diffstat (limited to 'other/num_even_nums.cpp')
-rw-r--r-- | other/num_even_nums.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/other/num_even_nums.cpp b/other/num_even_nums.cpp new file mode 100644 index 0000000..1513c72 --- /dev/null +++ b/other/num_even_nums.cpp @@ -0,0 +1,29 @@ +#include "leetcode.h" + +/* + * given array 'nums' of integers, return how + * many of them contain an even number of digits. + */ + +class Solution { +public: + int findNumbers(vector<int> &nums) { + int ans = 0; + // use auto to identify data type & iterate through array + for (auto i : nums) + /* + * log10 returns a decimal so convert it to int + * it also returns n - 1. eg. log10(100) = 2 + * use bitwise and to check if it's odd + */ + ans += log10(i) & 1; + return ans; + } +}; + +int main() { + Solution obj; + vector<int> nums1 = {12, 345, 2, 6, 7896}, nums2 = {555, 901, 482, 1771}; + cout << obj.findNumbers(nums1) << endl; // expect: 2 + cout << obj.findNumbers(nums2) << endl; // expect: 1 +} |