leetcode(57)- Implement strStr()
题目:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
思路:
- 题意:判断一个字符串是否包含另外一个
- 写一个函数判断一个字符串从坐标k,是否相等于另一个,遍历一下,i < a.length() -b.length()+1,b。length() < a.length()
-
代码:
public class Solution {
public int strStr(String haystack, String needle) {
if(haystack == null||needle == null||needle.length() > haystack.length()){
return -1;
}
for(int a = 0;a < (haystack.length() - needle.length()+1);a++){
if(equal(haystack,a,needle)){
return a;
}
}
return -1;
}
public boolean equal(String a,int k,String b){
for(int m = 0;m < b.length();m++){
if(b.charAt(m) != a.charAt(k)){
return false;
}
k++;
}
return true;
}
}
leetcode(57)- Implement strStr()的更多相关文章
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return 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() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
随机推荐
- iOS开发之字数不一的多标签Demo
有朋友让帮他写一个封装的字数不一的多标签视图,所以今天将代码展示一下,供大家学习 代码中封装了两种方法,分别是:1.传递数组,数组中是NSString类型的方法:2.传递数组,数组中是NSDictio ...
- 自己动手实现一个Android Studio插件
在使用Android Studio开发的时候,大部分人都会使用一些插件来提高开发效率,例如我们所熟知的butternife,selector,,GsonFormat等,这些分别从不同的原理来帮助我们提 ...
- XML Schema
XML Schema 是基于 XML 的 DTD 替代者. XML Schema 描述 XML 文档的结构. XML Schema 语言也称作 XMLSchema 定义(XML Schema Defi ...
- iOS设计指南
备忘:iOS设计指南:http://www.ui.cn/detail/32167.html
- 关于GCJ02和WGS84坐标系的一点实验
大家都知道,在兲朝的电子地图的坐标都是经过了一个坐标偏移,叫GCJ_02的东西.在网上发现了将WGS84经纬度转成GCJ02的一个代码,写了个小程序测试了下看看全国各地的偏移量有多大. 关于WGS84 ...
- Swift基础之集成单选按钮横竖两种样式
最近马上放假所以比较忙,今天简单写一个项目中出现的单选按钮的横竖样式,PS:封装的是Swift语言样式 首先创建一个UIView的类,然后创建方法,最后调用类中的方法进行显示 //参数一:需要显示的内 ...
- OpenCV特征点检测匹配图像-----添加包围盒
最终效果: 其实这个小功能非常有用,甚至加上只有给人感觉好像人脸检测,目标检测直接成了demo了,主要代码如下: // localize the object std::vector<Point ...
- C++_友元函数
1.为什么要引入友元函数:在实现类之间数据共享时,减少系统开销,提高效率 具体来说:为了使其他类的成员函数直接访问该类的私有变量 即:允许外面的类或函数去访问类的私有变量和保护 ...
- (NO.00005)iOS实现炸弹人游戏(六):游戏数据的初始化(三)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 现在我们来看看实际初始化地图的randomCreateMap方法 ...
- [C++学习历程]Visual Studio 2010 的HelloWorld
大学时期曾学过C++的知识,那时候也没有使用VS这样高档的IDE工具,就是C++6.0那样来的.对于重新拾起C++来说,换了个IDE,那么就先从使用IDE学起吧~ 作者:苏生米沿 本文链接:http: ...