首页 > 其他 > 详细

LintCode StrStr

时间:2016-05-14 07:53:33      阅读:234      评论:0      收藏:0      [点我收藏+]

1. 讨论目标字符串若为空, 则返回-1; 资源字符串若为空, 则返回-1。

2.讨论目标字符串个数为零, 则返回0; 资源字符串个数为零, 则返回-1。

3. 插入旗帜来使第二循环的结束为有条件地返回(为true才返回, 为false则break跳到上循环继续)。

class Solution {
    /**
     * Returns a index to the first occurrence of target in source,
     * or -1  if target is not part of source.
     * @param source string to be scanned.
     * @param target string containing the sequence of characters to match.
     */
    public int strStr(String source, String target) {
        //write your code here
        if(source == null || target == null) return -1;
        if(target.length() == 0) return 0;
        if(source.length() == 0) return -1;
        
        for(int i = 0; i < source.length(); i++){
            boolean flag = true;
            for( int j = 0; j < target.length(); j++){
                if(source.charAt(i + j) == target.charAt(j)){
                    
                }
                else{
                    flag = false;
                    break;
                }
            }
            
            if(flag) return i;            
        }
        
        return -1;
    }
}

 

LintCode StrStr

原文:http://www.cnblogs.com/LittleAlex/p/5491846.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!