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 ...
随机推荐
- jdbc练习demo
//连接云端服务器数据库工具类 public class TestJDBCUtil { public static String driver="com.mysql.jdbc.Driver& ...
- 前端基础——AJAX
一 简介 AJAX(Asynchronous Javascript And XML),即AJAX = 异步JavaScript + XML.AJAX是一种用于创建快速动态网页的技术. AJAX两大特 ...
- leetcode763
public class Solution { public IList<int> PartitionLabels(string S) { var dic = new Dictionary ...
- 人脸识别-<转>
人脸检测库libfacedetection介绍 libfacedetection是于仕琪老师放到GitHub上的二进制库,没有源码,它的License是MIT,可以商用.目前只提供了windows 3 ...
- iframe 元素会创建包含另外一个文档的内联框架(即行内框架)
HTML 与 XHTML 之间的差异 在 HTML 4.1 Strict DTD 和 XHTML 1.0 Strict DTD 中,不支持 iframe 元素. 提示和注释: 提示:您可以把需要的文本 ...
- springboot 中 集成druid ,redis
1,导入druid jar包 <!--引入drud--> <dependency> <groupId>com.alibaba</groupId> < ...
- 使用navicat的SSH隧道连接数据库
这几天在连接远程数据库的时候,发现用navicat的普通方法居然连接不上,然后就想用ssh连接试试,开始是报错的,错误是这样的: lost connection to Mysql server at ...
- 新手C#int.Parse、int.TryParse的学习2018.08.04
int.Parse()用于将字符串转换为32为int类型,但是在遇到非数字或者类似1.545这种小数的时候会报错,后来采用了int.TryParse,这个在转换后会判断是否可以正常转换,若不能,会返回 ...
- 01d-1: 算法分析
- for 续10
---------siwuxie095 for 帮助信息: ...