如在 字符串"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;
}
}
464 个解决方案
#1
不知道效率怎么样
但是这是一个思路
你可以用正则表达式去匹配
从ASCII为0的到128
以下是.NET下正则表达式的使用方法
http://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex(VS.80).aspx
// Define a regular expression for repeated words.
Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
// Define a test string.
string text = "The the quick brown fox fox jumped over the lazy dog dog.";
// Find matches.
MatchCollection matches = rx.Matches(text);
// Report the number of matches found.
Console.WriteLine("{0} matches found.", matches.Count);
// Report on each match.
foreach (Match match in matches)
{
string word = match.Groups["word"].Value;
int index = match.Index;
Console.WriteLine("{0} repeated at position {1}", word, index);
}
但是这是一个思路
你可以用正则表达式去匹配
从ASCII为0的到128
以下是.NET下正则表达式的使用方法
http://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex(VS.80).aspx
// Define a regular expression for repeated words.
Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
// Define a test string.
string text = "The the quick brown fox fox jumped over the lazy dog dog.";
// Find matches.
MatchCollection matches = rx.Matches(text);
// Report the number of matches found.
Console.WriteLine("{0} matches found.", matches.Count);
// Report on each match.
foreach (Match match in matches)
{
string word = match.Groups["word"].Value;
int index = match.Index;
Console.WriteLine("{0} repeated at position {1}", word, index);
}
#2
关注效果.
string _Text = "abcdefgabc";
SortedList<char, int> _Value = new SortedList<char, int>();
int _Count = _Text.Length;
for (int i = 0; i != _Count; i++)
{
if (_Value.IndexOfKey(_Text[i]) == -1)
{
_Value.Add(_Text[i], 1);
}
else
{
_Value[_Text[i]]++;
}
}
foreach(char _Char in _Value.Keys)
{
MessageBox.Show(_Char+","+ _Value[_Char].ToString());
}
string _Text = "abcdefgabc";
SortedList<char, int> _Value = new SortedList<char, int>();
int _Count = _Text.Length;
for (int i = 0; i != _Count; i++)
{
if (_Value.IndexOfKey(_Text[i]) == -1)
{
_Value.Add(_Text[i], 1);
}
else
{
_Value[_Text[i]]++;
}
}
foreach(char _Char in _Value.Keys)
{
MessageBox.Show(_Char+","+ _Value[_Char].ToString());
}
#3
#4
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());
#5
ding
#6
ding
#7
==哈
晚上写一个
晚上写一个
#8
string GetCharCount(string text)
{
string result=string.Empty;
int len=text.Length;
while(len>0)
{
string temp=text[0].ToString();
text=text.Replace(temp,"");
int count=len-text.Length;
result+=temp+","+count+"\r\n";
len=text.Length;
}
return result;
}
#9
支持一个~替换了多少个字~~一次出一种字符~~~学习了
#10
收藏一下。
#11
强!
#12
int[] c = new int[256];
for (int i = 0; i < 256; i++)
{
c[i] = 0;
}
foreach (char e in str)
{
int i = (int)e;
c[i] ++ ;
}
for (int i = 0; i < 256; i++)
{
if (c[i] != 0)
{
console.write((char)i + "," + c[i].ToString() + "\r\n");
}
}
for (int i = 0; i < 256; i++)
{
c[i] = 0;
}
foreach (char e in str)
{
int i = (int)e;
c[i] ++ ;
}
for (int i = 0; i < 256; i++)
{
if (c[i] != 0)
{
console.write((char)i + "," + c[i].ToString() + "\r\n");
}
}
#13
这样多简单,其实核心代码就三行:
foreach (char e in str)
{
int i = (int)e;
c[i] ++ ;
}
把字符串里每一个字符转换成对应的int值,然后将int数组里对应的那个值++
foreach (char e in str)
{
int i = (int)e;
c[i] ++ ;
}
把字符串里每一个字符转换成对应的int值,然后将int数组里对应的那个值++
#14
先占位
#15
顶
#16
C++ Code
int main(void)
{
string str = "abcdeffasljdlfjaslflsadjflkasdfjlsadlfgabc";
sort(str.begin(), str.end()); //先排序
cout<<str<<endl;
char c;
int count;
int i = 0;
while(i < str.length()){
c = str[i];
count = 1;
while(c == str[++i]){
count++;
}
cout<<c<<","<<count<<endl;
}
return 0;
}
int main(void)
{
string str = "abcdeffasljdlfjaslflsadjflkasdfjlsadlfgabc";
sort(str.begin(), str.end()); //先排序
cout<<str<<endl;
char c;
int count;
int i = 0;
while(i < str.length()){
c = str[i];
count = 1;
while(c == str[++i]){
count++;
}
cout<<c<<","<<count<<endl;
}
return 0;
}
#17
学习了...
#18
declare @a varchar(max)
set @a='abcdefgabc'
declare @b table(s varchar(1))
declare @n int
declare @i int
set @i=1
set @n=len(@a)
set rowcount @n
while @i<=@n
begin
insert into @b select substring(@a,@i,1)
set @i=@i+1
end
set rowcount 0
select count(s),s from @b group by s
#19
declare @a varchar(max)
set @a='abcdefgabc'
declare @b table(s varchar(1))
declare @n int
declare @i int
set @i=1
set @n=len(@a)
set rowcount @n
while @i<=@n
begin
insert into @b select substring(@a,@i,1)
set @i=@i+1
end
set rowcount 0
select count(s),s from @b group by s
/*结果
2 a
2 b
2 c
1 d
1 e
1 f
1 g
*/
#20
string a="abcdefgabc";
int i=a.Length;
while(i>0)
{
a=a.Replace(a[0],'');
Console.WriteLine(i-a.Length);
}
#21
学习
#22
强烈顶!!!!!!!!!!!!
#23
又见Linq,简洁得令人难以置信。
另外,用List<T>应该也是一个不错的选择:
List<char> list = new List<char>(text.ToCharArray());
#24
void Calculate(const string &str,char *res)
{
if(res==NULL)
return;
int length = str.length();
for(int i=0;i<length;i++)
{
res[str[i]]++;
}
}
void main()
{
string str = "sfsfsdfasrq24234we:~!@#.....$%%S^ldf";
char res[200];
ZeroMemory(res,sizeof(res));
Calculate(str,res);
int total = 0;
for(int i=0;i<200;i++)
{
if(res[i]!=0)
{
cout<<char(i)<<": "<< (int)res[i]<<endl;
total += (int)res[i];
}
}
cout<<"total="<<total<<" strLength="<<str.length()<<endl;
}
#25
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication67
{
class Program
{
static void Main(string[] args)
{
string a = "abcdefgabc";
int i = a.Length;
while (i > 0)
{
Console.WriteLine(a[0].ToString()+":");
a = a.Replace(a[0].ToString(), "");
Console.Write(i - a.Length);
Console.WriteLine();
i=a.Length;
}
}
}
}
#26
php:
$str = "abcdefgabc";
for(;$i<strlen($str);$i++)$astr[]=$str[$i];
print_r(array_count_values($astr));
#27
修改成StringBuilder 要好一点因为
对 a 进行 重赋值 是要重新分配内存的
如果字符 非常大,就是用StringBuilder
#28
都是强人啊
#29
procedure aa(s: string;l: TStrings);
var
c: array[0..255] of integer;
i: integer;
begin
for i := 0 to 255 do
c[i] := 0;
for i := 1 to pinteger(integer(s) - 4)^ do
inc(c[byte(s[i])]);
for i := 0 to 255 do
if c[i] > 0 then l.Add(char(i) +'|'+IntToStr(i)+'|'+ IntToStr(c[i]))
end;
#30
学习一下
#31
凑个热闹, 来个JavaScript的...
var ret = {};
var str = "abcdefgabc";
for(var i=0; i<str.length; i++){
var char = str.charAt(i);
ret[char] = ret[char] ? ret[char] + 1 : 1;
};
for(var pro in ret){
document.write(pro.toString() + " : " + ret[pro] + "<br/>");
}
#32
php的实现方法:
PHP_FUNCTION(array_count_values)
{
zval **input, /* Input array */
**entry; /* An entry in the input array */
zval **tmp;
HashTable *myht;
HashPosition pos;
/* Get arguments and do error-checking */
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &input) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (Z_TYPE_PP(input) != IS_ARRAY) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The argument should be an array");
return;
}
/* Initialize return array */
array_init(return_value);
/* Go through input array and add values to the return array */
myht = Z_ARRVAL_PP(input);
zend_hash_internal_pointer_reset_ex(myht, &pos);
while (zend_hash_get_current_data_ex(myht, (void **)&entry, &pos) == SUCCESS) {
if (Z_TYPE_PP(entry) == IS_LONG) {
if (zend_hash_index_find(Z_ARRVAL_P(return_value),
Z_LVAL_PP(entry),
(void**)&tmp) == FAILURE) {
zval *data;
MAKE_STD_ZVAL(data);
Z_TYPE_P(data) = IS_LONG;
Z_LVAL_P(data) = 1;
zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_PP(entry), &data, sizeof(data), NULL);
} else {
Z_LVAL_PP(tmp)++;
}
} else if (Z_TYPE_PP(entry) == IS_STRING) {
if (zend_hash_find(Z_ARRVAL_P(return_value),
Z_STRVAL_PP(entry),
Z_STRLEN_PP(entry)+1,
(void**)&tmp) == FAILURE) {
zval *data;
MAKE_STD_ZVAL(data);
Z_TYPE_P(data) = IS_LONG;
Z_LVAL_P(data) = 1;
zend_hash_update(Z_ARRVAL_P(return_value), Z_STRVAL_PP(entry), Z_STRLEN_PP(entry) + 1, &data, sizeof(data), NULL);
} else {
Z_LVAL_PP(tmp)++;
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can only count STRING and INTEGER values!");
}
zend_hash_move_forward_ex(myht, &pos);
}
}
#33
同意用List。
另外,空间换时间算法也是可以的,也就是去除字符数组里面重复的字符对象。
另外,空间换时间算法也是可以的,也就是去除字符数组里面重复的字符对象。
#34
mark,学习了
#35
终于看到一个像算法的...
什么泛型,枚举器,linq...跟手工的哈希表比起来统统都是渣!
#36
C的,TCPL的序章中的一个思路。
#include <stdio.h>
int main(void)
{
char * s = "abcdefgabc";
int c[256] = { 0 }, i;
while(*s != '\0')
c[*s++]++;
for(i = 0; i < 256; ++i)
if(c[i])
printf("%c,%d\n", i, c[i]);
return 0;
}
#37
public class CountChar
{
static UInt16[,] ASCMapping = new UInt16[128,2];
private static void initMapping()
{
for (UInt16 i = 0; i < 128; i++)
{
ASCMapping[i, 0] = i;
}
}
private static void PrintResult()
{
for (UInt16 i = 0; i < 128; i++)
{
if (ASCMapping[i, 1] > 0)
Console.WriteLine(string.Format("{0},{1}",(char)ASCMapping[i, 0], ASCMapping[i, 1]));
}
}
public static void test()
{
initMapping();
string s = "dad1dsad1esa12da";
for (int i = 0; i < s.Length; i++)
{
ASCMapping[(UInt16)s[i],1]++;
}
PrintResult();
}
}
楼主题目为字符串,但这段代码只支持ASCII字符,可扩展,并且适合长字符串的统计
{
static UInt16[,] ASCMapping = new UInt16[128,2];
private static void initMapping()
{
for (UInt16 i = 0; i < 128; i++)
{
ASCMapping[i, 0] = i;
}
}
private static void PrintResult()
{
for (UInt16 i = 0; i < 128; i++)
{
if (ASCMapping[i, 1] > 0)
Console.WriteLine(string.Format("{0},{1}",(char)ASCMapping[i, 0], ASCMapping[i, 1]));
}
}
public static void test()
{
initMapping();
string s = "dad1dsad1esa12da";
for (int i = 0; i < s.Length; i++)
{
ASCMapping[(UInt16)s[i],1]++;
}
PrintResult();
}
}
楼主题目为字符串,但这段代码只支持ASCII字符,可扩展,并且适合长字符串的统计
#38
喜欢用C#方法做完!
#39
qiang a ding shang ba
#40
qiang a ding shang ba
#41
应该是这种算法 如果不考率 空间大小 毕竟现在的程序空间很宽余了 u盘都上G了
就是把字符ascII 当数组下标用 256个桶是哪个桶子就在那个桶加1
#42
写个java的,不过限于ascii码字符
测试代码:
public static int[] func(String input) {
int[] result = new int[128];
for (int i = 0, l = input.length(); i < l; i++) {
result[input.charAt(i)]++;
}
return result;
}
测试代码:
public void test() {
int[] result = func("abcdefgabc");
for (int i = 0; i < 128; i++) {
if (result[i] != 0) {
System.out.println((char)i + "," + result[i]);
}
}
}
#43
哇!来晚了,方法挺多的哈!
#44
哈希算法就行了
#45
编译后只有一百多字节, 标准C
const char str[] = "fdsaasdfhjklpouop[gtyuusrd65987676067876^&*(&*^&^(*tyiu%^&*(^&%";
char result[128];
for( int i = 0; i < sizeof(result); result[i++] = 0 );
for( int i = 0; i < sizeof(str)-1; result[str[i++]]++ );
int j = 0;
for( int i = 0; i < sizeof(result); i++ )
if( result[i] ) result[j++] = i;
result[j] = 0;
printf( "%s %s %d", str, result, j );
getchar();
特点:
1. 没有调用任何函数(显示语句除外),只用基本的CPU指令:寻址和加1
2. 编译后的代码极短,实测值如下:
算法部分代码124字节,
数据128字节(不算被测字符串)
3. 速度极快,算法部分只用几千个时钟周期,微秒数量级。
4. 适用于标准ASC码1-127
const char str[] = "fdsaasdfhjklpouop[gtyuusrd65987676067876^&*(&*^&^(*tyiu%^&*(^&%";
char result[128];
for( int i = 0; i < sizeof(result); result[i++] = 0 );
for( int i = 0; i < sizeof(str)-1; result[str[i++]]++ );
int j = 0;
for( int i = 0; i < sizeof(result); i++ )
if( result[i] ) result[j++] = i;
result[j] = 0;
printf( "%s %s %d", str, result, j );
getchar();
特点:
1. 没有调用任何函数(显示语句除外),只用基本的CPU指令:寻址和加1
2. 编译后的代码极短,实测值如下:
算法部分代码124字节,
数据128字节(不算被测字符串)
3. 速度极快,算法部分只用几千个时钟周期,微秒数量级。
4. 适用于标准ASC码1-127
#46
这个效率高。
#47
大致都差不多,还是这个看着最舒服,清爽.
#48
就看懂9楼的 不错 哈哈 其他没看懂
#49
78657073外包联盟
#50
学习。。。。。。。
#1
不知道效率怎么样
但是这是一个思路
你可以用正则表达式去匹配
从ASCII为0的到128
以下是.NET下正则表达式的使用方法
http://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex(VS.80).aspx
// Define a regular expression for repeated words.
Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
// Define a test string.
string text = "The the quick brown fox fox jumped over the lazy dog dog.";
// Find matches.
MatchCollection matches = rx.Matches(text);
// Report the number of matches found.
Console.WriteLine("{0} matches found.", matches.Count);
// Report on each match.
foreach (Match match in matches)
{
string word = match.Groups["word"].Value;
int index = match.Index;
Console.WriteLine("{0} repeated at position {1}", word, index);
}
但是这是一个思路
你可以用正则表达式去匹配
从ASCII为0的到128
以下是.NET下正则表达式的使用方法
http://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex(VS.80).aspx
// Define a regular expression for repeated words.
Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
// Define a test string.
string text = "The the quick brown fox fox jumped over the lazy dog dog.";
// Find matches.
MatchCollection matches = rx.Matches(text);
// Report the number of matches found.
Console.WriteLine("{0} matches found.", matches.Count);
// Report on each match.
foreach (Match match in matches)
{
string word = match.Groups["word"].Value;
int index = match.Index;
Console.WriteLine("{0} repeated at position {1}", word, index);
}
#2
关注效果.
string _Text = "abcdefgabc";
SortedList<char, int> _Value = new SortedList<char, int>();
int _Count = _Text.Length;
for (int i = 0; i != _Count; i++)
{
if (_Value.IndexOfKey(_Text[i]) == -1)
{
_Value.Add(_Text[i], 1);
}
else
{
_Value[_Text[i]]++;
}
}
foreach(char _Char in _Value.Keys)
{
MessageBox.Show(_Char+","+ _Value[_Char].ToString());
}
string _Text = "abcdefgabc";
SortedList<char, int> _Value = new SortedList<char, int>();
int _Count = _Text.Length;
for (int i = 0; i != _Count; i++)
{
if (_Value.IndexOfKey(_Text[i]) == -1)
{
_Value.Add(_Text[i], 1);
}
else
{
_Value[_Text[i]]++;
}
}
foreach(char _Char in _Value.Keys)
{
MessageBox.Show(_Char+","+ _Value[_Char].ToString());
}
#3
#4
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());
#5
ding
#6
ding
#7
==哈
晚上写一个
晚上写一个
#8
string GetCharCount(string text)
{
string result=string.Empty;
int len=text.Length;
while(len>0)
{
string temp=text[0].ToString();
text=text.Replace(temp,"");
int count=len-text.Length;
result+=temp+","+count+"\r\n";
len=text.Length;
}
return result;
}
#9
支持一个~替换了多少个字~~一次出一种字符~~~学习了
#10
收藏一下。
#11
强!
#12
int[] c = new int[256];
for (int i = 0; i < 256; i++)
{
c[i] = 0;
}
foreach (char e in str)
{
int i = (int)e;
c[i] ++ ;
}
for (int i = 0; i < 256; i++)
{
if (c[i] != 0)
{
console.write((char)i + "," + c[i].ToString() + "\r\n");
}
}
for (int i = 0; i < 256; i++)
{
c[i] = 0;
}
foreach (char e in str)
{
int i = (int)e;
c[i] ++ ;
}
for (int i = 0; i < 256; i++)
{
if (c[i] != 0)
{
console.write((char)i + "," + c[i].ToString() + "\r\n");
}
}
#13
这样多简单,其实核心代码就三行:
foreach (char e in str)
{
int i = (int)e;
c[i] ++ ;
}
把字符串里每一个字符转换成对应的int值,然后将int数组里对应的那个值++
foreach (char e in str)
{
int i = (int)e;
c[i] ++ ;
}
把字符串里每一个字符转换成对应的int值,然后将int数组里对应的那个值++
#14
先占位
#15
顶
#16
C++ Code
int main(void)
{
string str = "abcdeffasljdlfjaslflsadjflkasdfjlsadlfgabc";
sort(str.begin(), str.end()); //先排序
cout<<str<<endl;
char c;
int count;
int i = 0;
while(i < str.length()){
c = str[i];
count = 1;
while(c == str[++i]){
count++;
}
cout<<c<<","<<count<<endl;
}
return 0;
}
int main(void)
{
string str = "abcdeffasljdlfjaslflsadjflkasdfjlsadlfgabc";
sort(str.begin(), str.end()); //先排序
cout<<str<<endl;
char c;
int count;
int i = 0;
while(i < str.length()){
c = str[i];
count = 1;
while(c == str[++i]){
count++;
}
cout<<c<<","<<count<<endl;
}
return 0;
}
#17
学习了...
#18
declare @a varchar(max)
set @a='abcdefgabc'
declare @b table(s varchar(1))
declare @n int
declare @i int
set @i=1
set @n=len(@a)
set rowcount @n
while @i<=@n
begin
insert into @b select substring(@a,@i,1)
set @i=@i+1
end
set rowcount 0
select count(s),s from @b group by s
#19
declare @a varchar(max)
set @a='abcdefgabc'
declare @b table(s varchar(1))
declare @n int
declare @i int
set @i=1
set @n=len(@a)
set rowcount @n
while @i<=@n
begin
insert into @b select substring(@a,@i,1)
set @i=@i+1
end
set rowcount 0
select count(s),s from @b group by s
/*结果
2 a
2 b
2 c
1 d
1 e
1 f
1 g
*/
#20
string a="abcdefgabc";
int i=a.Length;
while(i>0)
{
a=a.Replace(a[0],'');
Console.WriteLine(i-a.Length);
}
#21
学习
#22
强烈顶!!!!!!!!!!!!
#23
又见Linq,简洁得令人难以置信。
另外,用List<T>应该也是一个不错的选择:
List<char> list = new List<char>(text.ToCharArray());
#24
void Calculate(const string &str,char *res)
{
if(res==NULL)
return;
int length = str.length();
for(int i=0;i<length;i++)
{
res[str[i]]++;
}
}
void main()
{
string str = "sfsfsdfasrq24234we:~!@#.....$%%S^ldf";
char res[200];
ZeroMemory(res,sizeof(res));
Calculate(str,res);
int total = 0;
for(int i=0;i<200;i++)
{
if(res[i]!=0)
{
cout<<char(i)<<": "<< (int)res[i]<<endl;
total += (int)res[i];
}
}
cout<<"total="<<total<<" strLength="<<str.length()<<endl;
}
#25
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication67
{
class Program
{
static void Main(string[] args)
{
string a = "abcdefgabc";
int i = a.Length;
while (i > 0)
{
Console.WriteLine(a[0].ToString()+":");
a = a.Replace(a[0].ToString(), "");
Console.Write(i - a.Length);
Console.WriteLine();
i=a.Length;
}
}
}
}
#26
php:
$str = "abcdefgabc";
for(;$i<strlen($str);$i++)$astr[]=$str[$i];
print_r(array_count_values($astr));
#27
修改成StringBuilder 要好一点因为
对 a 进行 重赋值 是要重新分配内存的
如果字符 非常大,就是用StringBuilder
#28
都是强人啊
#29
procedure aa(s: string;l: TStrings);
var
c: array[0..255] of integer;
i: integer;
begin
for i := 0 to 255 do
c[i] := 0;
for i := 1 to pinteger(integer(s) - 4)^ do
inc(c[byte(s[i])]);
for i := 0 to 255 do
if c[i] > 0 then l.Add(char(i) +'|'+IntToStr(i)+'|'+ IntToStr(c[i]))
end;
#30
学习一下
#31
凑个热闹, 来个JavaScript的...
var ret = {};
var str = "abcdefgabc";
for(var i=0; i<str.length; i++){
var char = str.charAt(i);
ret[char] = ret[char] ? ret[char] + 1 : 1;
};
for(var pro in ret){
document.write(pro.toString() + " : " + ret[pro] + "<br/>");
}
#32
php的实现方法:
PHP_FUNCTION(array_count_values)
{
zval **input, /* Input array */
**entry; /* An entry in the input array */
zval **tmp;
HashTable *myht;
HashPosition pos;
/* Get arguments and do error-checking */
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &input) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (Z_TYPE_PP(input) != IS_ARRAY) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The argument should be an array");
return;
}
/* Initialize return array */
array_init(return_value);
/* Go through input array and add values to the return array */
myht = Z_ARRVAL_PP(input);
zend_hash_internal_pointer_reset_ex(myht, &pos);
while (zend_hash_get_current_data_ex(myht, (void **)&entry, &pos) == SUCCESS) {
if (Z_TYPE_PP(entry) == IS_LONG) {
if (zend_hash_index_find(Z_ARRVAL_P(return_value),
Z_LVAL_PP(entry),
(void**)&tmp) == FAILURE) {
zval *data;
MAKE_STD_ZVAL(data);
Z_TYPE_P(data) = IS_LONG;
Z_LVAL_P(data) = 1;
zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_PP(entry), &data, sizeof(data), NULL);
} else {
Z_LVAL_PP(tmp)++;
}
} else if (Z_TYPE_PP(entry) == IS_STRING) {
if (zend_hash_find(Z_ARRVAL_P(return_value),
Z_STRVAL_PP(entry),
Z_STRLEN_PP(entry)+1,
(void**)&tmp) == FAILURE) {
zval *data;
MAKE_STD_ZVAL(data);
Z_TYPE_P(data) = IS_LONG;
Z_LVAL_P(data) = 1;
zend_hash_update(Z_ARRVAL_P(return_value), Z_STRVAL_PP(entry), Z_STRLEN_PP(entry) + 1, &data, sizeof(data), NULL);
} else {
Z_LVAL_PP(tmp)++;
}
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can only count STRING and INTEGER values!");
}
zend_hash_move_forward_ex(myht, &pos);
}
}
#33
同意用List。
另外,空间换时间算法也是可以的,也就是去除字符数组里面重复的字符对象。
另外,空间换时间算法也是可以的,也就是去除字符数组里面重复的字符对象。
#34
mark,学习了
#35
终于看到一个像算法的...
什么泛型,枚举器,linq...跟手工的哈希表比起来统统都是渣!
#36
C的,TCPL的序章中的一个思路。
#include <stdio.h>
int main(void)
{
char * s = "abcdefgabc";
int c[256] = { 0 }, i;
while(*s != '\0')
c[*s++]++;
for(i = 0; i < 256; ++i)
if(c[i])
printf("%c,%d\n", i, c[i]);
return 0;
}
#37
public class CountChar
{
static UInt16[,] ASCMapping = new UInt16[128,2];
private static void initMapping()
{
for (UInt16 i = 0; i < 128; i++)
{
ASCMapping[i, 0] = i;
}
}
private static void PrintResult()
{
for (UInt16 i = 0; i < 128; i++)
{
if (ASCMapping[i, 1] > 0)
Console.WriteLine(string.Format("{0},{1}",(char)ASCMapping[i, 0], ASCMapping[i, 1]));
}
}
public static void test()
{
initMapping();
string s = "dad1dsad1esa12da";
for (int i = 0; i < s.Length; i++)
{
ASCMapping[(UInt16)s[i],1]++;
}
PrintResult();
}
}
楼主题目为字符串,但这段代码只支持ASCII字符,可扩展,并且适合长字符串的统计
{
static UInt16[,] ASCMapping = new UInt16[128,2];
private static void initMapping()
{
for (UInt16 i = 0; i < 128; i++)
{
ASCMapping[i, 0] = i;
}
}
private static void PrintResult()
{
for (UInt16 i = 0; i < 128; i++)
{
if (ASCMapping[i, 1] > 0)
Console.WriteLine(string.Format("{0},{1}",(char)ASCMapping[i, 0], ASCMapping[i, 1]));
}
}
public static void test()
{
initMapping();
string s = "dad1dsad1esa12da";
for (int i = 0; i < s.Length; i++)
{
ASCMapping[(UInt16)s[i],1]++;
}
PrintResult();
}
}
楼主题目为字符串,但这段代码只支持ASCII字符,可扩展,并且适合长字符串的统计
#38
喜欢用C#方法做完!
#39
qiang a ding shang ba
#40
qiang a ding shang ba
#41
应该是这种算法 如果不考率 空间大小 毕竟现在的程序空间很宽余了 u盘都上G了
就是把字符ascII 当数组下标用 256个桶是哪个桶子就在那个桶加1
#42
写个java的,不过限于ascii码字符
测试代码:
public static int[] func(String input) {
int[] result = new int[128];
for (int i = 0, l = input.length(); i < l; i++) {
result[input.charAt(i)]++;
}
return result;
}
测试代码:
public void test() {
int[] result = func("abcdefgabc");
for (int i = 0; i < 128; i++) {
if (result[i] != 0) {
System.out.println((char)i + "," + result[i]);
}
}
}
#43
哇!来晚了,方法挺多的哈!
#44
哈希算法就行了
#45
编译后只有一百多字节, 标准C
const char str[] = "fdsaasdfhjklpouop[gtyuusrd65987676067876^&*(&*^&^(*tyiu%^&*(^&%";
char result[128];
for( int i = 0; i < sizeof(result); result[i++] = 0 );
for( int i = 0; i < sizeof(str)-1; result[str[i++]]++ );
int j = 0;
for( int i = 0; i < sizeof(result); i++ )
if( result[i] ) result[j++] = i;
result[j] = 0;
printf( "%s %s %d", str, result, j );
getchar();
特点:
1. 没有调用任何函数(显示语句除外),只用基本的CPU指令:寻址和加1
2. 编译后的代码极短,实测值如下:
算法部分代码124字节,
数据128字节(不算被测字符串)
3. 速度极快,算法部分只用几千个时钟周期,微秒数量级。
4. 适用于标准ASC码1-127
const char str[] = "fdsaasdfhjklpouop[gtyuusrd65987676067876^&*(&*^&^(*tyiu%^&*(^&%";
char result[128];
for( int i = 0; i < sizeof(result); result[i++] = 0 );
for( int i = 0; i < sizeof(str)-1; result[str[i++]]++ );
int j = 0;
for( int i = 0; i < sizeof(result); i++ )
if( result[i] ) result[j++] = i;
result[j] = 0;
printf( "%s %s %d", str, result, j );
getchar();
特点:
1. 没有调用任何函数(显示语句除外),只用基本的CPU指令:寻址和加1
2. 编译后的代码极短,实测值如下:
算法部分代码124字节,
数据128字节(不算被测字符串)
3. 速度极快,算法部分只用几千个时钟周期,微秒数量级。
4. 适用于标准ASC码1-127
#46
这个效率高。
#47
大致都差不多,还是这个看着最舒服,清爽.
#48
就看懂9楼的 不错 哈哈 其他没看懂
#49
78657073外包联盟
#50
学习。。。。。。。