我做了这道题只击败了全国37.5%但是知识点值得学习
var list = dic.OrderByDescending(t => t.Value);
这道题主要考察了包含关系:
提供c#代码如下:
public static string FrequencySort(string s)
{
StringBuilder res = new StringBuilder();
var dic = new Dictionary<char, int>();
for (int i = 0; i < s.Length; i++)
{
if (dic.ContainsKey(s[i]))
dic[s[i]]++;
else
dic[s[i]] = 1;
}
var list = dic.OrderByDescending(t => t.Value);
foreach (var item in list)
{
for (int i = 0; i < item.Value; i++)
{
res.Append(item.Key);
}
}
return res.ToString();
}
调用:
public static void Main(string[] args)
{
string str = "tree";
string c= FrequencySort(str);
Console.WriteLine(c);
Console.ReadKey();
}