ALL is Well!

          敏捷是一條很長的路,摸索著前進著

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            30 隨筆 :: 23 文章 :: 71 評論 :: 0 Trackbacks

          題目要求:

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

           

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

          先選定頭位置為第一個字符,然后從最后向前遍歷這個字符串,

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

          遞歸結束后得到結果,

          繼續將頭向后推1位,然后再從字符串最后向前遍歷,

          如此循環,當尾等于頭時,退出最外層循環,輸出結果。

           

          具體實現:

          /** 
           * 
          @author bzwm 
           *  
           
          */
           
          public class FindSymmetryStr 
              
          /** 
               * 找出字符串中對稱的子字符串的個數 
               * 
          @param orgStr 
               * 
          @return 
               
          */
           
              
          public static int findSymmetryStr(String orgStr) 
                  
          //結果初始化 
                  int count = 0

                  
          //當輸入字符串不為null且長度大于1時進行查找,否則直接返回0 
                  if (orgStr != null && orgStr.length() > 1
                      
          //得到輸入字符串的長度 
                      int size = orgStr.length(); 
                      
          //字符串的頭字符索引 
                      int head; 
                      
          //字符串從后向前遍歷時的"尾"字符索引,即當前字符索引 
                      int current; 
                      
          //字符串的頭字符 
                      char hStr; 
                      
          //字符串從后向前遍歷時的"尾"字符 
                      char cStr; 

                      
          //從前開始遍歷字符串 
                      for (head = 0; head < size; head++
                          
          //取得頭字符 
                          hStr = orgStr.charAt(head); 
                          
          //指向輸入字符串的最后 
                          current = size - 1
                          
          //當尾字符索引等于頭字符索引時退出循環 
                          while (current > head) 
                              
          //取得尾字符 
                              cStr = orgStr.charAt(current); 
                              
          //如果頭尾字符相等,則繼續判斷 
                              if (hStr == cStr) 
                                  
          //取出頭尾中間的子字符串,對其進行分析 
                                  String newStr = orgStr.substring(head + 1, current); 
                                  
          //如果此子字符串的長度大于1,則進行遞歸 
                                  if (newStr.length() > 1
                                      
          //遞歸得到此子字符串中對稱的字符串個數 
                                      count += findSymmetryStr(newStr); 
                                  
          //如果此子字符串只有1個或0個字符,則表明原頭尾字符和此單個字符組成對稱字符串 
                                  else 
                                      count
          ++
                                  
          //將尾字符索引向前推1位 
                                  current--

                              }
           
                              
          //如果頭尾字符不相等,則將尾字符索引向前推1位 
                              else 
                                  current
          --
                              }
           
                          }
           
                      }
           
                  }
           

                  
          return count; 
              }
           

              
          //測試程序 
              public static void main(String args[]) 
                  
          int count = findSymmetryStr("cddcbcbeffeghg");// 
                  System.out.println("symmetry string count is : " + count); 
              }
           
          }


          ----2008年12月22日
          posted on 2010-09-01 11:13 李 明 閱讀(2206) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 开远市| 偏关县| 马公市| 门源| 沽源县| 隆德县| 望城县| 新源县| 视频| 连城县| 渝北区| 陇川县| 车致| 游戏| 双峰县| 巴青县| 石阡县| 海丰县| 兖州市| 盱眙县| 海晏县| 菏泽市| 兰州市| 濮阳市| 新巴尔虎右旗| 白城市| 海晏县| 三门县| 冷水江市| 南和县| 合江县| 峨边| 南京市| 渑池县| 潼关县| 玛纳斯县| 冀州市| 黄浦区| 孟州市| 通州市| 东丰县|