刪除字串中出現次數最少的字元,字串次數

來源:互聯網
上載者:User

刪除字串中出現次數最少的字元,字串次數

在公交車上看一部落格實現刪除字串中出現次數最少的字元,覺得裡面使用數組來作為hash非常好,所以我就自己實現一遍。

要求:實現刪除一個字串中出現次數最少的字元,輸出刪除後的字元,要求安裝原來順序輸出。如果出現次數一樣,則都刪除。

例如:abcabc 則全部刪除,輸出Null。

            aabbccddd 則輸出 ddd

代碼:

char* DeleteLeastString(char* desStrint){if (desStrint == NULL){return NULL;}char* crtn = new char[len];    //儲存輸出字元,注意是newchar* ctemp = crtn;char* ctempDes = desStrint;int min = len                  //記錄最少出現次數int len = 0;int hash[256] = {0};           //以下標為key,值為value(次數)while(*ctempDes) //擷取字元個數,並填充hash表{++hash[(int)*ctempDes];ctempDes++;len++;}ctempDes = desStrint;while(*ctempDes) //找出字元出現最少次數的值if (min > hash[(int)*ctempDes++])min = target;ctempDes = desStrint;while(*ctempDes)//輸出刪除後的字串{if (min < hash[(int)*ctempDes])*ctemp++ = *ctempDes++;elsectempDes++;}*ctemp = '\0';return crtn;}


我相信,肯定有比我更好的實現方法,知道的告知一聲,謝謝。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.