Skip to content

Commit cada034

Browse files
committed
Add 906,917-919,949-952
1 parent 529e4cc commit cada034

File tree

9 files changed

+330
-1
lines changed

9 files changed

+330
-1
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Solution {
2+
public:
3+
bool larger(string a, string b){
4+
if(a.size() < b.size())
5+
return false;
6+
if(a.size() > b.size())
7+
return true;
8+
for(int i = 0; i < a.size(); i++){
9+
if(a[i] != b[i]) {
10+
return a[i] > b[i];
11+
}
12+
}
13+
return false;
14+
}
15+
16+
string square(string input){
17+
// 平方
18+
long long temp = stoll(input);
19+
long long result = temp*temp;
20+
return to_string(result);
21+
}
22+
bool isPar(string input){
23+
// 回文
24+
for(int l=0, r=input.size()-1;l<=r;l++,r--){
25+
if(input[l] != input[r])
26+
return false;
27+
}
28+
return true;
29+
}
30+
31+
int superpalindromesInRange(string L, string R) {
32+
int n = R.size();
33+
vector<vector<string>> pool; // 按相同位数存储回文串
34+
for(int i=0; i<n/2+1; i++){
35+
vector<string> cur;
36+
if(i == 0){
37+
cur = {"0", "1", "2", "3", "4", "5", "6", "7","8", "9"};
38+
}else if(i == 1){
39+
cur = {"00", "11", "22", "33", "44", "55", "66", "77", "88", "99"};
40+
}else{
41+
vector<string> &prev = pool[i-2];
42+
for(auto &p:prev){
43+
for(auto &dig:pool[0]){
44+
cur.push_back(dig + p + dig);
45+
}
46+
}
47+
}
48+
pool.push_back(cur);
49+
}
50+
51+
// 统计符合条件的super
52+
int count = 0;
53+
for(auto &strs:pool){
54+
for(auto &str:strs){
55+
string super = square(str);
56+
if(isPar(super) && (larger(super, L) || super == L ) && (larger(R, super) || super == R ) ){
57+
count ++;
58+
}
59+
}
60+
}
61+
return count;
62+
}
63+
64+
65+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
string reverseOnlyLetters(string S) {
4+
int l=0,r=S.size()-1;
5+
while(l<r){
6+
while(!isalpha(S[l]))
7+
l++;
8+
while(!isalpha(S[r]))
9+
r--;
10+
if(l<r){
11+
swap(S[l],S[r]);
12+
l++;
13+
r--;
14+
}
15+
}
16+
return S;
17+
}
18+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
int maxSubarraySumCircular(vector<int>& A) {
4+
if(A.size()==0)
5+
return 0;
6+
if(A.size()==1)
7+
return A[0];
8+
int ans=INT_MIN;
9+
int tol=0;
10+
int least=0;
11+
int num=INT_MAX;
12+
int sum=0;
13+
for(auto ele : A){
14+
tol+=ele;
15+
sum+=ele;
16+
if(sum<0){
17+
ans=max(ans,ele);
18+
sum=0;
19+
}else{
20+
ans=max(sum,ans);
21+
}
22+
least+=ele;
23+
if(least>0)
24+
least=0;
25+
else
26+
num=min(least,num);
27+
}
28+
if(ans<=0)
29+
return ans;
30+
else
31+
return max(ans,tol-num);
32+
}
33+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class CBTInserter {
11+
public:
12+
TreeNode* r;
13+
queue<TreeNode *> q;
14+
CBTInserter(TreeNode* root) {
15+
r = root;
16+
queue<TreeNode *> tq;
17+
tq.push(r);
18+
TreeNode *t;
19+
while(tq.size()>0){
20+
t = tq.front();
21+
tq.pop();
22+
if(t->left != NULL)
23+
tq.push(t->left);
24+
if(t->right != NULL)
25+
tq.push(t->right);
26+
if(t->left == NULL || t->right == NULL)
27+
q.push(t);
28+
}
29+
}
30+
31+
int insert(int v) {
32+
TreeNode *t = new TreeNode(v);
33+
TreeNode *n = q.front();
34+
if(n->left == NULL){
35+
n->left = t;
36+
}else{
37+
n->right = t;
38+
q.pop();
39+
}
40+
q.push(t);
41+
return n->val;
42+
}
43+
44+
TreeNode* get_root() {
45+
return r;
46+
}
47+
};
48+
49+
/**
50+
* Your CBTInserter object will be instantiated and called as such:
51+
* CBTInserter obj = new CBTInserter(root);
52+
* int param_1 = obj.insert(v);
53+
* TreeNode* param_2 = obj.get_root();
54+
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
public:
3+
string largestTimeFromDigits(vector<int>& A) {
4+
vector<bool> used(4,false);
5+
vector<int> result;
6+
for(int i=0;i<A.size();i++){
7+
if(A[i]<0 || A[i]>2)
8+
continue;
9+
int t1 = A[i];
10+
used[i] = true;
11+
for(int j=0;j<A.size();j++){
12+
if(used[j] || (t1==2 && (A[j]>3 || A[j]<0) ) )
13+
continue;
14+
int t2 = t1*10 + A[j];
15+
used[j] = true;
16+
for(int k=0;k<A.size();k++){
17+
if(used[k] || A[k]>5 || A[k]<0)
18+
continue;
19+
int t3 = t2*10 + A[k];
20+
used[k] = true;
21+
for(int y=0;y<A.size();y++){
22+
if(used[y])
23+
continue;
24+
result.push_back(t3*10+A[y]);
25+
// cout<<t3*10+A[y]<<endl;
26+
}
27+
used[k] = false;
28+
}
29+
used[j] = false;
30+
}
31+
used[i] = false;
32+
}
33+
34+
if(result.empty())
35+
return "";
36+
37+
vector<int>::iterator it = max_element(result.begin(), result.end());
38+
string r = to_string(*it);
39+
if(r.size()<4)
40+
r = string(4-r.size(),'0') + r;
41+
42+
return r.substr(0,2) + ":" + r.substr(2);
43+
}
44+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<int> deckRevealedIncreasing(vector<int>& deck) {
4+
sort(deck.begin(),deck.end());
5+
queue<int> q;
6+
for(int i=deck.size()-1;i>=0;i--){
7+
if(!q.empty()){
8+
int t = q.front();
9+
q.pop();
10+
q.push(t);
11+
}
12+
q.push(deck[i]);
13+
}
14+
vector<int> result(q.size());
15+
int i = q.size()-1;
16+
while(!q.empty()){
17+
result[i] = q.front();
18+
i--;
19+
q.pop();
20+
}
21+
return result;
22+
}
23+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
bool eq(TreeNode* n1, TreeNode* n2){
13+
if(n1 == NULL && n2 == NULL)
14+
return true;
15+
else if(n1 == NULL || n2 == NULL)
16+
return false;
17+
else{
18+
if(n1->val == n2->val)
19+
return true;
20+
}
21+
return false;
22+
}
23+
24+
bool flipEquiv(TreeNode* root1, TreeNode* root2) {
25+
if(eq(root1,root2)){
26+
if(root1 != NULL){
27+
return flipEquiv(root1->left, root2->left) && flipEquiv(root1->right, root2->right)
28+
|| flipEquiv(root1->left, root2->right) && flipEquiv(root1->right, root2->left);;
29+
}
30+
return true;
31+
}
32+
return false;
33+
}
34+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public:
3+
int p[100100], sz[100100];
4+
5+
int find(int x) {
6+
return x == p[x] ? x : p[x] = find(p[x]);
7+
}
8+
9+
void merge(int x, int y) {
10+
x = find(x);
11+
y = find(y);
12+
if(x == y) return;
13+
sz[x] += sz[y];
14+
p[y] = x;
15+
}
16+
17+
int largestComponentSize(vector<int>& A) {
18+
bool mark[100100] = {0}, used[100100] = {0};
19+
for(int x : A) {
20+
mark[x] = 1;
21+
p[x] = x;
22+
sz[x] = 1;
23+
}
24+
// Sieve of erastosthenes
25+
for(long long i = 2; i <= 100000; i++) {
26+
if(!used[i]) {
27+
int last = -1;
28+
// i is a prime, union all numbers which has i as its factor.
29+
for(long long j = i; j <= 100000; j += i) {
30+
used[j] = 1;
31+
if(mark[j]) {
32+
if(last != -1) merge(last, j);
33+
last = j;
34+
}
35+
}
36+
}
37+
}
38+
int ans = 0;
39+
for(int x : A) ans = max(ans, sz[x]);
40+
return ans;
41+
}
42+
};

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,10 +597,18 @@
597597

598598
[897.Increasing-Order-Search-Tree](Algorithms/897.Increasing-Order-Search-Tree/solution.cpp)
599599

600+
[906.Super-Palindromes](Algorithms/906.Super-Palindromes/solution.cpp)
601+
600602
[914.X-of-a-Kind-in-a-Deck-of-Cards](Algorithms/914.X-of-a-Kind-in-a-Deck-of-Cards/solution.cpp)
601603

602604
[915.Partition-Array-into-Disjoint-Intervals](Algorithms/915.Partition-Array-into-Disjoint-Intervals/solution.cpp)
603605

606+
[918.Maximum-Sum-Circular-Subarray](Algorithms/918.Maximum-Sum-Circular-Subarray/solution.cpp)
607+
608+
[917.Reverse-Only-Letters](Algorithms/917.Reverse-Only-Letters/solution.cpp)
609+
610+
[919.Complete-Binary-Tree-Inserter](Algorithms/919.Complete-Binary-Tree-Inserter/solution.cpp)
611+
604612
[925.Long-Pressed-Name](Algorithms/925.Long-Pressed-Name/solution.cpp)
605613

606614
[926.Flip-String-to-Monotone-Increasing](Algorithms/926.Flip-String-to-Monotone-Increasing/solution.cpp)
@@ -635,4 +643,12 @@
635643

636644
[947.Most-Stones-Removed-with-Same-Row-or-Column](Algorithms/947.Most-Stones-Removed-with-Same-Row-or-Column/solution.cpp)
637645

638-
[948.Bag-of-Tokens](Algorithms/948.Bag-of-Tokens/solution.cpp)
646+
[948.Bag-of-Tokens](Algorithms/948.Bag-of-Tokens/solution.cpp)
647+
648+
[949.Largest-Time-for-Given-Digits](Algorithms/949.Largest-Time-for-Given-Digits/solution.cpp)
649+
650+
[950.Reveal-Cards-In-Increasing-Order](Algorithms/950.Reveal-Cards-In-Increasing-Order/solution.cpp)
651+
652+
[951.Flip-Equivalent-Binary-Trees](Algorithms/951.Flip-Equivalent-Binary-Trees/solution.cpp)
653+
654+
[952.Largest-Component-Size-by-Common-Factor](Algorithms/952.Largest-Component-Size-by-Common-Factor/solution.cpp)

0 commit comments

Comments
 (0)