用Java設計一個程式,找到一個字串中對稱字串的個數【面試題】

來源:互聯網
上載者:User

題目要求:

用Java設計一個程式,實現一個字串的對稱個數,如字串"effeghg",有"ff","effe","ghg"這三個對稱字元,所以返回3.

 

我實現的思路就是遍曆這個字串,

先選定頭位置為第一個字元,然後從最後向前遍曆這個字串,

頭尾兩個字元相同,則取中間字串,進行遞迴。

遞迴結束後得到結果,

繼續將頭向後推1位,然後再從字串最後向前遍曆,

如此迴圈,當尾等於頭時,退出最外層迴圈,輸出結果。

 

具體實現:

  1. /**
  2.  * @author bzwm
  3.  * 
  4.  */
  5. public class FindSymmetryStr {
  6.     /**
  7.      * 找出字串中對稱的子字串的個數
  8.      * @param orgStr
  9.      * @return
  10.      */
  11.     public static int findSymmetryStr(String orgStr) {
  12.         //結果初始化
  13.         int count = 0;
  14.         //當輸入字串不為null且長度大於1時進行尋找,否則直接返回0
  15.         if (orgStr != null && orgStr.length() > 1) {
  16.             //得到輸入字串的長度
  17.             int size = orgStr.length();
  18.             //字串的頭字元索引
  19.             int head;
  20.             //字串從後向前遍曆時的"尾"字元索引,即當前字元索引
  21.             int current;
  22.             //字串的頭字元
  23.             char hStr;
  24.             //字串從後向前遍曆時的"尾"字元
  25.             char cStr;
  26.             //從前開始遍曆字串
  27.             for (head = 0; head < size; head++) {
  28.                 //取得頭字元
  29.                 hStr = orgStr.charAt(head);
  30.                 //指向輸入字串的最後
  31.                 current = size - 1;
  32.                 //當尾字元索引等於頭字元索引時退出迴圈
  33.                 while (current > head) {
  34.                     //取得尾字元
  35.                     cStr = orgStr.charAt(current);
  36.                     //如果頭尾字元相等,則繼續判斷
  37.                     if (hStr == cStr) {
  38.                         //取出頭尾中間的子字串,對其進行分析
  39.                         String newStr = orgStr.substring(head + 1, current);
  40.                         //如果此子字串的長度大於1,則進行遞迴
  41.                         if (newStr.length() > 1)
  42.                             //遞迴得到此子字串中對稱的字串個數
  43.                             count += findSymmetryStr(newStr);
  44.                         //如果此子字串只有1個或0個字元,則表明原頭尾字元和此單個字元組成對稱字串
  45.                         else
  46.                             count++;
  47.                         //將尾字元索引向前推1位
  48.                         current--;
  49.                     }
  50.                     //如果頭尾字元不相等,則將尾字元索引向前推1位
  51.                     else {
  52.                         current--;
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.         return count;
  58.     }
  59.     //測試程式
  60.     public static void main(String args[]) {
  61.         int count = findSymmetryStr("cddcbcbeffeghg");//
  62.         System.out.println("symmetry string count is : " + count);
  63.     }
  64. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.