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. 剑指offer之 数组中出现次数超过一半的数字

    public class Solution { public int MoreThanHalfNum_Solution(int [] array) { if(array==null||array.le ...

  2. Elasticsearch核心知识大纲脑图

  3. 2015年SCI收录遥感期刊28种目录

    链接地址:http://blog.sciencenet.cn/blog-57081-928025.html

  4. form表单验证失败,阻止表单提交

    form表单验证失败,阻止表单提交 效果演示: 贴上完整代码: <!DOCTYPE html> <html lang="en"> <head> ...

  5. java 中的拦截器和过滤器

    区别: 1.拦截器是基于java的反射机制的,而过滤器是基于函数回调 2.过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 3.拦截器只能对action请求起作用,而过滤器则可以对几 ...

  6. 分享知识-快乐自己:Struts2 前台日期到后台的日期格式转换

    案例目录: 关键代码展示: DateConverter: package com.mlq.util; import com.opensymphony.xwork2.conversion.TypeCon ...

  7. cf 429B Working out(简单dp)

    B. Working out time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  8. Unity3D之Mesh(一)绘制三角形

    前言: Unity自带几种简单的模型,如cube等:一般情况下,其余模型有3D建模软件生成,以合适的文件格式导入unity中:而mesh(以我目前很粗浅的了解)的一般用途就是:对现有的模型进行变形,以 ...

  9. C趣味题目

    http://www.cnblogs.com/lua5/archive/2010/12/05/1896755.html   c语言趣味题目 http://www.cppblog.com/OnTheWa ...

  10. 本地未安装Oracle数据库,如何连接远程Oracle数据库

    方法一:用Navicat Premium连接 注意,这里用的要是黄色的版本,而不是只针对Mysql的绿色版本 工具栏选择[工具]-[选项],点击[其他-OCI]    你会发现有个OCI librar ...