【 声明:版权全部,转载请标明出处,请勿用于商业用途。  联系信箱:libin493073668@sina.com】



题目链接:https://leetcode.com/problems/implement-strstr/



题意:

给定两个串,判定needle串是否haystack串的子串,假设是,返回匹配的起始位置。假设不是,则返回-1



思路:

直接两个循环暴力解决

class Solution
{
public:
int strStr(string haystack, string needle)
{
int len1 = haystack.length(),len2 = needle.length();
int i,j;
if(len2>len1) return -1;
if(len1 == len2 && haystack!=needle) return -1;
for(i = 0; i<=len1-len2; i++)
{
for(j = 0; j<len2; j++)
{
if(haystack[i+j]!=needle[j])
break;
}
if(j == len2)
return i;
}
return -1;
}
};

[LeedCode OJ]#28 Implement strStr()的更多相关文章

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

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

  2. 44. leetcode 28. Implement strStr()

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

  3. 28. Implement strStr()【easy】

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

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

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

  5. C# 写 LeetCode easy #28 Implement strStr()

    28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...

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

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

  7. 28. Implement strStr()

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

  8. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  9. 【LeetCode】28 - Implement strStr()

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

随机推荐

  1. mysql远程访问另一台主机数据库表,实现小表广播功能

    1.打开navicat,打开任意一个连接,新建一个查询,输入命令 show engines,出现如下界面 2. 如果FEDERATED对应的Support值为NO,则找到C:\ProgramData\ ...

  2. 【02】你是如何理解 HTML 语义化的,有什么好处

    [02]你是如何理解 HTML 语义化的   01,语义化,就是通过HTML标签来表示页面包含的信息. 02,其中有HTML标签的语义化和CSS命名的语义化. 03,HTML标签语义化的的含义是:   ...

  3. 【编程工具】Sublime Text3 之 Emmet 插件的详细使用的方法

    这篇关于 Emmet 插件使用的博文之前就想写了,今天刚好闲暇时间,就花了一些时间进行了总结. 我们都这道 Emmet 这款插件在前端设计里被称为神器,确实,神器称号名不虚传.因为这款插件可以帮助我们 ...

  4. BNUOJ 1207 滑雪

    滑雪 Time Limit: 1000ms Memory Limit: 65536KB   This problem will be judged on PKU. Original ID: 10886 ...

  5. 【Luogu】P1005矩阵取数游戏(高精度+DP)

    题目链接 yeah终于过辣! DP,f[i][j]表示每行还剩i到j这个区间的数没取的时候的值.借这个题我也把高精度的短板弥补了一下,以后高精加高精乘应该是没问题了. 哇终于不怂高精了…… 放上代码. ...

  6. 最小的图灵完备语言——BrainFuck

    最小的图灵完备语言--BrainFuck 图灵完备性(Turing completness) 在可计算性理论(computability theory)中,图灵等价指的是:对于两个计算机A和B,如果A ...

  7. P1140 相似基因 (动态规划)

    题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了4种核苷酸,简记作A,C,G,T.生物学家正致力于寻找人类基因的功能,以利用于诊断疾病和发明药物. 在一个人类基因工作组的任务中,生物学家研究 ...

  8. cf660E Different Subsets For All Tuples

    For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct sub ...

  9. django搭建开发环境

    1.安装python,安装pip,添加环境变量 2.使用虚拟环境Virtualenv,下载virtualenv解压,进入到此目录,cmd运行python setup.py install(或直接打开c ...

  10. Scrapy学习-1-入门

    基础知识 爬虫发展史   爬虫去重 1. 存储到数据库中 存取速度慢 2. 存储到内存中的集合里,内存占用十分大 当爬取数据有1亿条时 1*10**8*2Byte*50str_len/1024/102 ...