NC85. 拼接所有的字符串产生字典序最小的字符串
描述
示例1
输入:
["abc","de"]
输出:
"abcde"
示例2
输入:
["a","a","b"]
输出:
"aab"
C++ 解法, 执行用时: 40ms, 内存消耗: 6692KB, 提交时间: 2021-06-29
static const auto io_sync_off = []() { // turn off sync std::ios::sync_with_stdio(false); // untie in/out streams std::cin.tie(nullptr); return nullptr; }(); class Solution { public: /** * * @param strs string字符串vector the strings * @return string字符串 */ string minString(vector<string>& strs) { // write code here sort(strs.begin(), strs.end(), [](const string &lhs, const string &rhs){ int i = -1, len_l = lhs.size(), len_r = rhs.size(), len = len_l+len_r-2; while(++i <= len) { char ch_lr = i<len_l ? lhs[i] : rhs[ i-len_l ]; char ch_rl = i<len_r ? rhs[i] : lhs[ i-len_r ]; if(ch_lr < ch_rl) return true; if(ch_lr > ch_rl) return false; } return false; }); string res; for(string &str : strs) res += str; return res; } };
C++ 解法, 执行用时: 43ms, 内存消耗: 6788KB, 提交时间: 2022-05-08
static const auto io_sync_off = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); return nullptr; } (); class Solution { public: string minString(vector<string>& strs) { sort(strs.begin(), strs.end(), [](const string & lhs, const string & rhs) { int i = -1, len_l = lhs.size(), len_r = rhs.size(), len = len_l + len_r - 2; while (++i <= len) { char ch_lr = i < len_l ? lhs[i] : rhs[ i - len_l ]; char ch_rl = i < len_r ? rhs[i] : lhs[ i - len_r ]; if (ch_lr < ch_rl) return true; if (ch_lr > ch_rl) return false; } return false; }); string res; for (string& str : strs) res += str; return res; } };
C++ 解法, 执行用时: 43ms, 内存消耗: 6828KB, 提交时间: 2022-04-07
static const auto io_sync_off = []() { // turn off sync std::ios::sync_with_stdio(false); // untie in/out streams std::cin.tie(nullptr); return nullptr; }(); class Solution { public: /** * * @param strs string字符串vector the strings * @return string字符串 */ static bool cmp(const string &a, const string &b) { for (int i = 0; i < a.size() + b.size(); i++) { char c1 = i < a.size() ? a[i] : b[i - a.size()]; char c2 = i < b.size() ? b[i] : a[i - b.size()]; if (c1 != c2) { return c1 < c2; } } return false; } string minString(vector<string>& strs) { // write code here sort(strs.begin(), strs.end(), cmp); string str; for (auto a : strs) { str += a; } return str; } };
C++ 解法, 执行用时: 46ms, 内存消耗: 6748KB, 提交时间: 2021-09-16
class Solution { public: /** * * @param strs string字符串vector the strings * @return string字符串 */ static bool cmp(const string & str1, const string & str2) { int len1 = str1.size(); int len2 = str2.size(); for(int i = 0; i < len1 + len2; i++) { char c1 = i < len1 ? str1[i] : str2[i - len1]; char c2 = i < len2 ? str2[i] : str1[i - len2]; if(c1 != c2) return c1 < c2; } return false; } string minString(vector<string>& strs) { // write code here sort(strs.begin(), strs.end(), cmp); string res; for(auto i: strs) { res += i; } return res; } };
C++ 解法, 执行用时: 46ms, 内存消耗: 6788KB, 提交时间: 2022-05-30
static const auto io_sync_off = []() { // turn off sync std::ios::sync_with_stdio(false); // untie in/out streams std::cin.tie(nullptr); return nullptr; }(); class Solution { public: /** * * @param strs string字符串vector the strings * @return string字符串 */ string minString(vector<string>& strs) { // write code here sort(strs.begin(),strs.end(),[](const string &ls,const string & rs){ int i = -1,ll = ls.size(),lr = rs.size(),l = ll + lr; while(++i <= l) { char cl = i < ll ? ls[i] : rs[i-ll]; char cr = i < lr ? rs[i] : ls[i-lr]; if(cl < cr) return true; if(cl > cr) return false; } return false; }); string res; for(string &s : strs) res += s; return res; } };