28. 实现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() 定义相符。
class Solution {
public int strStr(String haystack, String needle) {
int needleLen = needle.length();
if (needleLen == 0) {
return 0;
}
for (int i = 0; i < haystack.length(); i++) {
if ((i + needleLen) <= haystack.length() && haystack.substring(i, i + needleLen).equals(needle)) {
return i;
}
}
return -1;
}
}
28. 实现strStr()的更多相关文章
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- 28.实现 strStr() 函数
28.实现 strStr() 函数 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在, ...
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...
- <每日 1 OJ> -LeetCode 28. 实现 strStr()
题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...
- 前端与算法 leetcode 28.实现 strStr()
# 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- Java实现 LeetCode 28 实现strStr()
28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...
- 【LeetCode】28. 实现 strStr()
28. 实现 strStr() 知识点:字符串:KMP算法 题目描述 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 ne ...
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
随机推荐
- MySql:SELECT 语句(六) CONCAT() 函数的使用
一.计算字段 为什么要用计算字段? 1)想要在一个字段中既显示公司地址,又显示公司名称,但是往往这两个都不在一个字段中 2)列数据是大小写混合的,但是报表程序需要把他们全部按大写形式展示出来 3)需要 ...
- chrome调试vue.js的插件:vue.js devtools
1.什么是vue.js devtools? vue.js devtools是chrome的一款插件,用来调试vue的一款神器. 2.为什么下载vue.js devtools? 由于vue是数据驱动的, ...
- 移动端目标识别(3)——使用TensorFlow Lite将tensorflow模型部署到移动端(ssd)之Running on mobile with TensorFlow Lite (写的很乱,回头更新一个简洁的版本)
承接移动端目标识别(2) 使用TensorFlow Lite在移动设备上运行 在本节中,我们将向您展示如何使用TensorFlow Lite获得更小的模型,并允许您利用针对移动设备优化 ...
- vs中添加库文件WinMM.Lib
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\WinMM.Lib;
- form表单的三个属性 action 、mothod 、 enctype。
form_action: 表单数据提交到此页面 下面的表单拥有两个输入字段以及一个提交按钮,当提交表单时,表单数据会提交到名为 "form_action.asp" 的页面: < ...
- Codeforces Round #436 E. Fire(背包dp+输出路径)
题意:失火了,有n个物品,每个物品有价值pi,必须在时间di前(小于di)被救,否则就要被烧毁.救某个物 品需要时间ti,问最多救回多少价值的物品,并输出救物品的顺序. Examples Input ...
- LINUX | 谷歌云开启SSH及设置root密码
一.设置root密码 1.先选择从浏览器打开ssh连接服务器 2.切换到root账号 sudo -i 3.设置root密码 passwd 然后会要求输入新密码,然后再重复一次密码,输入密码的时候不会显 ...
- winform UI 设计方法
这里主要提供了一种设计思路,你可以将成套的UI图像添加或替换到本程序中.在设计过程中,还存在许多疏漏,懒得仔细修改了,在使用过程中自行修改添加.这里的函数基本上都可以单独使用,需要哪个将cs文件加入到 ...
- mongodb突然出现不是内外部命令或可执行
可能因为晚上突然停电,电脑突然关机,给mongoDB造成影响,第二天出现了: 通过百度搜索与尝试:https://blog.csdn.net/hh12211221/article/details/78 ...
- git pre-push hook
This article introduces git pre-push hook. Problem In Lean project, we use a modified version of Goo ...