求经典算法,多多指教

无聊的一个人啊 2008-12-09 11:06:48
求算一字符串中独立字符的个数。
如string str="jsffff";
其中
j,1
s,1
f,4
...全文
68 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
多多指教
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace 字符统计
{
class Program
{
static void Main(string[] args)
{
string myString = "uhifhiohskljhdsafipo喔喔 哈哈 你好啊";
Dictionary<char, int> myDictionary = MyMethod.GetInfoFormString(myString);
foreach(KeyValuePair<char,int> kvp in myDictionary)
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}



}

}
public static class MyMethod
{
public static Queue<char> GetOnlyCharFromString(string str)
{
Queue<char> myQueue = new Queue<char>();
for (int i = 0; i < str.Length; i++)
{
if (myQueue.Contains(str[i]) == false)
myQueue.Enqueue(str[i]);
}
return myQueue;
}
public static Dictionary<char,int> GetInfoFormString(string str)
{
Queue<char> myQueue = GetOnlyCharFromString(str );
Dictionary<char,int> myDictionary=new Dictionary<char,int>();
int oldQueueCount = myQueue.Count;//记录下它原有的量;
for(int i=0;i<oldQueueCount ;i++)
{
char myChar=myQueue.Dequeue();
int num=0;
for(int j=0;j<str.Length;j++)
{
if(str[j]==myChar)
num++;
}
myDictionary.Add(myChar,num);
}
return myDictionary;
}

}

}


111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧