【leetcode刷题笔记】Implement strStr()
2024-09-05 17:22:05
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
暴力法,从haystack第一个字符开始查找needle。
代码如下:
public class Solution {
public String strStr(String haystack, String needle) {
if(haystack == null)
return null; if(needle.length() == 0)
return haystack; int i,j = 0;
for(i = 0;i < haystack.length() - needle.length() + 1;i++){
for(j = 0;j < needle.length();j++){
if(haystack.charAt(i+j) != needle.charAt(j))
break;
}
if(j == needle.length())
return haystack.substring(i,haystack.length());
}
return null;
}
}
最新文章
- windows系统路径环境变量
- CSS盒子模型与box-sizing
- [问题2014S14] 解答
- javaWeb 使用cookie显示上次访问网站时间
- USACO Section 2.2: Runaround Numbers
- js判断checkbox是否已选
- Alex: 2018年对混合现实MR的展望
- 圆形图片 ImageView
- 【干货】利用MVC5+EF6搭建博客系统(三)添加Nlog日志、缓存机制(MemoryCache、RedisCache)、创建控制器父类BaseController
- 国产 WEB UI 框架 (收费)-- Quick UI,Mini UI
- Kali Day01 --- arpspoof命令进行断网攻击(ARP欺骗)
- 【BZOJ4259】残缺的字符串(FFT)
- 原生js的ajax请求
- Android之 看“马达”如何贯通Android系统 (从硬件设计 -->; 驱动 -->; HAL -->; JNI -->; Framework -->; Application)
- Ubuntu端口命令---查看端口占用及关闭
- Centos7下Yum安装PHP5.5,5.6,7.0
- ASP.NET 文件上传于下载
- CCF认证201712-2游戏
- 对CCLE数据库可以做的分析--转载
- leetcode-40-组合总和 II
热门文章
- The return types for the following stored procedures could not be detected
- java之Cookie具体解释
- Windows安装Redis的php扩展
- JQuery小结(转)
- nodeJS 中关于 promise 的使用
- Highways - poj 2485 (Prim 算法)
- jquery简洁遮罩插件
- 12 Memcached 缓存无底洞现象
- 【POJ-2524】Ubiquitous Religions(并查集)
- 读书笔记-HBase in Action-第三部分应用-(1)OpenTSDB