Java [leetcode 28]Implement strStr()
题目描述:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Update (2014-11-02):
The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char *
or String
, please click the reload button to reset your code definition.
解题思路:
从开头开始,逐个匹配。
注意特殊情况。
代码如下:
public class Solution {
public static int strStr(String haystack, String needle) {
int haystackLength = haystack.length();
int needleLength = needle.length();
int j = 1;
if (needleLength == 0)
return 0;
if (needleLength > haystackLength)
return -1;
for (int i = 0; i <= haystackLength - needleLength; i++) {
if (haystack.charAt(i) == needle.charAt(0)) {
for (j = 1; j < needleLength; j++) {
if (haystack.charAt(i + j) != needle.charAt(j))
break;
}
if (j == needleLength)
return i;
}
}
return -1;
}
}
Java [leetcode 28]Implement 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()
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: ...
- [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()实现strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [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() (实现找子串函数)
题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description Problem : 实现找子串的操作:如果没有找到则返回 ...
- LeetCode——28. Implement strStr()
题目: class Solution { public: int strStr(string haystack, string needle) { if(needle.empty()){ return ...
随机推荐
- linux C语言getopt()函数的使用
getopt被用来解析命令行选项参数. #include <unistd.h> 函数及参数介绍 extern char *optarg; //选项的参数指针,如果选项字符串里的字母后接着冒 ...
- Battle Over Cities (25)(DFS、连通图)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- C语言-创建链表及排序
#include <stdio.h> #define NEWNODE (Node *)malloc(sizeof(Node)) typedef struct mynode{ int num ...
- Qt 内存管理机制(转)
许转载http://devbean.blog.51cto.com/448512/526734 强类型语言在创建对象时总会显式或隐式地包含对象的类型信息.也就是说,强类型语言在分配对象内存空间时,总 ...
- strcmp的源码实现
微软方法: int __cdecl strcmp (const char *src, const char *dst) { ; while(!(ret = *(unsigned char *)src ...
- GUIText的淡入淡出
单击按键“A”(随意改变),可以控制GUIText马上显示出来,然后淡出:按住按键“A”,可以使GUIText淡入,如果抬起按键则淡出. FadeInOut.cs using UnityEngine; ...
- 为什么要用Hibernate框架? 把SessionFactory,Session,Transcational封装成包含crud的工具类并且处理了事务,那不是用不着spring了?
既然用Hibernate框架访问管理持久层,那为何又提到用Spring来管理以及整合Hibernate呢?把SessionFactory,Session,Transcational封装成包含crud的 ...
- Angular与React的一些看法
Angular - React之争 Angular和React无疑是目前最受追捧的两个前端框架,谷歌也发现Angular1.x不足的地方,开始开发2.0版本,脸书发现React的组件化和虚拟DOM非常 ...
- 快速配置Ehcache
1. 编写ehcache.xml文件,将该文件放置于classpath路径下.代码如下: <?xml version="1.0" encoding="UTF-8&q ...
- python参考手册--第2章词汇和语法约定
1.续行符\ 三引号.().{}.[]中的内容不需要续行符 2.空格缩进 优选空格作为缩进,不要用tab,这是因为不同操作系统下tab对应的空格不一样,而python是通过严格的空格来控制语句块的. ...