刷题之Implement strStr()

 1 public class Solution {
 2     public int strStr(String haystack, String needle) {
 3         int big = haystack.length();
 4         int sub = needle.length();
 5         if(big==0 && sub==0) return 0;
 6         if(big==0 && sub!=0) return -1;
 7         int index = big-sub;
 8         for(int i=0;i<=index;i++){
 9             if(haystack.substring(i,sub+i).equals(needle))return i;
10         }
11         return -1;
12     }
13 }

是int index = haystack.indexOf(needle)的另一种做法

时间: 2024-10-12 12:30:59

刷题之Implement strStr()的相关文章

leetcode第27题--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,如果包含的话,返回第一次包含的地址.如果没有则返回NULL. 这题官网打出来的是easy,但是要做好绝不简单.我能想到的就是O(m*n)的复杂度了.最经典的是用KMP算法. class Soluti

leetcode_28题——Implement strStr()(采用KMP算法,还没AC,但自己这边测试无误)

Implement strStr() Total Accepted: 49294 Total Submissions: 223057My Submissions Question Solution Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02):The s

LeetCode 刷题总结

最近找工作,免不了看CC150 刷 LeetCode 来练手,练习之余也向各路大神 系统并且深入的学习.巩固一下算法知识.一. 线性表1. 数组    Remove Duplicates from Sorted Array    Remove Duplicates from Sorted Array II    Search in Rotated Sorted Array    Search in Rotated Sorted Array II    Median of Two Sorted A

LeetCode 28. Implement strStr()

https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 字符串简单题.一定要写的没BUG. 如果不在函数最后一行写return语句的话,LeetCode会出RUNTIME ERROR. Line 27: co

Implement strStr()

package cn.edu.xidian.sselab.string; /** *  * @author zhiyong wang * title: Implement strStr() * content: *  Implement strStr(). *   *  Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.  * */pub

LeetCode刷题之一:寻找只出现一次的数字

投简历的时候看到了个刷题网站,http://www.nowcoder.com/527604,就做了一套题,现记录下来. 题目为: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it withou

LeetCode Implement strStr()

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If you still

【LeetCode】28. Implement strStr() 解题小结

题目: 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 (needle.empty()) return 0; if (haystack.em

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. 二.题目分析 实现strstr()函数.返回needle(关键字)在haystack(字符串)中第一次出现的位置,如果needle不在haystack中,则返回-1.由于使用暴力方法的时间复杂度为O(mn)会超时,可使用著名的KM