当前位置:   article > 正文

letcode第 311 场周赛_311场周赛csdn

311场周赛csdn

6180. 最小偶倍数

  1. class Solution {
  2. public:
  3. int smallestEvenMultiple(int n) {
  4. if(n&1)return n*2;
  5. return n;
  6. }
  7. };

6181. 最长的字母序连续子字符串的长度

最长也就26,枚举即可

  1. class Solution {
  2. public:
  3. int longestContinuousSubstring(string s) {
  4. int n=s.length();
  5. for(int len=26;len>=1;len--){
  6. for(int l=0;l+len-1<n;l++){
  7. int r=l+len-1;
  8. bool flag = true;
  9. for(int k=l+1;k<=r;k++){
  10. if(s[k]!=s[k-1]+1){
  11. flag=false;
  12. }
  13. }
  14. if(flag){
  15. return len;
  16. }
  17. }
  18. }
  19. return 1;
  20. }
  21. };

6182. 反转二叉树的奇数层

记录奇数层即可

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14. vector<int>v[100007];
  15. int id[100007];
  16. void dfs(TreeNode* tmp, int dep){
  17. if(dep&1){
  18. v[dep].push_back(tmp->val);
  19. }
  20. if(tmp->left){
  21. dfs(tmp->left,dep+1);
  22. dfs(tmp->right,dep+1);
  23. }
  24. }
  25. void gao(TreeNode* tmp, int dep){
  26. if(dep&1){
  27. tmp->val=v[dep][v[dep].size()-id[dep]-1];
  28. id[dep]++;
  29. }
  30. if(tmp->left){
  31. gao(tmp->left,dep+1);
  32. gao(tmp->right,dep+1);
  33. }
  34. }
  35. TreeNode* reverseOddLevels(TreeNode* root) {
  36. memset(id,0,sizeof(id));
  37. dfs(root,0);
  38. gao(root,0);
  39. return root;
  40. }
  41. };

6183. 字符串的前缀分数和

显然,分数即每个串与其他所有串的LCP之和。

按字典序排序后,类似滑动窗口做就行。

或者直接字典树。

  1. class Solution {
  2. public:
  3. int id[1007],lcp[1007];
  4. vector<int> sumPrefixScores(vector<string>& words) {
  5. vector<pair<string, int> >x;
  6. for(int i=0;i<words.size();i++){
  7. x.push_back({words[i],i});
  8. }
  9. sort(x.begin(),x.end());
  10. int n = x.size();
  11. for(int i=1;i<n;i++){
  12. int len=0;
  13. while(len<words[x[i].second].length() && len<words[x[i-1].second].length() && words[x[i].second][len]==words[x[i-1].second][len])len++;
  14. lcp[i]=len;
  15. }
  16. lcp[0]=1111;
  17. vector<int>ANS;
  18. for(int i=0;i<n;i++)ANS.push_back(0);
  19. for(int i=0;i<n;i++){
  20. int ans=0;
  21. int id=0;
  22. for(int j=0;j<n;j++){
  23. int a=x[i].second,b=x[j].second;
  24. id=min(id,lcp[j]);
  25. while((id>words[a].length()) || (id>words[b].length()))id--;
  26. while((id<words[a].length())&& (id<words[b].length()) && (words[a][id]==words[b][id])){
  27. id++;
  28. }
  29. while((id-1>=0) && (words[a][id-1]!=words[b][id-1])){
  30. id--;
  31. }
  32. ans+=id;
  33. }
  34. ANS[x[i].second]=ans;
  35. }
  36. return ANS;
  37. }
  38. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/860495
推荐阅读
相关标签
  

闽ICP备14008679号