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 ...
随机推荐
- antlr v4 使用指南连载5——如何编写词法定义
如何编写词法定义 继上一篇文章,相信大家都明了编写词法规则的两个基本原则.那么接下来就可以开始编写词法文件了.对于计算机科学来说,很多词法规则是一致的.如标识符.数字等,它们都可以重复在 ...
- 最简单的视频网站(JavaEE+FFmpeg)
本文记录一个最简单的视频网站系统.此前做过一些基于JavaEE中的SSH (Strut2 + Spring + Hibernate)的网站系统,但是一直没有做过一个视频网站系统,所以就打算做一个&qu ...
- View绘制流程
1. View 树的绘图流程 当 Activity 接收到焦点的时候,它会被请求绘制布局,该请求由 Android framework 处理.绘制是从根节点开始,对布局树进行 measure 和 dr ...
- android分包方案
当一个app的功能越来越复杂,代码量越来越多,也许有一天便会突然遇到下列现象: 1. 生成的apk在2.3以前的机器无法安装,提示INSTALL_FAILED_DEXOPT 2. 方法数量过多,编译时 ...
- markdown语法(看这张图就够了)
这是维基百科的一张图,基本就够用了 https://en.wikipedia.org/wiki/Markdown#Example
- Android Studio(AS)-->导入项目
1:首先,你必须要有一个工程(Project), 才可以打开项目(Module); (注意:Eclipse中的Workspace对应Android Studio 中的Project, Eclipse中 ...
- JSP标签JSTL(5)--常用的标签函数
在使用JSTL的标签函数的时候请务必加上如下代码 <!-- 添加jsp标签的核心库 --> <%@ taglib uri="http://java.sun.com/jsp/ ...
- C语言中的内存分配
对于一个C语言程序而言,内存空间主要由以下几个部分组成: 1)程序代码区:用来存储程序的二进制代码 2)全局区/静态存储区 3)BSS段:用来存储未初始化的全局变量和静态变量. 4)栈区:存储局部变量 ...
- pig函数以及关键字 的一些实例应用的总结(来自pig笔记)
http://wenku.baidu.com/link?url=yb7KnpSj9nHxWk_MsEVUezvB24evRf9wR87FX0dTT77pGXNXi6k3o_kTmAkBrpIHTqo6 ...
- 纯HTML5APP与原生APP的差距在哪?
笔者写过一些纯H5的APP,虽然开发起来的确很快很舒服,但和原生比起来纯H5APP还是有很多问题,主要聚集在以下几个方面: 1.动画 动画有很多种,比如侧边栏菜单的滑入滑出.元素的响应动画.页面切换之 ...