[LC] 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().
- class Solution(object):
- def strStr(self, haystack, needle):
- """
- :type haystack: str
- :type needle: str
- :rtype: int
- """
- if not needle:
- return 0
- if not haystack:
- return -1
- len_haystack, len_needle = len(haystack), len(needle)
- for i in range(0, len_haystack - len_needle + 1):
- cur = haystack[i]
- if cur == needle[0]:
- j = 0
- while j < len_needle:
- if haystack[i + j] != needle[j]:
- break
- j += 1
- if j == len_needle:
- return i
- return -1
[LC] 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 ...
随机推荐
- TCP/IP与IETF的RFC
究竟是谁控制着 TCP/IP协议族,又是谁在定义新的标准以及其他类似的事情?事实上, 有四个小组在负责Internet技术. 1) Internet协会(ISOC,Internet Society)是 ...
- 计蒜客 密码锁(BFS)
https://www.jisuanke.com/course/1797/121114 Description 现在一个紧急的任务是打开一个密码锁.密码由四位数字组成,每个数字从 1 到 9 进行编号 ...
- Android开发环境搭建以及模拟环境搭建
Android开发环境 现在主流的Android开发环境有: Eclipse + ADT + SDK Android Studio + SDK IntelliJ IDEA + SDK 现在国内大部分开 ...
- MySQL-SQL语句分类
MySQL中的SQL语句有:DDL,DML,DCL,DQL,TCL DDL:数据库定义语言 data Definition language 用于创建.修改.和删除数据库内的数据结构,如: 1:创建和 ...
- C语言 指针在函数传参中的使用
int add(int a, int b) //函数传参的时候使用了int整型数据,本身是数值类型.实际调用该函数时,实参将自己拷贝一份,并将拷贝传递给形参进行运算.实参自己实际是不参与运算的.所 ...
- Dinic学习笔记
网络流是啥不用我说了吧 增广路定理不用我说了吧 Dinic就是分层然后只在层间转移,然后就特别快,\[O(N^2M)\] 伪代码: function dinic int flow = 0 ; whil ...
- Python笔记_第一篇_面向过程_第一部分_6.语句的嵌套
学完条件控制语句和循环控制语句后,在这里就会把“语言”的精妙之处进行讲解,也就是语句的嵌套.我们在看别人代码的时候总会对一些算法拍案叫绝,里面包含精妙和精密的逻辑分析.语句的嵌套也就是在循环体内可以嵌 ...
- 代码验证ncut和谱聚类的系数
W = rand(30); W = W+W'; I = cell(3,1); I{1} = 1:10; I{2} = 11:20; I{3} = 21:30; vol = -ones(3,1); fo ...
- winEdt 使用
晚上摘抄的方法: 1.点选Options -> Options Interface 2.右边会跳出一个介面,点选Advance Configuration... -> Event Hand ...
- 吴裕雄--天生自然 PYTHON3开发学习:日期和时间
import time; # 引入time模块 ticks = time.time() print ("当前时间戳为:", ticks) import time localtime ...