1295. Find Numbers with Even Number of Digits

problem

solution

option 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

class Solution {
public:
int digits(int n){
int d= 0 ;
while(n){
d++;
n/=10;
}
return d;
}
int findNumbers(vector<int>& nums) {
int count;
for(int n:nums) count+=(digits(n)%2==0?1:0);
return count;
}
};

option 2 - math

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int findNumbers(vector<int>& nums) {
int count = 0;
for(int n:nums){
int temp = floor(log10(n))+1;
count+=(temp%2==0?1:0);
}
return count;
}
};

analysis

  • option 1
    • time complexity O(nlogn)
    • space complexity O(1)
  • option 2
    • time complexity O(n)
    • space complexity O(1)