[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 ...
随机推荐
- DocView mode 2 -- 快捷键
** 启动 C-c C-c 切换DocView和文件内容显示 M-x doc-view-mode 启动主模式 M-x doc-view-minor-mode 启动辅模式 k k ...
- [WPF自定义控件库]使用WindowChrome的问题
1. 前言 上一篇文章介绍了使用WindowChrome自定义Window,实际使用下来总有各种各样的问题,这些问题大部分都不影响使用,可能正是因为不影响使用所以一直没得到修复(也有可能别人根本不觉得 ...
- pycharm下多个工程项目并存显示
问题:使用pycharm新建一个工程时,出现如下提示: 无论选择哪一个,都会发现之前已经建立的工程没有并存显示 解决办法: 1. 找到file->settings: 2.点击project st ...
- wordpress无法登录的解决方法
使用wordpress建站的朋友可能会遇到wordpress管理密码,有时甚至是正确的密码,但是多次尝试输入都无法登录,并且输入用户名和输入电子邮件都无法获取密码,遇到这种情况怎么办,本文教你如何处理 ...
- x86 idt
Interrupt/trap gate
- 机房合作(三):We are Team,We are Family
导读:拖拖拉拉,机房的合作也算是接近了尾声了.在这个过程中,真心是感谢我的两个组员.这个机房合作,看似简单,但我的组员给我的帮助和感动,都是不可忽略的.记得刚开始的时候,我就说过:不怕猪一样的组长,咱 ...
- MySQL主从复制报错一致性问题解决
当MySQL主从复制中因为不一致报错的情况,我们第一时间想到的就是使用pt-table-checksum来进行检查主从一致并进行修复,但是使用此工具的前提是主从复制线程都为on状态, 所以这种情况下可 ...
- mysqlbinlog备份和mysqldump备份
-bash : mysqldump: command not found -bash : mysqlbinlog:command not found 首先得知道mysql命令或mysqldump命令的 ...
- 30分钟学会如何使用Shiro(转自:http://www.cnblogs.com/learnhow/p/5694876.html)
本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分 ...
- Python 频繁读取Mysql相关问题
1.需要频繁select大量数据,时间长.消耗内存大,如何解决mysql性能问题? 如果对返回的结果数量没有要求,可以控制返回的数量: cursor.fetchmany(size=1000) 这样是只 ...