LeetCode_28. Implement strStr()
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().
package leetcode.easy; public class ImplementStrStr {
@org.junit.Test
public void test() {
String haystack1 = "hello";
String needle1 = "ll";
String haystack2 = "aaaaa";
String needle2 = "bba";
System.out.println(strStr(haystack1, needle1));
System.out.println(strStr(haystack2, needle2));
} public int strStr(String haystack, String needle) {
char[] source = haystack.toCharArray();
int sourceOffset = 0;
int sourceCount = haystack.length();
char[] target = needle.toCharArray();
int targetOffset = 0;
int targetCount = needle.length();
int fromIndex = 0;
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
} char first = target[targetOffset];
int max = sourceOffset + (sourceCount - targetCount); for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */
if (source[i] != first) {
while (++i <= max && source[i] != first)
;
} /* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j] == target[k]; j++, k++)
; if (j == end) {
/* Found whole string. */
return i - sourceOffset;
}
}
}
return -1;
}
}
LeetCode_28. Implement strStr()的更多相关文章
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns 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 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [leetcode 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- 【leetcode】Implement strStr() (easy)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [LeetCode] Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Implement strStr() [LeetCode]
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
随机推荐
- 小技巧:with用法 pycharm控制台输出带颜色的文字 打印进度条的
with用法 with用法在python中是一个很独特的用法,因为别的语言的中没有这个用法.所以针对这个特点我们来做一次总结,什么样的情况下可以同with 我们学到的有文件的操作,和acquire ...
- javascript常用工具类util.js
//如果大家想要补充,请留言 /** * 判断指定名称的复选框是否被选中 * * @param {} * chname复选框名称 */ function chkCheckCha(chname) { v ...
- 一份令人愉快的vs代码包和资源的整理清单
https://viatsko.github.io/awesome-vscode/ https://github.com/viatsko/awesome-vscode
- mysql官方下载安装教程(centos)
Linux下Mysql 5.6.30 tar包安装 (2016-04-27 22:45:39) 转载▼ 环境:centos 6.4 x64 先下载mysql安装包 打开 http://dev.mysq ...
- element-ui 限制只能输入number
element-ui <el-form-item label="大于等于:"> <el-input @keyup.native="number" ...
- JS 仿支付宝input文本输入框放大组件
input输入的时候可以在后边显示数字放大镜 <!doctype html> <html lang="en"> <head> <meta ...
- Java中String、StringBuilder和StringBuffer
StringBuilder和StringBuffer内部都是通过char[]来实现的.(jdk1.9后,底层把char 数组变成了byte[].)唯一不同的就是我们的StringBuffer内部操作方 ...
- Python多线程笔记(三),queue模块
尽管在Python中可以使用各种锁和同步原语的组合编写非常传统的多线程程序,但有一种首推的编程方式要优于其他所有编程方式即将多线程程序组织为多个独立人物的集合,这些任务之间通过消息队列进行通信 que ...
- c 输出是自动显示输出类型
显示0x i= print("%#x\n",i) 显示6位有效数字 i= print("l=%.6lf\n",i)
- Java基础系列 - 泛型和反射机制
package com.test5; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * Java泛型和反射机 ...