[leetcode]28. Implement strStr()实现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.
题意:
实现strStr() : Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
Solution1:Two Pointers, finding substring[i...j] in str1,such that it equals str2
code:
/*
Time: O(n^2).
Space: O(1).
*/ class Solution {
public int strStr(String s1, String s2) {
//题意确认 return 0 when needle is an empty string
if(s2.length() == 0) return 0; //for(int i = 0; i < s1.length(); i++){ 确保s1中含有s2,则扫s1的指针的范围可以缩小到s1.length() - s2.length() + 1
for(int i = 0; i < s1.length() - s2.length() + 1; i++){
int j = i;
int k = 0;
while( j < s1.length() && k < s2.length() && s1.charAt(j) == s2.charAt(k)){
j++;
k++;
}
if( k == s2.length()){
return i;
}
}
return -1;
}
}
[leetcode]28. Implement strStr()实现strStr()的更多相关文章
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return 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: ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode 28——Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode 28 Implement strStr() (实现找子串函数)
题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description Problem : 实现找子串的操作:如果没有找到则返回 ...
- [LeetCode] 28. Implement strStr() ☆
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode——28. Implement strStr()
题目: class Solution { public: int strStr(string haystack, string needle) { if(needle.empty()){ return ...
随机推荐
- 统一社会信用代码+组织机构代码 校验 python
转自: https://blog.csdn.net/warrah/article/details/69338912 https://blog.csdn.net/qq_37142340/article/ ...
- Dynamics CRM Plug-in
Plug-in 就是我们俗称的dll file 或者是assembly file. 里面有自定义的代码可以运行在服务器端 Plug-in Pipeline: 只有3个阶段可以做改动: Pre-Vali ...
- Java高级特性 第5节 序列化和、反射机制
一.序列化 1.序列化概述 在实际开发中,经常需要将对象的信息保存到磁盘中便于检索,但通过前面输入输出流的方法逐一对对象的属性信息进行操作,很繁琐并容易出错,而序列化提供了轻松解决这个问题的快捷方法. ...
- 第2章 Java基本语法(下): 流程控制--项目(记账本)
2-5 程序流程控制 2-5-1 顺序结构 2-5-2 分支语句1:if-else结构 案例 class IfTest1{ public static void main(String[] args) ...
- nginx+keeplived+tomcat
1,宣告操作系统版本,nginx,java,tomcat,keeplived版本 操作系统 用途 VIP IP地址 软件版本 CentOS 7.3 mini NTP服务器 无 192.168.197. ...
- CSS之padding&margin
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Nginx、HAProxy、LVS三者的优缺点
一.Nginx优点: 1.工作在网络7层之上,可针对http应用做一些分流的策略,如针对域名.目录结构,它的正规规则比HAProxy更为强大和灵活,所以,目前为止广泛流行. 2.Nginx对网络稳定性 ...
- SQL Server 2012无法连接到WMI提供程序
这篇文章主要介绍了SQL Server 2012无法连接到WMI提供程序(Cannot connect to WMI provider)解决方案,需要的朋友可以参考下 今天一位同事在启动自己工作机的S ...
- numpy学习笔记(三)
(1)numpy的位操作 序号 操作及描述 1. bitwise_and 对数组元素执行位与操作 2. bitwise_or 对数组元素执行位或操作 3. ...
- jenkins-1
1 下载jenkins,https://jenkins.io/download/, 我在此处用的是war的的形式启动的,配置tomact的server.xml,如果是一个主机多个tomact的话还要编 ...