[LeedCode OJ]#28 Implement strStr()
【 声明:版权全部,转载请标明出处,请勿用于商业用途。 联系信箱: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()的更多相关文章
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- 28. Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- 【LeetCode】28 - Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- 00051_static关键字
1.static概念 当在定义类的时候,类中都会有相应的属性和方法.而属性和方法都是通过创建本类对象调用的.当在调用对象的某个方法时,这个方法没有访问到对象的特有数据时,方法创建这个对象有些多余.可是 ...
- pycharm中脚本执行的3种模式(unittest框架、pytest框架、普通模式)
背景知识,某次使用HTMLTestRunner的时候,发现一直都无法导出报告,后来查询资料发现了一些坑,现在整理一下来龙去脉. 一:pycharm默认的是pytest框架去执行unittest框架的测 ...
- springboot的使用体验和思考
首先,写这篇博客的背景: 1,通过maven使用springboot创建项目,进行了简单的页面跳转,并未编写service和DAL层,也就是说,并未整合持久化框架 2,阅读了maven的官方文档.sp ...
- mysqlbinlog备份和mysqldump备份
-bash : mysqldump: command not found -bash : mysqlbinlog:command not found 首先得知道mysql命令或mysqldump命令的 ...
- HDU 1423 Greatest Common Increasing Subsequence ——动态规划
好久以前的坑了. 最长公共上升子序列. 没什么好说的,自己太菜了 #include <map> #include <cmath> #include <queue> ...
- 为什么上传文件的表单里要加个属性enctype----摘录
上传文件的表单中<form>要加属性enctype="multipart/form-data",很多人只是死记硬背知道上传表单要这么写,知其然而不知其所以然.那到底为什 ...
- leetcode 94 中序遍历模板
/**递归的写法 * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * Tre ...
- LinkedList的构造函数有哪些
LinkedList构造函数有(两种): public LinkedList() public LinkedList(Collection<? extends E> c) /** * Co ...
- C 语言中的 fgets()
转自:http://blog.csdn.net/daiyutage/article/details/8540932 原型: char * fgets(char * s, int n,FILE *st ...
- Codeforces 271D - Good Substrings [字典树]
传送门 D. Good Substrings time limit per test 2 seconds memory limit per test 512 megabytes input stand ...