Skip to content

Commit 3ad2047

Browse files
add 1295
1 parent fa9de70 commit 3ad2047

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 1295. Find Numbers with Even Number of Digits
7+
*
8+
* Given an array nums of integers, return how many of them contain an even number of digits.
9+
*
10+
* Example 1:
11+
* Input: nums = [12,345,2,6,7896]
12+
* Output: 2
13+
* Explanation:
14+
* 12 contains 2 digits (even number of digits).
15+
* 345 contains 3 digits (odd number of digits).
16+
* 2 contains 1 digit (odd number of digits).
17+
* 6 contains 1 digit (odd number of digits).
18+
* 7896 contains 4 digits (even number of digits).
19+
* Therefore only 12 and 7896 contain an even number of digits.
20+
*
21+
* Example 2:
22+
* Input: nums = [555,901,482,1771]
23+
* Output: 1
24+
* Explanation:
25+
* Only 1771 contains an even number of digits.
26+
*
27+
* Constraints:
28+
* 1 <= nums.length <= 500
29+
* 1 <= nums[i] <= 10^5
30+
* */
31+
public class _1295 {
32+
public static class Solution1 {
33+
public int findNumbers(int[] nums) {
34+
int count = 0;
35+
for (int num : nums) {
36+
if (String.valueOf(num).length() % 2 == 0) {
37+
count++;
38+
}
39+
}
40+
return count;
41+
}
42+
}
43+
44+
public static class Solution2 {
45+
public int findNumbers(int[] nums) {
46+
return (int) Arrays.stream(nums).filter(num -> String.valueOf(num).length() % 2 == 0).count();
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy