• Implement strStr()


    题目:

    Implement strStr().

    Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

    思路: 指针的操作,大字符串从头到尾走,遇到与小字符串相同的就一起走,不同的就只走大字符串。小字符串走完的时候返回结果。

    代码:

     1 class Solution {
     2   public:
     3       char *strStr(char *haystack, char *needle) {
     4           
     5           int hayLen = strlen(haystack);
     6           int needLen = strlen(needle);
     7           
     8           for(int i = 0; i <= hayLen - needLen; i++)
     9          {
    10              char* p = haystack + i;
    11              char* q = needle;
    12              while(*q != '\0')
    13              {
    14                  if (*p != *q)
    15                      break;
    16                  else
    17                  {
    18                      p++;
    19                      q++;
    20                  }
    21                    
    22              }
    23              if (*q == '\0')
    24                  return haystack + i; 
    25                       
    26          }
    27          
    28          return NULL;
    29      }
    30  };
  • 相关阅读:
    iOS学习-UITextField
    iOS学习-UIButton->Block
    iOS学习-UIButton
    iOS常用技术-微信下标栏
    学习进度表
    学习进度表
    读后感
    我的学习进度表
    调查问卷
    我的学习进度表
  • 原文地址:https://www.cnblogs.com/tanghulu321/p/3064475.html
Copyright © 2020-2023  润新知