zoukankan      html  css  js  c++  java
  • strStr

     1 class Solution {
     2     /**
     3      * Returns a index to the first occurrence of target in source,
     4      * or -1  if target is not part of source.
     5      * @param source string to be scanned.
     6      * @param target string containing the sequence of characters to match.
     7      */
     8     public int strStr(String source, String target) {
     9         // write your code here
    10         if (source == null && target == null){
    11             return -1;
    12         }
    13         if (source == null){
    14             return -1;
    15         }
    16         if (target == null){
    17             return -1;
    18         }
    19         for (int i = 0; i < source.length() - target.length() + 1; i++){
    20             int j = 0;
    21             for ( ; j < target.length(); j++){
    22                 if (source.charAt(i + j) != target.charAt(j)){
    23                     break;
    24                 }
    25             }
    26             if (j == target.length()){
    27                     return i;
    28                 }
    29         }
    30         return -1;
    31     }
    32 }
  • 相关阅读:
    CF-807B
    CF-807A
    sort()的升降序函数操作
    CF-805D
    CF-805B
    CF-805A
    CF-796C
    CF-796B
    图论学习四之Disjoint set union并查集
    图论学习三之Shortest Path最短路
  • 原文地址:https://www.cnblogs.com/CuiHongYu/p/7070079.html
Copyright © 2011-2022 走看看