2016.6.18——Implement strStr()

Implement strStr()

本题收获:

1.考虑多种边界条件。

2.haystack.size() size_type 是无符号的,即为正数

  在32位系统上定义为 unsigned int

  在64位系统上定义为 unsigned long

   两个size相减若<0,则直接出现以下情况:

  
  并不是负数,这里就会出现越界。

3.查看多行断点,设置监视来查看相应的值及类型。

  题目:

  Implement strStr().

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

  思路:

    注意题目中并没有说haystack.size() 一定大于needle.size()

    我的思路:暴力搜索,利用两个for循环,逐个对比,没有考虑有些边界条件。

    leetcode:  暴力搜索,但是考虑充分了边界条件

  需要考虑的边界条件: haystack ,needle

  1." " , " "   输出0

  2." " , "a"  输出 -1

  3."a" , " "  输出0

  4."mississippi" , "issip"  输出4

  5."aa" , "aaaa" 输出-1

  6."a" , "a" 输出0,注意正常时,是从0开始还是1开始(第一个位置到底是0还是1)

  代码:正确的代码

 1 class Solution {
 2 public: 
 3     int strStr(string haystack, string needle) {
 4         int m = haystack.length(), n = needle.length();
 5         if (!n) return 0;
 6         for (int i = 0; i < m - n + 1; i++) {
 7             int j = 0;
 8             for (; j < n; j++)
 9                 if (haystack[i + j] != needle[j])    
10                     break;
11             if (j == n) return i;
12         }
13         return -1;
14     }
15 };

   特殊情况:haystack:mississippi  needle:issip 

  我的代码:

  1.思路有问题

 1 class Solution {
 2 public:
 3     int strStr(string haystack, string needle) {
 4         if (haystack.size() == 0 && needle.size() == 0) return 0;
 5         if (haystack.size() != 0 && needle.size() == 0) return 0;
 6         if (haystack.size() == 0 && needle.size() != 0) return -1;
 7         int n = 0;
 8         int j = needle.size();
 9         for (size_t i = 0; i < haystack.size(); i++)
10         {
11             for (size_t j = 0; j < needle.size(); j++)
12             {
13                 if (needle[j] != haystack[i])
14                 {
15                     j = 0;
16                     n++;
17                     break;
18                 }
19             }
20             return n - needle.size() + 1;
21         }
22         
23     }
24 };

  很多边界条件都不符合,每次测试加一个if语句,代码冗余度越来越高,如果加了两个if语句还有错就需要考虑是不是思路的问题了。

  测试全代码:

  有个问题:我不用m = haystack.size(),和n = needle.size()时,需要加if语句判断haystack.size(),needle.size()的大小,但是用m,n后就不需要加if语句,为什么????

  

  相减在加1后超过 unsigned long的取值范围(unsigned long 0~4294967295),故会出现如下报错——指针越界

  

 1 // Implement strStr().cpp : 定义控制台应用程序的入口点。
 2 //考虑多点测试条件
 3 //
 4 
 5 #include "stdafx.h"
 6 #include "iostream"
 7 #include "stack"
 8 using namespace std;
 9 
10 class MyClass
11 {
12 public:
13     int strStr(string haystack, string needle)            //师兄的代码中哪里隐含了haystack.size() >= needle.size()????
14     {
15         if (needle.size() == 0) return 0;
16         int m = haystack.size(), n = needle.size();        //为什么修改之后就可以了 
17         //if (haystack.size() >= needle.size())
18         //{
19             for (int i = 0; i < m-n+1; i++)
20             {
21                 int j = 0;
22                 for (; j < needle.size(); j++)
23                 {
24                     if (haystack[i + j] != needle[j])
25                         break;
26                 }
27                 if (j == needle.size())
28                 {
29                     return i + 1;
30                 }
31             }
32         //}
33         return -1;
34     }
35 };
36 
37 /*
38 class MyClass {
39 public:
40     int strStr(string haystack, string needle) {
41         int m = haystack.length(), n = needle.length();
42         if (!n) return 0;
43         for (int i = 0; i < m - n + 1; i++) {
44             int j = 0;
45             for (; j < n; j++)
46                 if (haystack[i + j] != needle[j])
47                 break;
48             if (j == n) return i;
49         }
50         return -1;
51     }
52 };*/
53 
54 
55 int _tmain(int argc, _TCHAR* argv[])
56 {
57     string hay = "abb";
58     string need = "abaaa";
59     MyClass solution;
60     int m = 0;
61     m = solution.strStr(hay, need);
62     cout << m << endl;
63     system("pause");
64     return 0;
65 }

   调试:

  1.设置断点,在行的前面单击即可设置断点,在单击以下就删除断点,如下图:

      

  2.设置完一个断点后,若想看其他的值,重新设置断点比较麻烦,可以用菜单中的逐语句(F10),每次可以向下一行,查看相应值。

      

  3.设置监视,设置完断点之后,在添加监视,可以看到,需要监视的元素目前的值。(一定是在添加完断点之后,如果添加完后,重新调试则不会出现相应的值,按下”监视“-“值”中的刷新即可,)

      

posted on 2016-06-18 18:25  zhuzhu2016  阅读(141)  评论(0编辑  收藏  举报

导航