Java实现 strStr() 函数。 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。

标题:Java实现 strStr() 函数。 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。

一、题目:
实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
在这里插入图片描述

二、题解
方法一:

/**
	 * 逐个逐个比较n(n-L)   3ms
	 * @param a
	 * @param b
	 * @return
	 */
	public int strStr02(String a, String b) {
		if(null==a || null==b) {
			return -1;
		}
        if(0==b.length()) {
			return 0;
		}
        if(a.length()<b.length()){
            return -1;
        }
		boolean flag=true;
		for(int i=0;i<=a.length()-b.length();i++) {
			flag=true;
			for(int j=0;j<b.length();j++) {
				if(a.charAt(i+j)!=b.charAt(j)) {  //注意越界
					flag=false;
					break;
				}
			}
			if(flag) {
				return i;
			}
		}
		
		return -1;
    }

方法二、方法一个简化版,使用了substring();

/**
	 * 逐个逐个比较n(n-L)    1ms
	 * @param a
	 * @param b
	 * @return
	 */
	public int strStr022(String a, String b) {
		if(null==a || null==b) {
			return -1;
		}
        if(0==b.length()) {
			return 0;
		}
        if(a.length()<b.length()){
            return -1;
        }
        
		for(int i=0;i<=a.length()-b.length();i++) {
			if(a.substring(i, b.length()+i).equals(b)) {
				return i;
			}
		}
		
		return -1;
    }

方法三:方法二的精确版

/**
	 * 逐个逐个比较n(n-L)    1ms
	 * @param a
	 * @param b
	 * @return
	 */
	public int strStr03(String a, String b) {
		if(null==a || null==b) {
			return -1;
		}
        if(0==b.length()) {
			return 0;
		}
        if(a.length()<b.length()){
            return -1;
        }
        
		for(int i=0;i<=a.length()-b.length();i++) {
			if(a.charAt(i)!=b.charAt(0)) {
				continue;
			}
			if(a.substring(i, b.length()+i).equals(b)) {
				return i;
			}
		}
		
		return -1;
    }

完整代码如下:

/**
 * 实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。
 * @author dell
 *
 */
public class TestTwoStrToFirstStrIndex {
	/**
	 *    6ms
	 * @param haystack
	 * @param needle
	 * @return
	 */
	public int strStr(String haystack, String needle) {
		if(null==haystack || null==needle) {
			return -1;
		}
        if(0==needle.length()) {
			return 0;
		}
		int j=0;
		int index=-1;
		boolean flag=false;
		for(int i=0;i<haystack.length();) {
			if(haystack.charAt(i)!=needle.charAt(j)) {
				if(!flag) {//一直没有碰到相等的元素
					i++;
				}else {//前面一个元素相等,后面的不相等,故j回到起点,i回到相等点的后一个位置
					flag=false;
					i=index+1;
					j=0;
				}
			}else {//碰到相等的元素
				i++;
				j++;
				if(!flag) {
					index=i-1;
					flag=true;
				}
				if(j==needle.length()) {
					return i-j;
				}
			}
		}
		
		return -1;
    }
	@Test
	public void test() {
//		String haystack="baccd";
//		String needle="cd";

//		String haystack="";
//		String needle="";

		String haystack="mississippi";
		String needle="issip";
		int index = this.strStr(haystack, needle);
		System.out.println("index:"+index);
	}
	
	/**
	 * 逐个逐个比较n(n-L)   3ms
	 * @param a
	 * @param b
	 * @return
	 */
	public int strStr02(String a, String b) {
		if(null==a || null==b) {
			return -1;
		}
        if(0==b.length()) {
			return 0;
		}
        if(a.length()<b.length()){
            return -1;
        }
		boolean flag=true;
		for(int i=0;i<=a.length()-b.length();i++) {
			flag=true;
			for(int j=0;j<b.length();j++) {
				if(a.charAt(i+j)!=b.charAt(j)) {  //注意越界
					flag=false;
					break;
				}
			}
			if(flag) {
				return i;
			}
		}
		
		return -1;
    }
	@Test
	public void test02() {
//		String haystack="baccd";
//		String needle="cd";

//		String haystack="";
//		String needle="";

		String haystack="mississippi";
		String needle="issip";
		int index = this.strStr02(haystack, needle);
		System.out.println("index:"+index);
	}
	
	/**
	 * 逐个逐个比较n(n-L)    1ms
	 * @param a
	 * @param b
	 * @return
	 */
	public int strStr022(String a, String b) {
		if(null==a || null==b) {
			return -1;
		}
        if(0==b.length()) {
			return 0;
		}
        if(a.length()<b.length()){
            return -1;
        }
        
		for(int i=0;i<=a.length()-b.length();i++) {
			if(a.substring(i, b.length()+i).equals(b)) {
				return i;
			}
		}
		
		return -1;
    }
	@Test
	public void test022() {
//		String haystack="baccd";
//		String needle="cd";

//		String haystack="";
//		String needle="";

		String haystack="mississippi";
		String needle="issip";
		int index = this.strStr022(haystack, needle);
		System.out.println("index:"+index);
	}
	
	/**
	 * 逐个逐个比较n(n-L)    1ms
	 * @param a
	 * @param b
	 * @return
	 */
	public int strStr03(String a, String b) {
		if(null==a || null==b) {
			return -1;
		}
        if(0==b.length()) {
			return 0;
		}
        if(a.length()<b.length()){
            return -1;
        }
        
		for(int i=0;i<=a.length()-b.length();i++) {
			if(a.charAt(i)!=b.charAt(0)) {
				continue;
			}
			if(a.substring(i, b.length()+i).equals(b)) {
				return i;
			}
		}
		
		return -1;
    }
	@Test
	public void test03() {
//		String haystack="baccd";
//		String needle="cd";

//		String haystack="";
//		String needle="";

		String haystack="mississippi";
		String needle="issip";
		int index = this.strStr03(haystack, needle);
		System.out.println("index:"+index);
	}
	
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值