50,781
社区成员
发帖
与我相关
我的任务
分享
完结撒花
1.
/**
* 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:
string inv(string ch) {
reverse(ch.begin(),ch.end());
return ch;
}
bool isPalindrome(ListNode* head) {
string ch;
while(head!=nullptr) {
ch+=head->val;
head=head->next;
}
return ch==inv(ch);
}
};
2.
class Solution {
public:
vector<vector<string>>res;
int n;
vector<string>cnt;
string inv(string ch) {
reverse(ch.begin(),ch.end());
return ch;
}
void dfs(string s,int v) {
if(v>=n) {
res.push_back(cnt);
return;
}
for(int i=v;i<n;i++) {
string as=s.substr(v,i-v+1);
if(inv(as)==as) {
cnt.push_back(as);
dfs(s,i+1);
cnt.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
n=s.size();
dfs(s,0);
return res;
}
};
3.
class Solution {
public:
int get(int n)
{
string a=to_string(n);
string b=a;
reverse(b.begin(),b.end());
string c=a.substr(0,a.size()-1)+b;
return stoi(c);
}
bool check(int n)
{
if(n<2) return false;
for(int i=2;i<=n/i;i++)
{
if(n%i==0) return false;
}
return true;
}
int primePalindrome(int n) {
if(n>7&&n<=11) return 11;
for(int i=1;;i++)
{
int a=get(i);
if(a>=n&&check(a)) return a;
}
return 0;
}
};
4.
class Solution {
public:
int Prime(int n){
if(n<=2) return 0;
int cnt=1;
bool isPrime[5000010];
memset(isPrime,true,sizeof(isPrime));
isPrime[0]=isPrime[1]=false;
for(int i=3;i<n;i+=2){
if(isPrime[i]){
cnt++;
for(int j=2;j*i<=n;j++) isPrime[j*i]=false;
}
}
return cnt;
}
int countPrimes(int n) {
return Prime(n);
}
};