一道面试题:如:“alsdk 0poqwer rkj  kerie qwel;  wejw ”在这个字符串中,找出出现次数最多的字母,并计算次数?

解决方案 »

  1.   


    declare @ddd nvarchar(2000)
    declare @ll int
    set @ddd='alsdk 0poqwer rkj  kerie qwel;  wejw ';
    set @ddd=rtrim(ltrim(@ddd));--去空格
    set @ddd=replace(@ddd,' ','');--去空格
    set @ll=len(@ddd);--求总长度
    select @ll
    select len(@ddd)-len(replace(@ddd,'a',''))--a的个数
    select len(@ddd)-len(replace(@ddd,'w',''))--w的个数应该先将串分解了,然后游标
    select len(@ddd)-len(replace(@ddd,'a',''))--a的个数
    游标一行一行,替换a的位置。
    然后看最大值。
      

  2.   

    可以做一个Hashtable,key为字符,value为出现的次数
    遍历字符串
    碰到不存在于hashtable中的字符,加到hashtable中
    遇到已经存在于hashtable中的,将Value加1即可
    最后找最大value对应的字符
      

  3.   

    你要是在程序里处理
    char[] chars=''alsdk 0poqwer rkj  kerie qwel;  wejw ''.toCharArray();
      

  4.   

    蹭点分
    Dictionary<char, int> counter = new Dictionary<char, int>();
    string s = "alsdk 0poqwer rkj  kerie qwel;  wejw ";
    char max = s[0];
    s = s.ToLower(); // 全小写
    foreach (char c in s)
    {
        if (!char.IsLetter(c)) continue; // 非字母
        if (counter.ContainsKey(c))
            counter[c]++;
        else counter.Add(c, 1);    if (counter[max] < counter[c]) max = c;
    }
    Console.WriteLine("出现最多的字符是:'{0}' 共出现:{1}次", max, counter[max]);
      

  5.   

    次序颠倒一下,变小写是后面加的
    s = s.ToLower(); // 全小写
    char max = s[0];
      

  6.   

                string s = "alsdk 0poqwer rkj  kerie qwel;  wejw ";
                Dictionary<string, int> d = new Dictionary<string, int>();
    //List<int> number=new List<int>();            for (int i = 0; i <= s.Length; i++)
                {
    //number.Add(s.Split(new char[] { s[0]}).Length-1);
                    d["Char:"+s[0]] = s.Split(new char[] { s[0]}).Length-1;
                    s = s.Replace(s[0].ToString(),"");
                    i = 0;
                }//排序...自己写吧。
      

  7.   


    string s  = "alsdk 0poqwer rkj  kerie qwel;  wejw";
    string s1 = "alsdk 0poqwer rkj  kerie qwel;  wejw";
    int[] arr = new int[s.Length];
    int j     = 0;
    for (int i = 0; i < s.Length; i++)
    {
    int tmp1 = s.Length;
    s = s.Replace(s[i].ToString(), "");
    arr[j] = tmp1 - s.Length;
    j++; i--;
    }int tmp = 0;
    for (int i = 0; i < arr.Length; i++)
    {
    if (arr[i] > tmp && arr[i]>0)
    {
        tmp = arr[i]; j = i;
        //j = i;
    }
    }label4.Text = "alsdk 0poqwer rkj  kerie qwel;  wejw出现次数最多的字符是:" + s1[j] + ";出现次数是:" + tmp;
      

  8.   

    上面的是出现最多的字符
    显示最多的字母应当string s  = "alsdk 0poqwer rkj  kerie qwel;  wejw";
                string s1 = "alsdk 0poqwer rkj  kerie qwel;  wejw";
                s = s.Replace(" ", "");
                s1 = s1.Replace(" ", "");
                int[] arr = new int[s.Length];
                int j     = 0;
                for (int i = 0; i < s.Length; i++)
                {
                    int tmp1 = s.Length;
                    s = s.Replace(s[i].ToString(), "");
                    arr[j] = tmp1 - s.Length;
                    j++; i--;
                }            int tmp = 0;
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i] > tmp && arr[i]>0)
                    {
                        tmp = arr[i]; j = i;
                        //j = i;
                    }
                }            label4.Text = "alsdk 0poqwer rkj  kerie qwel;  wejw出现次数最多的字符是:" + s1[j] + ";出现次数是:" + tmp;
      

  9.   

                String str1 = "alsdk 0poqwer rkj  kerie qwel;  wejw";
                char[] cStr = str1.ToCharArray();
                Array.Sort(cStr);
      //之后自己数吧
      

  10.   

                String strTemp = "alsdk 0poqwer rkj  kerie qwel;  wejw";
                int iMax = 0;
                char cMax = Char.MinValue;
                while (strTemp.Length > 0)
                {
                    int iLen = strTemp.Length;
                    char c = strTemp[0];                
                    strTemp = strTemp.Replace(c.ToString(),"");
                    if (c == ' ') continue;//空格忽略
                    if (iMax < iLen - strTemp.Length)
                    {
                        iMax = iLen - strTemp.Length;
                        cMax = c;
                    }
                }
               Console.WriteLine("出现最多的字符是:'{0}' 共出现:{1}次", cMax, iMax); 
      

  11.   

    把每个字母都放到一个表里。
    select 一下
      

  12.   

    using System;class Program
    {
        static void Main(string[] args)
        {
            string str = "alsdk 0poqwer rkj kerie qwel; wejw";        int[] count = new int[256];        int max = 0;
            foreach (char c in str)
                if (count[c]++ == max) max++;        for (int i = 0; i < 256; i++)
                if (count[i] == max) Console.WriteLine("字符'{0}'出现了{1}次",(char)i,max);        Console.Read();
        }
    }
      

  13.   


         class Program
        {
            private const string checkStr = "static void Main(string[] args)Console.ReadLine();namespace ConsoleApplication1using System.Collections.Generic;using System;";
            static void Main(string[] args)
            {
                string newStr = checkStr.Replace(" ", "").ToLower();
                char[] everyOne=newStr.ToCharArray();
                int maxLenght = 0;
                char maxChar=' ';
                for (int i = 0; i < everyOne.Length; i++)
                {
                    int lenght = newStr.Split(everyOne[i]).Length;
                    if (lenght > maxLenght)
                    {
                        maxLenght = lenght;
                        maxChar = everyOne[i];
                    }
                }
                System.Console.WriteLine("the max char is:{0},count is:{1}", maxChar.ToString(), (maxLenght-1).ToString());
                System.Console.ReadLine();
            }
        }[
      

  14.   

    也可以先排序,然后String的StartsWith,EndsWith判断出现的次数
    有高人提出效率高,方法简单的方法吗?
      

  15.   

    21楼,请测试
    string str = "zswang 路过";char的范围是0-32767不是0-255楼主说的是字母,那么就26个即可经过测试发现IsLeeter()不是判断字母的意思-_-!!!
    char.IsLetter('汉')为True
    char.IsLetter('⊙')为False
    看来表示是否为文字(非符号)的意思完善一下该是:
    string str = "alsdk 0poqwer rkj  kerie qwel;  wejw ;zswang 路过";
    int[] count = new int[26];
    int max = 0;
    str = str.ToLower();
    foreach (char c in str)
        if (c >= 'a' && c <= 'z')
            if (count[c - 'a']++ == max) max++;for (int i = 0; i < 26; i++)
        if (count[i] == max)
            Console.WriteLine("字符'{0}'出现了{1}次", (char)('a' + i), max);
    当然楼主已经失踪,就忽略掉
      

  16.   

    winform中拖一个按钮,两个RichText 
    using   System; 
    using   System.Collections.Generic; 
    using   System.ComponentModel; 
    using   System.Data; 
    using   System.Drawing; 
    using   System.Text; 
    using   System.Windows.Forms; 
      
    namespace   cutWord 

            public   partial   class   Form1   :   Form 
            { 
                    public   Form1() 
                    { 
                            InitializeComponent(); 
                    }                 private   void   button1_Click(object   sender,   EventArgs   e) 
                    { 
                            List <KeyValuePair <string,   int> >   L   =   cutWord(this.richTextBox1.Text); 
                            this.richTextBox2.Text   =   " "; 
                            int   count   =   0; 
                            for   (int   i   =   L.Count-1;   i   > 0;   i--) 
                            { 
                                    if   (count   >   15) 
                                    { 
                                            break; 
                                    } 
                                    count++; 
                                    this.richTextBox2.Text   +=   L[i].Key   + "[ "   +   L[i].Value   + "]\n "; 
                            }                                   
                    } 
                    private   List <KeyValuePair <string,   int> >     cutWord(string   article) 
                    { 
                            Dictionary <string,   int>   D   =   new   Dictionary <string,   int> (); 
                            //if   len(escape(x))   /len(x)=6   then   isGB=true   else   isGB=false 
                            //HttpUtility..::                         System.Text.RegularExpressions.Regex   Re   =   new   System.Text.RegularExpressions.Regex(@ "[^\u4e00-\u9fa5]+ "); 
                            for   (int   l   =   2;   l   <=   4;   l++) 
                            { 
                                    for   (int   i   =   0;   i   <   article.Length-l;   i++) 
                                    { 
                                            string   theWord   =   article.Substring(i,   l); 
                                            if   (Re.Replace(theWord, " ")   ==   theWord) 
                                            { 
                                                    if   (D.ContainsKey(theWord)) 
                                                    { 
                                                            D[theWord]++; 
                                                    } 
                                                    else 
                                                    { 
                                                            D.Add(theWord,1); 
                                                    } 
                                            }                                                             
                                    } 
                            } 
                    
                            List <KeyValuePair <string,   int> >   L   =   new   List <KeyValuePair <string,   int> > (); 
                            foreach   (KeyValuePair <string,   int>   K   in   D) 
                            { 
                                    if   (K.Value   >   1) 
                                    { 
                                            L.Add(K); 
                                    } 
                                    
                                            
                            }                         L.Sort( 
                                    delegate(KeyValuePair <String,   int>   a,   KeyValuePair <String,   int>   b) 
                                    {                                     
                                            if   (a.Value   ==   b.Value) 
                                            { 
                                                    if   (a.Key.Length   ==   b.Key.Length) 
                                                    { 
                                                            return   0; 
                                                    } 
                                                    else 
                                                    { 
                                                            if   (a.Key.Length   >   b.Key.Length) 
                                                            { 
                                                                    return   1; 
                                                            } 
                                                            else 
                                                            { 
                                                                    return   -1; 
                                                            } 
                                                    }                                                 
                                            } 
                                            if   (a.Value   >   b.Value) 
                                            { 
                                                    return   1; 
                                            } 
                                            else 
                                            { 
                                                    return   -1; 
                                            } 
                                            
                                    }                         ); 
                            return   (L);                 } 
            } 
    } 测试Sohu首页关键字和频率: 
    女人[20] 
    搜狐[16] 
    手机[13] 
    游戏[13] 
    博客[13] 
    明星[11] 
    留学[10] 
    美女[9] 
    大学[9] 
    上海[9] 
    中国[9] 
    北京[9] 
    奥运[9] 
    曝光[9] 
    开盘[9] 
    视频[8] 晕!女人排第一 
    新浪: 中国[22] 
    视频[20] 
    手机[14] 
    上海[13] 
    新浪[13] 
    北京[12] 
    北大[12] 
    国际[12] 
    投资[11] 
    清华[11] 
    博客[11] 
    直播[11] 
    大学[10] 
    留学[10] 
    项目[9] 
    赚钱[9] 再看CSDN首页: 
    软件[16] 
    程序[13] 
    程序员[11] 
    序员[11] 
    开发[11] 
    大会[7] 
    微软[7] 
    我的[7] 
    专区[6] 
    技术[6] 
    中国[5] 
    使用[5] 
    我们[5] 
    模式[5] 
    系统[5] 
    人才[5] 
      

  17.   

    string str = "alsdk 0poqwer rkj  kerie qwel;  wejw ;zswang 路过";
    int max = 0;
    char rtn='';
    str = str.ToLower();
    foreach (char c in str)
    {
    ///我是手写的,忘记了是Count还是Length
      if(str.Split(c).Length>max)
        {
         max=str.Split(c).Length;
         rtn=c;
        }
    }
      

  18.   

    循环输入字符串,
    用一个dictionary来存放,字母和出现的次数,key放字母,value放次数,然后输出value最大的那个
      

  19.   

    26楼所言极是,我忘记了char是个16位的了
      

  20.   

    注意:此属性在 .NET Framework 2.0 版中是新增的。
      

  21.   

    KeyedCollection.Dictionary 性在 .NET Framework 2.0 版中是新增的。
      

  22.   

    void freq(char *str)
    {
            unsigned int count[26] = {0}, max_count=0;
            char fre_char = 0, i = 0;        while (*str != '\0') { if (isalpha((int)(*str++))) ++(count[*(str-1)-'a']); }        for (i=0; i<26; ++i)
            {
                    if (max_count < count[i])
                    {
                            max_count = count[i];
                            fre_char  = (char) (i+'a');
                    }
            }
            printf("frequent char is %c, max count is %d\n", fre_char, max_count);
    }
      

  23.   

    加入大小写判断和最大出现次数相同时的处理
    unsigned int freq(const char *str, char *fre_chars)
    {
            unsigned int count[52] = {0}, max_count = 0;
            char i = 0;        while (*str != '\0')
            {
                    if(islower((int)(*str)))
                            ++(count[*str-'a']);
                    else if(isupper((int)(*str)))
                            ++(count[*str-'A'+26]);
                    ++str;
            }        for (i=0; i<52; ++i)
            {
                    if (max_count < count[i])
                            max_count = count[i];
            }        for (i=0; i<52; ++i)
            {
                    if (count[i] == max_count)
                            *fre_chars++  = (i>25 ? (char)(i-26+'A') : (char)(i+'a'));
            }        return max_count;
    }
      

  24.   

    怎么都是C#的代码啊,有没有C++的或JAVA的?
      

  25.   

    26楼代码:string str = "alsdk 0poqwer rkj  kerie qwel;  wejw ;zswang 路过";
    int[] count = new int[26];
    int max = 0;
    str = str.ToLower();//该语句复杂度为o(2n+m),其中m为大写字符数
    foreach (char c in str)
        if (c >= 'a' && c <= 'z')//该语句时间复杂度为o(2n)
            if (count[c - 'a']++ == max) max++;//该语句时间复杂度为o(3n+k),k为max++执行次数for (int i = 0; i < 26; i++)//在n值很大时,该语句的执行时间可以忽略不计,视为一个常数l。
        if (count[i] == max)
            Console.WriteLine("字符'{0}'出现了{1}次", (char)('a' + i), max);分析其算法复杂度
    其累计算法复杂度为o(7n+m+k+l),其中比较5n次,计算n次,赋值n+m+k次改进代码(只认增强的ACSII码,其他一概不认):string str = "alsdk 0poqwer rkj  kerie qwel;  wejw ;zswang 路过";
    int[] count = new int[256];
    int max = 0;
    foreach (char c in str)
        if (c & 0xff00 == 0)//该语句时间复杂度为o(2n)
            count[c]++;//该语句时间复杂度为o(n)
    List<char> list = new List();
    for (int i = 0; i < 256; i++)//在n值很大时,该语句的执行时间可以忽略不计,视为一个常数L,该L比26楼的l要大。
    {
        if (count[i] > max)
        {
            max = count[i];
            list.Clear();
            list.Add((char)i);
        }
        else if (count[i] == max)
        {
            list.Add((char)i);
        }
    }
    foreach(char c in list)
    {
        Console.WriteLine("字符'{0}'出现了{1}次", c, max);
    }总计时间复杂度o(3n+L),其中比较n次,计算n次,赋值n次。
      

  26.   

    declare @ddd nvarchar(2000)
    declare @ll int
    set @ddd='alsdk 0poqwer rkj  kerie qwel;  wejw ';
    set @ddd=rtrim(ltrim(@ddd));--去空格
    set @ddd=replace(@ddd,' ','');--去空格
    set @ll=len(@ddd);--求总长度
    select @ll
    select len(@ddd)-len(replace(@ddd,'a',''))--a的个数
    select len(@ddd)-len(replace(@ddd,'w',''))--w的个数这个答案好
      

  27.   

    用2个Hashtable来做,一遍扫描应该可以了。第一个hashtable记录字符重复的个数,第二个hashtable记录扫描过程中出现次数最多的字符及其出现次数。不一定是最完美的,有任何建议可以提出,谢谢。
            
            public static Hashtable FindHiFreqChar(string inputStr)
            {
                Hashtable ht1 = new Hashtable();
                Hashtable ht2 = new Hashtable();
                int maxCurrent = 1;
                int tmpCount;
                ht1.Add(inputStr[0], 1);
                for (int i = 1; i < inputStr.Length; i++)
                {
                    if (ht1.Contains(inputStr[i]))
                    {
                        ht1[inputStr[i]] = (int)ht1[inputStr[i]] + 1;
                        tmpCount = (int)ht1[inputStr[i]];
                        if (tmpCount >= maxCurrent)
                        {
                            maxCurrent = tmpCount;
                            ht2.Clear();
                            ht2.Add(inputStr[i], maxCurrent);
                        }                    
                    }
                    else
                    {
                        if (i == inputStr.Length - 1 && maxCurrent <= 1)
                        {                        
                            ht2.Clear();
                            ht2.Add(inputStr[i], 1);                        
                        }
                        else
                        {
                            ht1.Add(inputStr[i], 1);
                        }
                                          
                    }
                }            return ht2;
            }
      

  28.   


    public static Hashtable FindHiFreqChar(string inputStr)
            {
                Hashtable ht1 = new Hashtable();
                Hashtable ht2 = new Hashtable();
                int maxCurrent = 1;
                int tmpCount;
                ht1.Add(inputStr[0], 1);
                for (int i = 1; i < inputStr.Length; i++)
                {
                    if (ht1.Contains(inputStr[i]))
                    {
                        ht1[inputStr[i]] = (int)ht1[inputStr[i]] + 1;
                        tmpCount = (int)ht1[inputStr[i]];
                        if (tmpCount >= maxCurrent)
                        {
                            maxCurrent = tmpCount;
                            ht2.Clear();
                            ht2.Add(inputStr[i], maxCurrent);
                        }                    
                    }
                    else
                    {
                        if (i == inputStr.Length - 1 && maxCurrent <= 1)
                        {                        
                            ht2.Clear();
                            ht2.Add(inputStr[i], 1);                        
                        }
                        else
                        {
                            ht1.Add(inputStr[i], 1);
                        }
                                          
                    }
                }            return ht2;
            }