<2017年9月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

统计

  • 随笔 - 57
  • 文章 - 7
  • 评论 - 0
  • 引用 - 0

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

Blog

Coder 必备技巧

Compiler for Wurq

搜索

  •  

最新评论

阅读排行榜

评论排行榜

【LeeCode 2017/07/01】28. Implement strStr()
 1 // 子串为空则任意匹配
 2 class Solution {
 3 public:
 4     int strStr(string haystack, string needle) {
 5         int len_s = haystack.length();
 6         int len_t = needle.length();
 7 
 8         bool is_cmp = (haystack == needle) || needle == "";
 9         for (int i = 0; i < len_s; i++)
10         {
11             if (haystack[i] != needle[0])
12                 continue;
13 
14             is_cmp = true;
15             for (int j = 0; j < len_t; j++) {
16                 if (i + j<len_s && haystack[i + j] == needle[j])
17                     continue;
18 
19                 is_cmp = false;
20                 break;
21             }
22 
23             if (is_cmp)
24                 return i;
25         }
26 
27         return is_cmp ? 0 : -1;
28     }
29 };

posted on 2017-07-01 14:04 Wurq 阅读(98) 评论(0)  编辑 收藏 引用 所属分类: 【LeeCode 每日N题】


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理