leetcode 实现strStr()
实现strStr()函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
示例 1:
输入: haystack = "hello", needle = "ll"
输出: 2
示例 2:
输入: haystack = "aaaaa", needle = "bba"
输出: -1
说明:
当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的indexOf()定义相符。
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function (haystack, needle) {
if (!needle || haystack === needle) {
return 0;
}
let h_len=haystack.length;
let n_len=needle.length;
for (let i = 0; i <= h_len - n_len; i++) {
let j = 0;
for (; j < n_len; j++) {
if (haystack[i + j] !== needle[j]) {
break;
}
}
if (j === n_len) {
return i;
}
}
return -1;
};
indexOf就不用思考了,看了别人的解答又想了好久才想出这种写法
leetcode 实现strStr()的更多相关文章
- Leetcode : eImplement strStr
Leetcode : eImplement strStr 描述 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个 ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [Leetcode] implement strStr() (C++)
Github leetcode 我的解题仓库 https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...
- LeetCode OJ--Implement strStr()
http://oj.leetcode.com/problems/implement-strstr/ 判断一个串是否为另一个串的子串 比较简单的方法,复杂度为O(m*n),另外还可以用KMP时间复杂度为 ...
- LeetCode Implement strStr()(Sunday算法)
LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...
- [LeetCode] Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- leetcode implement strStr python
#kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: ...
- LeetCode: Implement strStr() [027]
[题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...
- LeetCode Implement strStr() 实现strstr()
如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...
随机推荐
- WebAPI认证与授权
Web APi之认证(Authentication)及授权(Authorization)[一](十二) http://www.cnblogs.com/CreateMyself/p/4856133.ht ...
- SpringFox swagger2 and SpringFox swagger2 UI 接口文档生成与查看
依赖: <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency ...
- delphi 实体类 JSON 数组
delphi 实体类 与JSON转换,序列化 TJson REST.JSON.pas TJson.JsonToObjectTJson.ObjectToJsonString JsonEncode O ...
- Remove “Quick Access” entry in Eclipse Juno
Here is a quick hack which doesn't require any plugin installation, instead you just need to add a f ...
- linux之cut
[linux之cut] -b:字节 -c:字符 -d:自定义域 -f:域范围 参考:http://wenku.baidu.com/view/9399bc8383d049649b66588b.html
- pg_hba.conf、pool_hba.conf 以及 pool_passwd 三者间的关系
pg_hba.conf.pool_hba.conf 以及 pool_passwd 三者间的关系: 1.pg_hba.conf.pool_hba.conf 以及 pool_passwd 三者关系 pg_ ...
- freemaker在表格中遍历数据
Controller层如下所示: @RequestMapping(value = "/test", method = RequestMethod.GET) public Strin ...
- 图解Java常用数据结构(一)
最近在整理数据结构方面的知识, 系统化看了下Java中常用数据结构, 突发奇想用动画来绘制数据流转过程. 主要基于jdk8, 可能会有些特性与jdk7之前不相同, 例如LinkedList Linke ...
- 磁盘存储结构与文件恢复实验(FAT文件系统)
实验地点:主楼A2-412 一.实验室名称:主楼实验室A2-412 二.实验项目名称:磁盘存储结构与文件恢复实验 三.实验学时:6学时 四.实验原理: 在Debug环 ...
- 单源最短路:Dijkstra算法 及 关于负权的讨论
描述: 对于图(有向无向都适用),求某一点到其他任一点的最短路径(不能有负权边). 操作: 1. 初始化: 一个节点大小的数组dist[n] 源点的距离初始化为0,与源点直接相连的初始化为其权重,其他 ...