A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王海旺 中级黑马   /  2013-7-20 21:49  /  1068 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

cd 是abcd 的子字符串;ac不是abcd的子字符串;
又如
isSubString("The", "The cat in the hat.") is true
isSubString("hat.", "The cat in the hat.") is true

现在我写了一个Search类
Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Search {
    //定义计数器
    private int i = 0 ;
    private int j = 0 ;
    //第一个字母
    private boolean first ;
      
    public boolean searchChar(String target,String source){
            for(;j<source.length() - target.length() + 1;j++){
                if(target.charAt(0) == source.charAt(j) ){
                    first = true;
                    break;
                }
            }
              
            if( first == true ){
                if(target.length() == 1){
                    return true;
                }
                else{
                    for(i=1,j++;i<target.length();i++,j++){
                        if(target.charAt(i) != source.charAt(j)){
                            return false;
                        }
                    }
                    return true;
                }
            }
            else{
                return false;
            }
    }
}





但是对于isSubString("hat.", "The cat in the hat.") is true   
hat 对于后者  因为有两个h,所以无法实现,那么我的代码该如何修改?

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1 你的代码怎么回事

查看全部评分

2 个回复

倒序浏览
{:soso_e156:}
回复 使用道具 举报
本帖最后由 刘张朋 于 2013-7-21 00:57 编辑

我做了一下,你可以参考一下
  1. package stringtest;

  2. public class SubStringTest {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args) {

  7.                 System.out.println(new SubStringTest().isSubString("aabc", "mabcbjcaabc"));
  8.         }
  9.         /*我的思路是:如果找到与target首字母与source的某字母相同,那么角标x与y的都+1;如果target某个字母比较到中途
  10.          * 又判断到source的对应字母与不同,那么x值0;同时y的角标不变,相对于重新比较;如果target的第一个字母就没找到相同的,那么只有y+1*/
  11.         public boolean isSubString(String target,String source){
  12.                 int lenX = target.length();
  13.                 int lenY = source.length();
  14.                 int x = 0, y= 0;
  15.                
  16.                 for(;;y++){
  17.                      if(target.charAt(0) == source.charAt(y) ){
  18.                            break;   
  19.                      }
  20.                      if(y>=lenY - lenX)//之所以加上大于号,主要是考虑到lenY - lenX小于0的情况
  21.                           return false;
  22.               }
  23.                
  24.                 for(;x<lenX && y<lenY;){
  25.         //                System.out.println(x+":"+y);
  26.                         if(target.charAt(x)==source.charAt(y)){
  27.                                 x++;
  28.                                 y++;
  29.                                 if(x==lenX)
  30.                                         return true;        //target以及比较到末尾相同,返回true
  31.                         }
  32.                         else{
  33.                                 
  34.                                 if(x!=0){        //开始比较target与source相同,突然道某一位不同,重新从target比较
  35.                                         x = 0;
  36.                                         continue;
  37.                                 }
  38.                                 y++;        //从target的首位就不相同,只有y+1
  39.                         }
  40.                 }
  41.                 return false;
  42.         }

  43. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马