LeetCode 28 Implement strStr() 找到字串返回位置。

题目:

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

翻译:

找到字符串的子串位置,并返回。如果没有则返回-1

思路:

通过最简单的BF遍历,如果不符合则指向下一个字符,最后如果长度等于子串长度,则返回位置。

代码:

 public static int strStr(String haystack, String needle) {
	      if(haystack == null || needle == null)
	    	  return 0;
	      if(haystack.length() < needle.length())
	    	  return -1;
	      int hlen = haystack.length();
	      int nlen = needle.length();
	      for(int i = 0 ; i <= hlen - nlen;i++)
	      {
	    	  int j = 0;
	    	  for(;j < nlen;j++)
	    	  {
	    		  if(needle.charAt(j)!=haystack.charAt(i+j))
	    			  break;
	    	  }
	    	  if(j == nlen)
	    		  return i;
	      }
	      return -1;
	    }

还有一种方法是KMP。虽说前些日子看了KMP,但让自己动手写起来还是难度有点大。

于是,又重新复习了一遍。贴上KMP的算法。

public static void getNext(String str,int next[])
	{
		int j = 0;
		int k = -1;
		next[0] = -1;
		int len = str.length();
		while(j < len-1)
		{
			if(k == -1 || str.charAt(k) == str.charAt(j))
			{
				j++;
				k++;
				next[j] = k;
			}
			else {
				k = next[k];
			}
		}
	}
	public  static  int KMP(String str,String temp)
	{
		int i = 0;
		int j = 0;
		int slen = str.length();
		int tlen = temp.length();
		int []next = new int[tlen];
		getNext(temp, next);
		while(i < slen && j < tlen)
		{
			if(str.charAt(i) == temp.charAt(j))
			{
				i++;
				j++;
			}
			else {
				if(next[j] == -1)
				{
					i++;
					j = 0;
				}
				else {
					j = next[j];
				}
			}
			if(j == tlen)
				return i - j;
		}
		return -1;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		String str = "ABC ABCDAB ABCDABCDABDE";
        String pattern = "ABCDABD";
        int i = KMP(str,pattern);
        System.out.println(i);

	}

原理就是找到next的数组。然后在遍历的时候如果匹配则返回,没有匹配则跳指向next数组对应的位置,继续扫描判断。 还是部分细节的处理掌握不牢固。以后多看看。

后来查看大神的写法,发现了一个不错的。采用rolling hash。

链接:http://blog.csdn.net/linhuanmars/article/details/20276833

基本思想是用一个hashcode来表示一个字符串,为了保证hash的唯一性,我们用比字符集大的素数为底,以这个素数的幂为基。举例来说,字符集是小写字母集,取素数29为底。比如字符串“abacd",转化为hashcode=1+2*29+1*29^2+3*29^3+4*29^4。然后是如何在前进一步的时候计算新的hashcode,比如匹配串是原串是”abacde“,匹配串长度为5,根据以上的方法计算”abacd“的hashcode=h,那么下一步”bacde“的hashcode=h/29+5*29^4。这是一个constant的操作,所以检测所有子串的时间复杂度只需要O(m+n-m)=O(n),也是一个线性算法。

public String strStr(String haystack, String needle) {
    if(haystack==null || needle==null) return null;
    if(haystack.length()==0){
        return needle.length()==0?"":null;
    }
    if(needle.length()==0) return haystack;
    if(haystack.length()<needle.length()) return null;  

 int base = 29;
 long patternHash = 0;
    long tempBase = 1;  

    for(int i=needle.length()-1; i>=0; i--){
     patternHash += (int)needle.charAt(i)*tempBase;
     tempBase *= base;
    }  

    long hayHash = 0;
    tempBase = 1;
    for(int i=needle.length()-1; i>=0; i--){
     hayHash += (int)haystack.charAt(i)*tempBase;
     tempBase *= base;
    }
    tempBase /= base;  

    if(hayHash == patternHash){
     return haystack;
    }  

    for(int i=needle.length(); i<haystack.length(); i++){
     hayHash = (hayHash - (int)haystack.charAt(i-needle.length())*tempBase)*base+(int)haystack.charAt(i);
        if(hayHash == patternHash){
      return haystack.substring(i-needle.length()+1);
     }
    }
    return null;
}   
时间: 2024-10-13 01:26:40

LeetCode 28 Implement strStr() 找到字串返回位置。的相关文章

leetCode 28. Implement strStr() 字符串

28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 在haystack中找与needle 第一个相匹配的位置.如果找不到,返回-1. 代码如下: class Solution { public:     int strStr(string haystac

44. leetcode 28. Implement strStr()

28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路:子串匹配,朴素匹配.

LeetCode 28. Implement strStr()

https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 字符串简单题.一定要写的没BUG. 如果不在函数最后一行写return语句的话,LeetCode会出RUNTIME ERROR. Line 27: co

[LeetCode] 28. Implement strStr() ☆

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 解法: 这道题让我们在一个字符串中找另一个字符串第一次出现的位置,那我们首先要做一些判断,如果子字符串为空,则返回0,如果子字符串长度大于母字符串长度,则返回-1.然后我们开始遍历母字符串,我们并不需要遍历整个母字符串,而是遍历到剩下的长度和子字符

19.1.30 [LeetCode 28] Implement strStr()

Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa",

Java [leetcode 28]Implement strStr()

题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If you

LeetCode 28 Implement strStr() (C,C++,Java,Python)

Problem: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If

[LeetCode] 28. Implement strStr() 实现strStr()函数

Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa",

leetcode 28. Implement strStr() 实现strStr()

C++代码,题目相对不是很难 1 class Solution { 2 public: 3 int strStr(string haystack, string needle) { 4 if(needle.empty()) return 0; 5 int m=haystack.size(),n=needle.size(); 6 int flag=-1; 7 for(int i=0;i<=m-n;i++){ 8 if(haystack[i]==needle[0]){ 9 flag=i; 10 fo