日常学习0105

1. leetcode

  • 周赛170
    • 5303:解码字母到整数映射
      • 题目链接
        https://leetcode-cn.com/contest/weekly-contest-170/problems/decrypt-string-from-alphabet-to-integer-mapping/
      • 解析:简单字符串处理,判断‘0’ - ‘2’ 的时候是代指个位数和双位数(通过‘#’作判断)即可
      • code
      class Solution {
      public:
      string freqAlphabets(string s) {
        int num = 0;
        int p = 0;
        int size = s.size();
        string res = {};
        while(p < size){
            char c = s[p];
            if(c >= '3' || (p + 2 < size && s[p + 2] != '#') || (p + 2 >= size)){
                res.push_back(c - '0' + 'a' - 1);
                p += 1;
                continue;
            }
            
            num = ((c - '0') * 10 + s[p + 1] - '0') - 1;
            res.push_back(num + 'a');
            p += 3;
        }
        
          return res;
        }
      };
      
    • 5304:子数组异或查询
      • 题目链接:https://leetcode-cn.com/contest/weekly-contest-170/problems/xor-queries-of-a-subarray/
      • 解析:看数组长度,大概可以知道,这个题目要求复杂在以下,用一个数组sum保存以索引(sum[i + 1])做结尾的数组异或结果, 对于区间[i, j],可以利用a = a ^ b ^ b 的性质, 可以得到其结果为sum[i + 1] ^ sum[j]
      • code
      class Solution {
        public:
            vector xorQueries(vector& arr, vector>& queries) {
               int size = arr.size();
               int qsize = queries.size();
               vector xors(size + 1, 0);
              for(int i = 0; i < size; i++){
                  xors[i + 1] = arr[i] ^ xors[i];
                }
      
                vector res(qsize, 0);
                for(int i = 0; i < qsize; i++){
                    vector query = queries[i];
                  res[i] = xors[query[1] + 1] ^ xors[query[0]];
                }
      
              return res;
      
              }
          };
      
  • 5305:获取你好友已观看的视频
    • 题目链接:
      https://leetcode-cn.com/contest/weekly-contest-170/problems/get-watched-videos-by-your-friends/
    • 解析:这题感觉没什么技巧,先建立无向图的邻接表,然后获得第level层的所有节点,然后将视频去重,计数,排序。
    • code
    class Solution {
    public:
       vector watchedVideosByFriends(vector>& watchedVideos, vector>& friends, int id, int level) {
           size_t n = friends.size();
           vector> graph ={n, vector()};
           vector visited(n, 0);
     
           for(int i = 0; i < n; i++){
               vector f = friends[i];
               for(int node: f){
                   graph[i].push_back(node);
                   graph[node].push_back(i);
             }
           }
     
         queue q = {};
         q.push(id);
         visited[id] = 1;
         int cur_level = 0;
         vector peope = {};
     
         while(q.size() && cur_level < level){
             peope = {};
             int size = q.size();
             for(int i = 0; i < size; i++){
                 int cur = q.front();
                 q.pop();
                 vector& nexts = graph[cur];
                 for(int next : nexts){
                     if(visited[next])
                         continue;
                     visited[next] = 1;
                     q.push(next);
                     peope.push_back(next);
                 }
             }
             cur_level++;
         }
     
         if(cur_level < level)
             return {};
     
         unordered_map record = {};
         for(int fid : peope){
             vector& videos = watchedVideos[fid];
             for(string video : videos)
                 record[video]++;
         }
     
         vector> unordered_res = {record.begin(), record.end()};
         sort(unordered_res.begin(), unordered_res.end(), [&](pair& p1, pair& p2){
            return p1.second == p2.second ? p1.first  < p2.first : p1.second < p2.second; 
         });
     
         vector res = {};
     
         for(pair& video : unordered_res)
             res.push_back(video.first);
     
     
         return res;
     
     }
    };
    
  • 5306:让字符串成为回文串的最少插入次数
    • 题目链接:
      https://leetcode-cn.com/contest/weekly-contest-170/problems/minimum-insertion-steps-to-make-a-string-palindrome/
    • 解析
      最长公共子串变形, 如何想到了,我比赛,感觉像, 写了,然后A了,我其实没细想逻辑。
  • code
```C++
class Solution {
public:
    int minInsertions(string s) {
        string p = {s.rbegin(), s.rend()};
        size_t size = s.size();
        vector> dp(size, vector(size, 0));
    
        for(int i = 0; i < size; i++){
            for(int j = 0; j < size; j++){
                int left  = i - 1 >= 0 ? dp[i - 1][j] : 0;
                int right = j - 1 >= 0 ? dp[i][j - 1] : 0;
                int pre = i - 1 >= 0 && j - 1 >= 0 ? dp[i - 1][j - 1] : 0;
                if(s[i] == p[j])
                    dp[i][j] =pre + 1;
                else
                    dp[i][j] = max(left, right);
            }
        }
    
        int common_size = dp[size - 1][size - 1];
    
        return size - common_size;
    }
};
```

你可能感兴趣的:(日常学习0105)