zoukankan      html  css  js  c++  java
  • leetcode1625 执行操作后字典序最小的字符串

    思路:

    注意到给定字符串的长度是偶数,所以无论如何操作,所有可能的字符串也只有10 * 10 * n(n是字符串的长度)种,可以暴力枚举。

    实现:

     1 class Solution
     2 {
     3 public:
     4     string findLexSmallestString(string s, int a, int b)
     5     {
     6         int n = s.length();
     7         queue<string> q;
     8         q.push(s);
     9         unordered_set<string> st;
    10         string res = s; 
    11         while (!q.empty())
    12         {
    13             string tmp = q.front(); q.pop();
    14             if (tmp < res) res = tmp;
    15             string x = ""; 
    16             for (int i = 0; i < n; i++)
    17             {
    18                 if (i % 2 == 0) x += tmp[i];
    19                 else 
    20                 {
    21                     x += char('0' + (tmp[i] - '0' + a) % 10);
    22                 }
    23             }
    24             if (!st.count(x)) { st.insert(x); q.push(x); }
    25             string y = tmp.substr(n - b, b) + tmp.substr(0, n - b); 
    26             if (!st.count(y)) { st.insert(y); q.push(y) ;}
    27         }
    28         return res; 
    29     }
    30 };
  • 相关阅读:
    react native
    快速幂模板
    Java异常归纳
    Java环境变量配置
    过滤器
    cookie和session页面随机数和防止重复提交
    javabean&el&jstl
    servlet&jsp
    Tomcat和Servlet入门
    网络编程
  • 原文地址:https://www.cnblogs.com/wangyiming/p/14724000.html
Copyright © 2011-2022 走看看