• Leetcode OJ : Implement strStr() [ Boyer–Moore string search algorithm ] python solution


     1 class Solution {
     2 public:
     3 int strStr(char *haystack, char *needle) {
     4     
     5     int i = 0 , skip[256];
     6     char *str = haystack, *substr = needle;
     7     int len_src = strlen(str), len_sub = strlen(substr);
     8     // preprocess
     9     for (i = 0; i < 256; i++)
    10         skip[i] = len_sub;
    11     int last = len_sub - 1;
    12     for (i = 0; i < last;i++)
    13         skip[substr[i]] = last - i;
    14     // search
    15     int pos = 0,  j;
    16     while (pos <= len_src-len_sub)  {
    17         j = last;
    18         while (j>=0 && str[pos+j]==substr[j])
    19             j--;
    20         if (j<0) 
    21              return pos;
    22         pos += skip[str[pos+last]];
    23     }
    24     return -1;
    25 }
    26 };
  • 相关阅读:
    K8S-安全机制
    K8S-Service
    K8S-Pod资源管理
    K8S-kubelet启动过程
    每日进度
    每日进度
    每日进度
    每日进度
    每日进度
    归纳及计划
  • 原文地址:https://www.cnblogs.com/ydlme/p/4295725.html
Copyright © 2020-2023  润新知