本例实现一个函数用于返回输入字符串里出现次数最多的字符。
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace StringTest
{
class Program
{
static void Main(string[] args)
{
string d = FindMaxChar("ABCDADCCCA");
}
private static string FindMaxChar(string value)
{
//compute the numbers of each char and store it in hashTable
Hashtable ht = new Hashtable();
for (int i = 0; i < value.Length; i++)
{
if (!ht.ContainsKey(value.Substring(i, 1)))
{
ht.Add(value.Substring(i, 1), GetCount(value, value.Substring(i, 1)));
}
}
int temp;
string maxStr = string.Empty;
List<int> lt = new List<int>();
foreach (System.Collections.DictionaryEntry objDE in ht)
{
lt.Add(Convert.ToInt32(objDE.Value));
}
//get the max Num.
for (int i = 0; i < lt.Count - 1; i++)
{
if (lt[i] > lt[i + 1])
{
temp = lt[i + 1];
lt[i + 1] = lt[i];
lt[i] = temp;
}
}
//get the max char.
foreach (System.Collections.DictionaryEntry objDE in ht)
{
if (objDE.Value.ToString() == lt[lt.Count - 1].ToString())
{
maxStr = objDE.Key.ToString();
}
}
return maxStr;
}
//get the count of char.
private static int GetCount(string orignal, string strTarget)
{
int count = 0;
for (int i = 0; (i = orignal.IndexOf(strTarget, i)) >= 0; i++)
{
count++;
}
return count;
}
}
}
本例的时间复杂度比较大,因此,有很大的优化空间。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace StringTest
{
class Program
{
static void Main(string[] args)
{
string d = FindMaxChar("ABCDADCCCA");
}
private static string FindMaxChar(string value)
{
//compute the numbers of each char and store it in hashTable
Hashtable ht = new Hashtable();
for (int i = 0; i < value.Length; i++)
{
if (!ht.ContainsKey(value.Substring(i, 1)))
{
ht.Add(value.Substring(i, 1), GetCount(value, value.Substring(i, 1)));
}
}
int temp;
string maxStr = string.Empty;
List<int> lt = new List<int>();
foreach (System.Collections.DictionaryEntry objDE in ht)
{
lt.Add(Convert.ToInt32(objDE.Value));
}
//get the max Num.
for (int i = 0; i < lt.Count - 1; i++)
{
if (lt[i] > lt[i + 1])
{
temp = lt[i + 1];
lt[i + 1] = lt[i];
lt[i] = temp;
}
}
//get the max char.
foreach (System.Collections.DictionaryEntry objDE in ht)
{
if (objDE.Value.ToString() == lt[lt.Count - 1].ToString())
{
maxStr = objDE.Key.ToString();
}
}
return maxStr;
}
//get the count of char.
private static int GetCount(string orignal, string strTarget)
{
int count = 0;
for (int i = 0; (i = orignal.IndexOf(strTarget, i)) >= 0; i++)
{
count++;
}
return count;
}
}
}
经过和偶一朋友(交流)给出进一步的优化方法如下,这次代码的时间复杂度大大的降低
Code
private static string FindMaxChar(string value)
{
Hashtable ht = new Hashtable();
for (int i = 0; i < value.Length; i++)
{
if (ht[value[i]] == null)
ht.Add(value[i], 1);
else
ht[value[i]] = int.Parse(ht[value[i]].ToString()) + 1;
}
DictionaryEntry maxItem = new DictionaryEntry();
maxItem.Value = 0;
foreach (DictionaryEntry item in ht)
{
if (Int32.Parse(maxItem.Value.ToString()) < Int32.Parse(item.Value.ToString()))
maxItem = item;
}
return maxItem.Key.ToString();
}
private static string FindMaxChar(string value)
{
Hashtable ht = new Hashtable();
for (int i = 0; i < value.Length; i++)
{
if (ht[value[i]] == null)
ht.Add(value[i], 1);
else
ht[value[i]] = int.Parse(ht[value[i]].ToString()) + 1;
}
DictionaryEntry maxItem = new DictionaryEntry();
maxItem.Value = 0;
foreach (DictionaryEntry item in ht)
{
if (Int32.Parse(maxItem.Value.ToString()) < Int32.Parse(item.Value.ToString()))
maxItem = item;
}
return maxItem.Key.ToString();
}