【leetcode刷题笔记】Implement strStr()
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;
}
}
【leetcode刷题笔记】Implement strStr()的更多相关文章
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- LeetCode刷题笔记--Python--28. 实现strStr()
class Solution: def strStr(self, haystack, needle): """ :type haystack: str :type nee ...
- [leetcode刷题笔记]Implement Trie (Prefix Tree)
题目链接 一A,开森- ac代码: class TrieNode { // Initialize your data structure here. char content; boolean isW ...
- LeetCode刷题笔记(1-9)
LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...
- leetcode刷题笔记
(1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...
随机推荐
- DelphiXe 中静态数组TByteArray和动态数组TBytes /array of byte 的区别
在应用中发现静态数组和动态数组是有区别的: procedure TForm1.Button1Click(Sender: TObject);var RsltStream: TMemoryStream; ...
- H5 打开App
IOS 1.只能在safari浏览器打开, 2.必须App安装的情况下. 3.IOS必须配置 URLSchemes 为 "yidiandai://" 才可以. <!DOCT ...
- 网络启动并安装Debian
网络启动(PXEBoot)并安装Debian的官方文档在这里,不过官方文档有点冗长,我这里假设已经有一台安装好Debian,需要网络安装另一台(这台可以是虚拟机,通过ISO文件等等方式安装的).PXE ...
- 大师养成计划之一:搭建springmvc框架
搭建spring-mvc框架 搭建spring-mvc框架步骤: 1.搭建web项目spring-mvc1 2.引入jar包 3.配置web.xml 3.1拷贝头文件: <web-app xml ...
- 403/you don't have the permission to access on this server
Localhost/index.php出现 错误403 you don't have the permission to access on this server 现在已经解决,特将方法与大家分享. ...
- 深入了解Erlang 垃圾回收机制以及其重要性(转)
声明:本片文章是由Hackernews上的[Erlang Garbage Collection Details and Why ItMatters][1]编译而来,本着学习和研究的态度,进行的编译,转 ...
- PHP SOCKET编程 .
1. 预备知识 一直以来很少看到有多少人使用php的socket模块来做一些事情,大概大家都把它定位在脚本语言的范畴内吧,但是其实php的socket模块可以做很多事情,包括做ftplist,http ...
- 精通 Android Data Binding
转自:https://github.com/LyndonChin/MasteringAndroidDataBinding 官方虽然已经给出了教程 - Data Binding Guide (中文版 - ...
- Java -- 数字
@.运用BigDecimal处理Double类型的算术运算的精度问题 原文:https://blog.csdn.net/j754379117/article/details/52238396 可使用 ...
- BlockingQueue的使用 http://www.cnblogs.com/liuling/p/2013-8-20-01.html
BlockingQueue的使用 本例介绍一个特殊的队列:BlockingQueue,如果BlockQueue 是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态,直到Blocki ...