28. Implement strStr()【easy】
28. Implement strStr()【easy】
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
解法一:
class Solution {
public:
int strStr(string haystack, string needle) {
if (haystack.empty() && needle.empty()) {
return ;
}
if (haystack.empty()) {
return -;
}
if (haystack.size() < needle.size()) {
return -;
}
for (string::size_type i = ; i < haystack.size() - needle.size() + ; i++) {
string::size_type j = ;
for (j = ; j < needle.size(); j++) {
if (haystack[i + j] != needle[j]) {
break;
}
}
if (j == needle.size()) {
return i;
}
}
return -;
}
};
28. Implement strStr()【easy】的更多相关文章
- 28.Implement strStr()【leetcod】
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 495. Implement Stack【easy】
Implement a stack. You can use any data structure inside a stack except stack itself to implement it ...
- c语言 Implement strStr()【Leetcode】
实现在一个母字符串中找到第一个子字符串的位置. #include <stdio.h> #include <string.h> #define _IRON_TRUE 1 #def ...
- 28. Search a 2D Matrix 【easy】
28. Search a 2D Matrix [easy] Write an efficient algorithm that searches for a value in an mx n matr ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 141. Sqrt(x) 【easy】
141. Sqrt(x) [easy] Implement int sqrt(int x). Compute and return the square root of x. Example sqrt ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
随机推荐
- [UOJ206]Gap
子任务$1$:直接找到最大值后从两边开始找,一步一步从左右到中间确定所有数,调用次数是$\left\lceil\dfrac n2\right\rceil$ 子任务$2$:先找到最大值$mx$和最小值$ ...
- Java高级架构师(一)第26节:测试并调整登录的业务功能
主Index的处理Java: package com.sishuok.architecture1; import org.springframework.beans.factory.annotatio ...
- 再谈EditText只能输入金额
上次写了一篇EditText只能输入金额的博客,后来发现一个bug,当还未输入数字的情况下输入小数点程序就崩了,我去测了一下支付宝,看看会怎么样,我先输入小数点,程序正常,我再输入数字,可以正常输入, ...
- 【java】字符串的反转
@org.junit.Test public void test(){ String a = "I IOVE CHINA"; if(a.indexOf(" ") ...
- 腾讯云会话服务器node+nginx
1.除了一个正常的服务器还需要一个会话服务器(websocket),利用node加socket.io来做 2.正常安装Nginx yum install nginx 3.Nginx的配置内容略微不同( ...
- http://zhidao.baidu.com/link?url=inouJq69pK4PVM2L26fvnxXfRKhJ1uKmttgVqIEqld14SEUa8JzXZfRYHS3qdltqMXBgEQycFsF8AI9DlSoH4_
http://zhidao.baidu.com/link?url=inouJq69pK4PVM2L26fvnxXfRKhJ1uKmttgVqIEqld14SEUa8JzXZfRYHS3qdltqMXB ...
- dubbo发布webservice服务
dubbo发布webservice服务 学习了:https://blog.csdn.net/zhangyunpengchang/article/details/51567127 https://blo ...
- android 开发者的个人博客集
1. http://stormzhang.com/posts.html //不少的好的工具与建议
- 倍福TwinCAT(贝福Beckhoff)应用教程12.2 TwinCAT控制松下伺服 NC初步
在前面我们已经学会了使用贝福自带的调试软件完成试运行,接下来是使用TWINCAT PLC实现这个功能,右击PLC添加一个PLC项目 在VISUs上右击添加一个HMI人机界面 目前PLC程序和人 ...
- 程序员之---C语言细节24(段错误、类型提升、sizeof 'A')
主要内容:段错误.类型提升.sizeof 'A' #include <stdio.h> int main() { union test{ char a[10]; int b; }u; i ...