love fish大鵬一曰同風(fēng)起,扶搖直上九萬里

          常用鏈接

          統(tǒng)計

          積分與排名

          friends

          link

          最新評論

          最弱智的搜索字符串算法

          package liaojiyong.net.blogjava.impl;

          /**
           * 
          @author liaojy
           * 下午03:03:35
           
          */

          class FindSubStringImpl{

              
          public int find(String sub, String main) {
                  
          char[] s = sub.toCharArray();
                  
          char[] m = main.toCharArray();
                  
          int subLength = sub.length();
                  
          int mainLength = main.length();
                  
          for (int i = 0; i < mainLength; i++{
                      
          if (m[0!= s[0])
                          
          while (++< mainLength && m[i] != s[0])
                              ;
                      
          if (i == mainLength) {
                          
          return -1;
                      }

                      
          int t = i;
                      
          for (int j = 1; j < subLength; j++{
                          t
          ++;
                          
          if (t >= mainLength)
                              
          break;
                          
          if ((m[t] == s[j]) && (j == subLength - 1)) {
                              
          return i;
                          }
           else if ((m[t] != s[j])) {
                              
          break;
                          }

                      }

                  }

                  
          return -1;
              }


              
          /**
               * 
          @param args
               * 
          @throws InstantiationException
               * 
          @throws IllegalAccessException
               
          */

              
          public static void main(String[] args) throws InstantiationException, IllegalAccessException {
                  System.out.println(FindSubStringImpl.
          class.newInstance().find("liao""maliaoinjiyong"));
              }

          }

          posted on 2007-04-27 15:12 liaojiyong 閱讀(919) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 伽师县| 仙桃市| 高州市| 赤峰市| 靖江市| 廉江市| 乌什县| 长汀县| 德令哈市| 通化县| 九江县| 柳州市| 城固县| 兴仁县| 甘孜| 石河子市| 五寨县| 东辽县| 玉屏| 通渭县| 兴义市| 郸城县| 三穗县| 泗水县| 涞源县| 五指山市| 邹城市| 承德市| 济阳县| 江永县| 德惠市| 宝丰县| 留坝县| 辽中县| 竹溪县| 乡城县| 大名县| 抚远县| 舞钢市| 盐亭县| 潮州市|