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. Linux 网卡操作与安全初始化

    一.Linux网络相关概念和修改IP地址的方法 1.1 网卡命名方式 CENTOS6的网卡命名方式: 它会根据情况有所改变而非唯一且固定,在CENTOS6之前,网络接口使用连续号码命名: eth0. ...

  2. Java微信开发_03_使用测试号进行开发

    今天进行自定义菜单的开发时,发现公众号没有自定义菜单的权限.于是想到用测试号,但微信服务器如何区分你要请求的是公众号还是测试号呢. 我们可以发现不同公众号的appID和appsecre是不同的,一对a ...

  3. Struts2 - 与 Servlet 耦合的访问方式访问web资源

    •       直接访问 Servlet API 将使 Action 与 Servlet 环境耦合在一起,  测试时需要有 Servlet 容器, 不便于对 Action 的单元测试. •       ...

  4. Android数据传递的五种方法汇总

    Android开发中,在不同模块(如Activity)间经常会有各种各样的数据需要相互传递,我把常用的几种 方法都收集到了一起.它们各有利弊,有各自的应用场景. 我现在把它们集中到一个例子中展示,在例 ...

  5. git branch detached from jb4.2.2_1.0.0-ga

    /*************************************************************************** * git branch detached f ...

  6. urllib,urlib2与httplib,urllib3

    urllib:编码参数离不开urllib,urllib.urlencode, urllib.urlopen(URL,[,data]) 支持POST,根据参数区分post或者get urllib2:发送 ...

  7. POJ1904 King's Quest

    King's Quest Language:Default King's Quest Time Limit: 15000MS Memory Limit: 65536K Total Submission ...

  8. POJ3784:Running Median

    浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=3784 用一个"对顶堆& ...

  9. grep的用法(转)

    grep参数 -c  : 显示匹配的行数(就是显示有多少行匹配了): -n :显示匹配内容所在文档的行号: -i  :匹配时忽略大小写: -s :错误信息不输出: -v :输出不匹配内容: -o : ...

  10. NSArray用法

    //类方法初始化一个数组对象 [array count] : 得到这个数组对象的长度. [array objectAtIndex index]: 传入数组的索引(index) 得到数据对象. [arr ...