算法大比拼(看看谁是真真的高手)?(java C# C++ C的高手请进)

无聊的一个人啊 2009-01-13 11:26:16
加精
求算一任意长度字符串中不同的字符以及它的个数?(设计一个漂亮的算法,快而优美)
如在 字符串"abcdefgabc"中求算出的结果是
a,2
b,2
c,2
d,1
e,1
f,1
g,1

以一个普通的算法开个头吧.
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;
}

}
...全文
9701 464 打赏 收藏 转发到动态 举报
写回复
用AI写文章
464 条回复
切换为时间正序
请发表友善的回复…
发表回复
ktei2008 2011-03-25
  • 打赏
  • 举报
回复
这破帖子都能成为精品?
fesly 2011-03-17
  • 打赏
  • 举报
回复
我最喜歡LINQ了
lbc2247117 2011-03-11
  • 打赏
  • 举报
回复
哎~~~ 我悲剧了,4楼的我看不懂
諾临風 2010-07-22
  • 打赏
  • 举报
回复
q734193444 2010-07-22
  • 打赏
  • 举报
回复

using System;
namespace Text
{
class YouMother
{
}
}

q734193444 2010-07-22
  • 打赏
  • 举报
回复

using System;
namespace Text
{
class YouMother
{
}
}
chunquanwang 2010-07-22
  • 打赏
  • 举报
回复
大虾不少啊!
louis_vivien 2010-07-12
  • 打赏
  • 举报
回复
学习 学习
BoyBaiDui 2010-07-08
  • 打赏
  • 举报
回复

string s= "abcdefg";
for(int i=0;i<s.length;i++)
{
convert.WriteLine(s.char(i));
}
rowzixin 2010-07-08
  • 打赏
  • 举报
回复
很牛哦
motnil 2010-07-06
  • 打赏
  • 举报
回复

def count(string):
s = list(set(string))
print s
length = len(s)
for c in s:
b = string.count(c)
print "there is %d %c in the string" % (b,c)
if __name__ == "__main__":
string = raw_input("please enter a string:\n")
count(string)
hebets 2010-05-31
  • 打赏
  • 举报
回复
mark一下展位有空写一个
fengxingphp 2010-05-20
  • 打赏
  • 举报
回复
\
string str = "abcdefgabc";
var result = from c in str
group c by c into g
select g;
foreach (var group in result)
Console.WriteLine(group.Key + "," + group.Count())……
\
jenny0810 2010-02-05
  • 打赏
  • 举报
回复
标记
honey1314 2010-01-12
  • 打赏
  • 举报
回复
受教了
cppConst 2010-01-10
  • 打赏
  • 举报
回复
注:上面代码忽略 0 的计数的正确性.
cppConst 2010-01-10
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

void Handle(const char* const szStr)
{
if ( NULL == szStr ) return ;

const int conAsc = 128;
int nCount[conAsc] = {0};
char* pCur = (char*)szStr;
char cCur = *szStr;

while ( cCur )
{
nCount[cCur]++;
cCur = *++pCur;
}

for (int i = 0; i < conAsc; i++)
{
cout<< (char)i << "'s count = " << nCount[i] <<"\n";
}

cout<<endl;
}
zhxi55 2009-11-19
  • 打赏
  • 举报
回复

"abcdefgabc"
.GroupBy(c => c)
.Select(g => String.Format("{0}, {1}", g.Key, g.Count()))
.ToList<string>().ForEach(p => Console.WriteLine(p));
zhxi55 2009-11-19
  • 打赏
  • 举报
回复

"abcdefgabc"
.GroupBy(c => c)
.Select(g => String.Format("{0}, {1}", g.Key, g.Count()))
.ToList<string>().ForEach(p => Console.WriteLine(p));
zhxi55 2009-11-19
  • 打赏
  • 举报
回复
"abcdefgabc"
.GroupBy(c => c)
.Select(g => String.Format("{0}, {1}", g.Key, g.Count()))
.ToList<string>().ForEach(p => Console.WriteLine(p));
加载更多回复(435)

110,536

社区成员

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

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

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