标签:
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; } }
标签:
原文地址:http://www.cnblogs.com/LittleAlex/p/5491846.html