zoukankan      html  css  js  c++  java
  • Implement strStr()

    称号

    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;
    	    }



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

  • 相关阅读:
    hw笔试题-01
    tcp
    linux schedule
    arm架构学习
    skyeye
    RTOS
    systemdump相关
    sdio
    linux kernel 多线程
    linux内核同步
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4619461.html
Copyright © 2011-2022 走看看