28、Implement strStr()

Implement strStr().

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

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().

代码:

static void Main(string[] args)
{
var haystack = "hello";
var needle = "ll"; var res = ImplementstrStr(haystack, needle);
Console.WriteLine(res);
Console.ReadKey();
} private static int ImplementstrStr(string haystack, string needle)
{
var match = true;
for (var i = ; i <= haystack.Length - needle.Length; i++)
{
match = true;
for (var j = ; j < needle.Length; j++)
{
if (haystack[i + j] != needle[j])
{
match = false;
break;
}
}
if (match) return i;
}
return -;

解析:

输入:字符串

输出:匹配位置

思想:定义一个匹配变量match,从首字母循环,使用内循环逐一判断是否每个字母都能匹配,若不匹配立刻退出内循环。

时间复杂度:O(m*n)  m和n分别是两个字符串长度。

C# 写 LeetCode easy #28 Implement strStr()的更多相关文章

  1. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  2. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

  3. 【LeetCode】28. Implement strStr() 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...

  4. 【LeetCode】28 - Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  5. 【LeetCode】28. Implement strStr() (2 solutions)

    Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...

  6. 28. Implement strStr()【easy】

    28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...

  7. leetCode练题——28. Implement strStr()

    1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...

  8. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  9. [LeetCode] 28. Implement strStr() 实现strStr()函数

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

随机推荐

  1. SDN关键技术-Segment Routing协议简介

    当前,SDN作为一种新的网络架构,已经成为行业高度关注的热点.其倡导的开放式网络,代表了从网络应用适应网络能力向网络能力主动适配网络应用需求这个网络建设理念的改变.转发与控制分离.集中的控制层面.开放 ...

  2. CSS3特殊图形制作

    CSS3特殊图形制作 现在IE8+的浏览器都支持CSS3+HTML5了,IE8及以下的浏览器的浏览器也快告一段落了,大前端的时代来了.废话不多说,现在开始用CSS3画图 1.心型 //HTML < ...

  3. BZOJ 3943 [Usaco2015 Feb]SuperBull:最大生成树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3943 题意: 有n只队伍,每个队伍有一个编号a[i]. 每场比赛有两支队伍参加,然后选一支 ...

  4. css3 多列布局使用

    css3的出现,解决了不少前端的问题,比如动画,圆角等: 这里总结一下css3 的多列布局: w3c上给出了很多属性: 我们一般用到column-count.column-gap.column-wid ...

  5. 双系统重装win7和ubuntu修复win7引导方法介绍(来源百度经验)

    很多朋友喜欢为电脑安装win7和ubuntu双系统,当我们重装双系统时,可能会出现win7引导不见的情况,接下来就告诉大家双系统重装win7和ubuntu修复win7引导的方法. 1.win7和ubu ...

  6. 分享知识-快乐自己:IO流基本操作

    点我参考常用API: IO图解: 相关理论: 流的概念和作用: 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将 ...

  7. cluster KMeans need preprocessing scale????

    Out: n_digits: 10, n_samples 1797, n_features 64 ___________________________________________________ ...

  8. js string.format 方法

    String.prototype.format = function(args) { var result = this; if (arguments.length > 0) { if (arg ...

  9. Tomcat报错:HTTP Status 500 - Wrapper cannot find servlet class

    HTTP Status 500 - Wrapper cannot find servlet class com.servlet.servlet.RegServlet or a class it dep ...

  10. ffmpeg用法(心得体会还有你见过的用法)

    ffmpeg的常用用法很多,我这里提供的用法有可能有许多地方是你没见过的. 一.ffmpeg合并视频 我经常需要切割再把一些零碎的视频给拼接起来,这样可以省许多磁盘空间.其实用mencoder挺不错的 ...