• LeetCode 第 207 场周赛


    重新排列单词间的空格

    写了个比较暴力的做法,代码不优美。

     1 class Solution {
     2 public:
     3     string reorderSpaces(string text) {
     4         int c1 = 0;
     5         int l = -1, r = -1;
     6         vector<string> ans;
     7         for (int i = 0; i < text.size(); i++) {
     8             if (text[i] == ' ') {
     9                 if (l != -1) {
    10                     string s = text.substr(l, r - l + 1);
    11                     ans.push_back(s);
    12                 }     
    13                 l = -1;
    14                 c1++;
    15             }
    16             else {
    17                 if (l == -1) l = i;
    18                 r = i;
    19             }
    20         }
    21         if (l != -1) {
    22             ans.push_back(text.substr(l, r - l + 1));
    23         }
    24         string res = "";
    25         if (ans.size() == 1) {
    26             res = ans[0];
    27             for (int i = 1; i <= c1; i++) res = res + " ";
    28             return res; 
    29         }
    30         int p = c1 / (ans.size() - 1);
    31         for (int i = 0; i < ans.size(); i++) {
    32             res = res + ans[i];
    33             if (i != ans.size() - 1) {
    34                 for (int j = 1; j <= p; j++) res = res + " ";
    35             }
    36         }
    37         int cpp = (c1 % (ans.size() - 1));
    38         for (int i = 1; i <= cpp; i++) res = res + " ";
    39         return res;
    40     }
    41 };
    View Code

    拆分字符串使唯一子字符串的数目最大

    DFS暴力跑,到每个位置枚举所有能够的长度

     1 class Solution {
     2 public:
     3     set<string> se;
     4     int ans = 0;
     5     int Max(int a, int b) {
     6         return a > b ? a : b;
     7     }
     8     void dfs(string s, int pos) {
     9         
    10         for (int i = 1; i <= s.size() - pos; i++) {
    11             string str = s.substr(pos, i);
    12             if (se.find(str) == se.end()) {
    13                 se.insert(str);
    14                 ans= Max(ans, se.size());
    15                 dfs(s, pos + i);
    16                 se.erase(str);
    17             }
    18         }
    19 
    20     }
    21     int maxUniqueSplit(string s) {
    22         dfs(s, 0);
    23         return ans;
    24     }
    25 };
    View Code

    矩阵的最大非负积

     1 class Solution {
     2 public:
     3     const long long mod = 1000000007;
     4     int n, m;
     5     long long ans = -1;
     6     bool vis[20][20];
     7     long long Max(long long a, long long b) {
     8         return a > b ? a : b;
     9     }
    10     void dfs(int x, int y, vector<vector<int>>& g, long long val) {
    11         if (val == 0) {
    12             ans = Max(ans, 0);
    13             return ;
    14         }
    15         if (x == n - 1 && y == m - 1) {
    16             ans =Max(ans, val);
    17         }
    18         if (x + 1 < n) {
    19             dfs(x + 1, y, g, (val * g[x + 1][y]));
    20         }
    21         if (y + 1 < m) {
    22             dfs(x, y + 1, g, (val * g[x][y + 1]));
    23         }
    24     }
    25     int maxProductPath(vector<vector<int>>& grid) {
    26         n = grid.size();
    27         m = grid[0].size();
    28         dfs(0, 0, grid, grid[0][0]);
    29         return (int) (ans % mod);
    30     }
    31 };
    View Code

    连通两组点的最小成本

    状压DP,不会做了,待补。

  • 相关阅读:
    PKU 3984 迷宫问题
    九度 1341 艾薇儿的演唱会
    九度 1335
    SDUT 1198 鞍点计算
    POJ 1363 Rails
    SDUT 1570 C旅行
    HDU 1042 N!
    SDUT 1568 俄罗斯方块
    HDU 1257 最少拦截系统
    POJ 3750 小孩报数问题
  • 原文地址:https://www.cnblogs.com/pavtlly/p/13881898.html
Copyright © 2020-2023  润新知