50,781
社区成员
发帖
与我相关
我的任务
分享
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
vector<int> res;
while(head != nullptr)
{
res.push_back(head->val);
head=head->next;
}
for(int i=0, j=res.size()-1; i<j; i++, j--)
{
if(res[i]!=res[j]) return false;
}
return true;
}
};
class Solution {
public:
bool is_prime(int n)
{
if(n<2) return false;
for(int i=2; i<=sqrt(n); i++)
if(n%i==0) return false;
return true;
}
int reverse(int n)
{
int res=0;
while(n)
{
res = res*10+n%10;
n/=10;
}
return res;
}
int primePalindrome(int n)
{
while(1)
{
if(is_prime(n) && n==reverse(n))
return n;
n++;
if(n > 1e7 && n < 1e8)
n=1e8;
}
}
};
class Solution {
public:
int prime[5000010];
bool st[5000010];
int countPrimes(int n) {
int cnt=0;
for(int i=2; i<n; i++)
{
if(!st[i]) prime[cnt++]=i;
for(int j=0; prime[j]<=n/i; j++)
{
st[prime[j]*i]=true;
if(i%prime[j]==0) break;
}
}
return cnt;
}
};