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

http://blog.csdn.net/runningtortoises/article/details/45667569


题目大意:

给两个字符串,求第二个字符串在第一个字符串中出现的最小位置,如果没有出现则输出-1


Java源代码(323ms):

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Solution {  
  2.     public int strStr(String haystack, String needle) {  
  3.         char[] chs1=haystack.toCharArray();  
  4.         char[] chs2=needle.toCharArray();  
  5.         int len1=chs1.length,len2=chs2.length;  
  6.         int[] next=new int[len2+1];  
  7.         getNext(chs2,next,len2);  
  8.         int i=0,j=0;  
  9.         while(i<len1 && j<len2){  
  10.             if(j==-1 || chs1[i]==chs2[j]){  
  11.                 i++;j++;  
  12.             }else{  
  13.                 j=next[j];  
  14.             }  
  15.         }  
  16.         if(j<len2)return -1;  
  17.         return i-len2;  
  18.     }  
  19.     private void getNext(char[] chs,int[] next,int len){  
  20.         int i=0,j=-1;  
  21.         next[0]=-1;  
  22.         while(i<len){  
  23.             if(j==-1 || chs[i]==chs[j]){  
  24.                 i++;j++;  
  25.                 next[i]=j;  
  26.             }else{  
  27.                 j=next[j];  
  28.             }  
  29.         }  
  30.     }  
  31. }  


C语言源代码(2ms):

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. void getNext(char *needle,int* next){  
  2.     int i=0,j=-1;  
  3.     next[0]=-1;  
  4.     while(needle[i]){  
  5.         if(j==-1 || needle[j]==needle[i]){  
  6.             j++;  
  7.             i++;  
  8.             next[i]=j;  
  9.         }else{  
  10.             j=next[j];  
  11.         }  
  12.     }  
  13. }  
  14. int strStr(char* haystack, char* needle) {  
  15.     int length=strlen(needle),i=0,j=0;  
  16.     int *next=(int*)malloc(sizeof(int)*(length+1));  
  17.     getNext(needle,next);  
  18.     while(j==-1 || (haystack[i] && needle[j])){  
  19.         if(j==-1 || haystack[i]==needle[j]){  
  20.             j++;i++;  
  21.         }else{  
  22.             j=next[j];  
  23.         }  
  24.     }  
  25.     if(needle[j])return -1;  
  26.     else return i-length;  
  27. }  

C++源代码(8ms):

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class Solution {  
  2. public:  
  3.     int strStr(string haystack, string needle) {  
  4.         int len1=haystack.size(),len2=needle.size();  
  5.         int* next=(int*)malloc(sizeof(int)*(len2+1));  
  6.         getNext(needle,next,len2);  
  7.         int i=0,j=0;  
  8.         while(i<len1 && j<len2){  
  9.             if(j==-1 || haystack[i]==needle[j]){  
  10.                 i++;j++;  
  11.             }else{  
  12.                 j=next[j];  
  13.             }  
  14.         }  
  15.         if(j<len2)return -1;  
  16.         else return i-len2;  
  17.     }  
  18. private:  
  19.     void getNext(string needle,int* next,int len1){  
  20.         int i=0,j=-1;  
  21.         next[0]=-1;  
  22.         while(i<len1){  
  23.             if(j==-1 || needle[i]==needle[j]){  
  24.                 j++;i++;  
  25.                 next[i]=j;  
  26.             }else{  
  27.                 j=next[j];  
  28.             }  
  29.         }  
  30.     }  
  31. };  

Python源代码(74ms):

[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class Solution:  
  2.     # @param {string} haystack  
  3.     # @param {string} needle  
  4.     # @return {integer}  
  5.     def strStr(self, haystack, needle):  
  6.         len1=len(haystack);len2=len(needle)  
  7.         next=[-1 for i in range(len2+1)]  
  8.         self.getNext(needle,next,len2)  
  9.         i=0;j=0  
  10.         while i<len1 and j<len2:  
  11.             if j==-1 or haystack[i]==needle[j]:  
  12.                 i+=1;j+=1  
  13.             else:j=next[j]  
  14.         if j<len2:return -1  
  15.         else:return i-len2  
  16.     def getNext(self,needle,next,len2):  
  17.         i=0;j=-1  
  18.         while i<len2:  
  19.             if j==-1 or needle[i]==needle[j]:  
  20.                 i+=1  
  21.                 j+=1  
  22.                 next[i]=j  
  23.             else:j=next[j]  

http://www.programcreek.com/2012/12/leetcode-implement-strstr-java/


Java Solution 1 - Naive

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

Java Solution 2 - KMP

Check out this article to understand KMP algorithm.

public int strStr(String haystack, String needle) {
        if(haystack==null || needle==null)    
            return 0;
 
	int h = haystack.length();
	int n = needle.length();
 
	if (n > h)
		return -1;
	if (n == 0)
		return 0;
 
	int[] next = getNext(needle);
	int i = 0;
 
	while (i <= h - n) {
		int success = 1;
		for (int j = 0; j < n; j++) {
			if (needle.charAt(0) != haystack.charAt(i)) {
				success = 0;
				i++;
				break;
			} else if (needle.charAt(j) != haystack.charAt(i + j)) {
				success = 0;
				i = i + j - next[j - 1];
				break;
			}
		}
		if (success == 1)
			return i;
	}
 
	return -1;
}
 
//calculate KMP array
public int[] getNext(String needle) {
	int[] next = new int[needle.length()];
	next[0] = 0;
 
	for (int i = 1; i < needle.length(); i++) {
		int index = next[i - 1];
		while (index > 0 && needle.charAt(index) != needle.charAt(i)) {
			index = next[index - 1];
		}
 
		if (needle.charAt(index) == needle.charAt(i)) {
			next[i] = next[i - 1] + 1;
		} else {
			next[i] = 0;
		}
	}
 
	return next;
}

http://blog.csdn.net/zz198808/article/details/7883588



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值