拼接所有的字符串产生字典序最小的字符串
拼接所有的字符串产生字典序最小的字符串
http://www.nowcoder.com/questionTerminal/f1f6a1a1b6f6409b944f869dc8fd3381
往下翻,使用标准答案Pass这题吧。
错误答案:思想1,排序字符串组,值从低到高,合并,但是通不过测试。
# # # @param strs string字符串一维数组 the strings # @return string字符串 # class Solution: def minString(self, strs): strs.sort() return ''.join(strs)
标准答案:思想2,对任意两个字符串,按照 str1+str2 ≤ str2+str1方式排序。
用例通过率: 100.00% 运行时间: 179ms 占用内存: 7276KB。
#include <bits/stdc++.h> using namespace std; bool compare(string a, string b) { return (a + b) < (b + a); // 按照 str1+str2 ≤ str2+str1方式排序。 } class Solution { public: /** * * @param strs string字符串vector the strings * @return string字符串 */ string minString(vector<string> &strs) { // write code here if (strs.empty()) { return ""; } sort(strs.begin(), strs.end(), compare); string res = ""; for (int i = 0; i < strs.size(); ++i) { res += strs[i]; } return res; } };
我们来看看结果。
输入133个字符串的组。
omyxak nonfp gsfyk nbnpjku blbk y rf u v qzkgimg irpj uypbil ilmzhjq ribxv ama p ry p b hi fielz wdm pblyuzj qb eg hn ih updonk wvgdcgv fevu kal ypiepak owu nyb f k x g glvpcuy fqsao clmwwwn vbbaxf k uc qnmnqec ljp ojsvns c gbyyec kh syhcssa hxj aew iwcvs wipqpr p c ltlr c bni hyz b gzlmn uv vfewhvr qfmntlg ofej xurc hcgl riqw ad p acglhcb pzvhhoj fnjt jmdjrws dy lg wzdkvt mp c v yk dwb tve snuns x rptvuff bebi aqbzg ycnxn yxfna ezrk aixq e ggzolw vsz mtzsv jjpszez mt fepkoym wue iwcgq wrrmoau edkshxw dcpni lczih slgzic zaqwy hiaugfm ya bhrq dyydca yr d cutvf oelx aqvwxpa gzcswf xtveon lzviohc fv dszhzsw pdj bgcfg e cqmf nf lu iq njozyd xsdi dw
或者
["omyxak","nonfp","gsfyk","nbnpjku","blbk","y","rf","u","v","qzkgimg","irpj","uypbil","ilmzhjq","ribxv","ama","p","ry","p","b","hi","fielz","wdm","pblyuzj","qb","eg","hn","ih","updonk","wvgdcgv","fevu","kal","ypiepak","owu","nyb","f","k","x","g","glvpcuy","fqsao","clmwwwn","vbbaxf","k","uc","qnmnqec","ljp","ojsvns","c","gbyyec","kh","syhcssa","hxj","aew","iwcvs","wipqpr","p","c","ltlr","c","bni","hyz","b","gzlmn","uv","vfewhvr","qfmntlg","ofej","xurc","hcgl","riqw","ad","p","acglhcb","pzvhhoj","fnjt","jmdjrws","dy","lg","wzdkvt","mp","c","v","yk","dwb","tve","snuns","x","rptvuff","bebi","aqbzg","ycnxn","yxfna","ezrk","aixq","e","ggzolw","vsz","mtzsv","jjpszez","mt","fepkoym","wue","iwcgq","wrrmoau","edkshxw","dcpni","lczih","slgzic","zaqwy","hiaugfm","ya","bhrq","dyydca","yr","d","cutvf","oelx","aqvwxpa","gzcswf","xtveon","lzviohc","fv","dszhzsw","pdj","bgcfg","e","cqmf","nf","lu","iq","njozyd","xsdi","dw"]
算法思想1排序结果:
acglhcb ad aew aixq ama aqbzg aqvwxpa b b bebi bgcfg bhrq blbk bni c c c c clmwwwn cqmf cutvf d dcpni dszhzsw dw dwb dy dyydca e e edkshxw eg ezrk f fepkoym fevu fielz fnjt fqsao fv g gbyyec ggzolw glvpcuy gsfyk gzcswf gzlmn hcgl hi hiaugfm hn hxj hyz ih ilmzhjq iq irpj iwcgq iwcvs jjpszez jmdjrws k k kal kh lczih lg ljp ltlr lu lzviohc mp mt mtzsv nbnpjku nf njozyd nonfp nyb oelx ofej ojsvns omyxak owu p p p p pblyuzj pdj pzvhhoj qb qfmntlg qnmnqec qzkgimg rf ribxv riqw rptvuff ry slgzic snuns syhcssa tve u uc updonk uv uypbil v v vbbaxf vfewhvr vsz wdm wipqpr wrrmoau wue wvgdcgv wzdkvt x x xsdi xtveon xurc y ya ycnxn yk ypiepak yr yxfna zaqwy
算法思想2排序结果:
acglhcb ad aew aixq ama aqbzg aqvwxpa b b bebi bgcfg bhrq blbk bni c c c c clmwwwn cqmf cutvf dcpni d dszhzsw dwb dw dy dyydca edkshxw e e eg ezrk fepkoym fevu f fielz fnjt fqsao fv gbyyec g ggzolw glvpcuy gsfyk gzcswf gzlmn hcgl hiaugfm hi hn hxj hyz ih ilmzhjq iq irpj iwcgq iwcvs jjpszez jmdjrws kal kh k k lczih lg ljp ltlr lu lzviohc mp mt mtzsv nbnpjku nf njozyd nonfp nyb oelx ofej ojsvns omyxak owu pblyuzj pdj p p p p pzvhhoj qb qfmntlg qnmnqec qzkgimg rf ribxv riqw rptvuff ry slgzic snuns syhcssa tve uc updonk u uv uypbil vbbaxf vfewhvr vsz v v wdm wipqpr wrrmoau wue wvgdcgv wzdkvt xsdi xtveon xurc x x ya ycnxn yk ypiepak yr yxfna y zaqwy