我不知道要使用哪种迭代方法来提高效率,这里我列出了我尝试过的解决方案。有没有其他的迭代方式,我的意思是任何特殊的方法或方式?
方法一:
这里我使用了两个 for 循环,所以迭代进行了 2N 次
public void CountChar()
{
String s = Ipstring();
int[] counts = new int[256];
char[] c = s.ToCharArray();
for (int i = 0; i < c.Length; ++i)
{
counts[c[i]]++;
}
for (int i = 0; i < c.Length; i++)
{
Console.WriteLine(c[i].ToString() + " " + counts[c[i]]);
Console.WriteLine();
}
}
方法二:
public void CountChar()
{
_inputWord = Ipstring();
char[] test = _inputWord.ToCharArray();
char temp;
int count = 0, tcount = 0;
Array.Sort(test);
int length = test.Length;
temp = test[0];
while (length > 0)
{
for (int i = 0; i < test.Length; i++)
{
if (temp == test[i])
{
count++;
}
}
Console.WriteLine(temp + " " + count);
tcount = tcount + count;
length = length - count;
count = 0;
if (tcount != test.Length)
temp = test[tcount];
//atchutharam. aaachhmrttu
}
}
方法三:
public void CountChar()
{
int indexcount = 0;
s = Ipstring();
int[] count = new int[s.Length];
foreach (char c in s)
{
Console.Write(c);
count[s.IndexOf(c)]++;
}
foreach (char c in s)
{
if (indexcount <= s.IndexOf(c))
{
Console.WriteLine(c);
Console.WriteLine(count[s.IndexOf(c)]);
Console.WriteLine("");
}
indexcount++;
////atchutharam
}
}