aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-04-29 21:10:55 -0600
committerJack Sangdahl <[email protected]>2025-04-29 21:10:55 -0600
commit56b6d146c7f33b89c4ef573d0746dc0f306684f0 (patch)
treeec9c360f98e0c4d1423b149b1c1ccd9591d844cd
parent2e8743f0ebda2c02358ce0f124a754fcf9059668 (diff)
downloadleetcode-56b6d146c7f33b89c4ef573d0746dc0f306684f0.tar.gz
leetcode-56b6d146c7f33b89c4ef573d0746dc0f306684f0.zip
1295. find numbers with even number of digitsHEADmaster
-rw-r--r--find_num_even_digit.c27
-rw-r--r--find_num_even_digit.py21
2 files changed, 48 insertions, 0 deletions
diff --git a/find_num_even_digit.c b/find_num_even_digit.c
new file mode 100644
index 0000000..351896f
--- /dev/null
+++ b/find_num_even_digit.c
@@ -0,0 +1,27 @@
+// 1295. Find Numbers with Even Number of Digits
+#include "leetcode.h"
+
+/*
+ * given an array 'nums' of integers, return how many of them contain an even
+ * number of digits.
+ */
+
+int findNumbers(int *nums, int numsSize) {
+ int cnt = 0;
+ for (int i = 0; i < numsSize; i++) {
+ int curr = 0;
+ while (nums[i]) {
+ curr++;
+ nums[i] /= 10;
+ }
+ if (!(curr & 1))
+ cnt++;
+ }
+ return cnt;
+}
+
+int main() {
+ int n1[] = {12, 345, 2, 6, 7896}, n2[] = {555, 901, 482, 1771};
+ printf("%d\n", findNumbers(n1, ARRAY_SIZE(n1))); // expect: 2
+ printf("%d\n", findNumbers(n2, ARRAY_SIZE(n2))); // expect: 1
+}
diff --git a/find_num_even_digit.py b/find_num_even_digit.py
new file mode 100644
index 0000000..5cdb9e3
--- /dev/null
+++ b/find_num_even_digit.py
@@ -0,0 +1,21 @@
+# 1295. Find Numbers with Even Number of Digits
+
+"""
+given an array 'nums' of integers, return how many of them contain an even
+number of digits.
+"""
+
+
+class Solution(object):
+ def findNumbers(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: int
+ """
+ return sum(1 for i in nums if (len(str(i)) & 1) == 0)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.findNumbers(nums=[12, 345, 2, 6, 7896]))
+ print(obj.findNumbers(nums=[555, 901, 482, 1771]))