Skip to content

Commit

Permalink
Create leetcode204.cpp
Browse files Browse the repository at this point in the history
leetcode solution for question 204
  • Loading branch information
rituraj-abes authored Oct 14, 2023
1 parent 8dfe2fe commit fe7dea1
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions C++/leetcode204.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
204. Count Primes
Given an integer n, return the number of prime numbers that are strictly less than n.
Example 1:
Input: n = 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
*/
class Solution {
public:
int countPrimes(int n) {
if(n == 0)
return 0;
vector<bool> prime(n,true);
prime[0] = prime [1] = false ;
int ans = 0;
for(int i = 2 ; i<n ; i++)
{
if(prime[i])
{
ans++;
int j=2*i;
while(j<n)
{
prime[j] = false;
j = j + i ;
}
}
}
return ans;
}
};

0 comments on commit fe7dea1

Please sign in to comment.