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

    Implement strStr().

    Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

    给定两个字符串,判断一个字符串是不是另一个字符串的子串,如果是返回第一个字串位置,如果不是返回-1。

     1     public int strStr(String haystack, String needle) {
     2         for (int i = 0; ; i++) {
     3             for (int j = 0; ; j++) {
     4                 if (j == needle.length()) return i;
     5                 if (i + j == haystack.length()) return -1;
     6                 if (haystack.charAt(i + j) != needle.charAt(j)) {
     7                     break;//没有找到结尾就中断了,break以后重头搜索
     8                 }
     9             }
    10         }        
    11     }
  • 相关阅读:
    前端之JavaScript
    前端之CSS
    前端之HTML
    编程总结
    线程
    锁机制,信号机制,事件机制
    并发编程
    struct
    linux查看端口
    vue页面跳转传参
  • 原文地址:https://www.cnblogs.com/wzj4858/p/7679031.html
Copyright © 2011-2022 走看看