码迷,mamicode.com
首页 > 其他好文 > 详细

Implement strStr()

时间:2015-07-03 20:28:49      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:

称号

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

方法

仅仅须要遍历一遍就可以。

	    public String strStr(String haystack, String needle) {
	    	
	        int lenH = haystack.length();
	        int lenN = needle.length();	    
	        if(lenH < lenN ){
	            return null;
	        }
	        
	        for(int i = 0; i < lenH - lenN + 1; i ++){
	            
	            int j = 0;
	            while(j < lenN){
	                if(haystack.charAt(i + j) != needle.charAt(j)){
	                    break;
	                }
	                j ++;
	            }
	            if(j == lenN){
	                return haystack.substring(i, lenH);
	            }
	        }
	        return null;
	    }



版权声明:本文博客原创文章,博客,未经同意,不得转载。

Implement strStr()

标签:

原文地址:http://www.cnblogs.com/lcchuguo/p/4619461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!