love fish大鵬一曰同風起,扶搖直上九萬里

          常用鏈接

          統(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

          主站蜘蛛池模板: 楚雄市| 郓城县| 团风县| 临邑县| 雅江县| 克什克腾旗| 杨浦区| 罗定市| 呼和浩特市| 平遥县| 文成县| 萨迦县| 昔阳县| 巴林右旗| 蒲江县| 尉犁县| 宁蒗| 正镶白旗| 太仆寺旗| 科技| 太原市| 台东市| 达州市| 桃江县| 通化县| 南木林县| 鄱阳县| 绥中县| 内江市| 巴彦淖尔市| 丰宁| 贵溪市| 澎湖县| 吉安市| 古蔺县| 深水埗区| 宁国市| 洪雅县| 马龙县| 昆明市| 财经|