• 力扣(LeetCode)试题28-实现strStr() C++代码


    没想到的点:若haystack="mississippi",needle = "issip"。按我的匹配算法(在haystack中检索needle的第一个元素,若没有该元素返回-1,若有则搜索后边的元素是否对应,这是有问题的!!),匹配到haystack中前部分issis,程序就结束了,返回了-1,实际应该匹配到issip。缜密的逻辑还需要不断提升呀!(基于此思想改动的代码已经补上,在最后:即在haystack中,第一个i不满足条件,循环退出,找寻第二个i,第三个i,直到满足条件,否则返回-1)

    错误代码记录一下吧,如下

     1 class Solution
     2 {
     3 public:
     4     int strStr(string haystack, string needle)
     5     {
     6         if (needle.length() == 0) return 0;
     7         else {
     8             int i = 0;
     9             while (i < haystack.length())
    10             {
    11                 if (haystack[i] != needle[0])
    12                 {
    13                     i += 1;
    14                 }
    15                 else
    16                     //否则,haystack中存在needle中的第一个元素,然后判断是不是needle整个元素
    17                 {
    18                     int k = 0; //指向needle的头元素
    19                     int limit = needle.length();
    20                     while (k < limit)
    21                     {
    22                         if (haystack[i] != needle[k])
    23                             return -1;
    24                         else
    25                         {
    26                             i += 1;
    27                             k += 1;
    28                         }
    29                     }
    30                     return i - k;//循环完毕,此时i指向了needle最后一个元素,需要减去这个needle的长度
    31                 }
    32             }
    33             return -1;
    34         }
    35     }
    36 
    37 };

    使用substr()方法的代码如下:

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 class Solution
     6 {
     7 public:
     8     int strStr(string haystack, string needle)
     9     {
    10         int needle_size = needle.length();
    11         int haystack_size = haystack.length();
    12         int res = haystack_size - needle_size;
    13         if (needle_size == 0) return 0; //特殊情况
    14         else 
    15         {
    16             //遍历haystack,寻找是否包含needle
    17             for (int i = 0; i <= res; ++i)
    18             {
    19                 string str = haystack.substr(i, needle_size);//substr从第i个位置截取,截取的长度为needle_size
    20                 if (str == needle) return i;
    21                 else continue;
    22             }
    23             return -1;
    24         }
    25     }
    26 
    27 };
    28 
    29 int main()
    30 {
    31     string haystack = "mississippi";
    32     string needle = "issip";
    33     Solution sol;
    34     int p;
    35     p = sol.strStr(haystack, needle);
    36     cout << p << endl;
    37     int u;
    38     cin >> u;
    39     return 0;
    40 }

     

    修改正确的代码如下:(啊,头秃)

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 class Solution
     6 {
     7 public:
     8     int strStr(string haystack, string needle)
     9     {
    10         int haystack_size = haystack.length();
    11         int needle_size = needle.length();
    12         int res = haystack_size - needle_size;
    13 
    14         if (needle_size == 0) return 0;//特殊情况
    15 
    16         else {
    17             int i = 0;//指针,指向haystack中的元素
    18             while (i <= res) //外层循环,用于找needle头元素在haystack中的位置。
    19             {
    20                 if (haystack[i] != needle[0])
    21                 {
    22                     i += 1;
    23                 }
    24                 else
    25                     //在haystack中找到了needle的第一个元素,然后判断之后的元素是否属于needle
    26                 {
    27                     int count = 0;//记录循环比对次数
    28                     int k = 0; //指向needle的头元素
    29                     while (count<=needle_size)
    30                     {
    31                         //进入循环,比对该元素之后是否是needle所含元素,循环次数必不大于needle长度
    32                         if (haystack[i] != needle[k])//该元素之后的元素不属于needle
    33                         {
    34                             i = i - count + 1; //将i返回,指向haystack中下一个元素
    35                             break;//跳出此循环,去haystack中寻找下一个与needle头元素相同的元素下标
    36                         }
    37                         i += 1;//指针各加一,判断下一对儿元素是否一致
    38                         k += 1;
    39                         count += 1;//循环次数+1
    40                         if (count == needle_size)//循环次数=needle长度,则说明一一对应,此时i指向尾巴元素
    41                             return i - needle_size;
    42                         
    43                     }
    44                 }
    45             }
    46             return -1;
    47         }
    48     }
    49 
    50 };
    51 
    52 int main()
    53 {
    54     string haystack = "mississippi";
    55     string needle = "issip";
    56     Solution sol;
    57     int p;
    58     p = sol.strStr(haystack, needle);
    59     cout << p << endl;
    60     int u;
    61     cin >> u;
    62     return 0;
    63 }

  • 相关阅读:
    AndroidAlarmManager(全局定时器/闹钟)
    Android dialog,activity 屏蔽Home键的教程详解
    实例教程一:电话拨号器
    Android的进程优先级与进程回收详解
    Android Bitmap内存限制
    【30篇突击 android】源码统计七
    在access中支持2个以上left join的方法,又是access的变态规则
    修改vs2005的键盘风格设置
    IE与Firefox的CSS兼容大全(转载)
    实现页面内多个表格在滚动时,表头浮动的效果(是同时多个表格哟)
  • 原文地址:https://www.cnblogs.com/pgzhanglin/p/13234279.html
Copyright © 2020-2023  润新知