leetcode Implement strStr()(easy) /java

我以为,当时我用c++写这个函数的时候,整个人如同乱麻。

这次用java写。先查的SE 8中String的方法。找到两个与此函数有关的方法:matches()和substring()。

import java.io.*;
import java.util.*;

public class Solution {
        public static int strStr(String haystack, String needle) {
        int r=-1;
        int len1=haystack.length();
        int len2=needle.length();
        if(len2==0)
            return 0;
        int len=len1-len2+1;
        int i,j;
        String s;
        for(i=0;i<len;i++)
        {
            s=haystack.substring(i,i+len2);
            if(s.matches(needle))
            {
                r=i;
                break;
            }
        }
        return r;
    }
    public static void main(String[] args)
    {

        System.out.println(strStr("ababcd","cd"));
        System.out.println(strStr("abcdab","ef"));
    }
}

测试的时候注意特殊情况

“a”与""

"aaa"与"a"

时间: 2024-12-31 04:00:56

leetcode Implement strStr()(easy) /java的相关文章

[LeetCode] 028. Implement strStr() (Easy) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 028. Implement strStr() (Easy) 链接: 题目:https://oj.leetcode.com/problems/implement-strstr/ 代码(github):https://github.com/illuz/leetcode 题意: 在一个字符串里找另一个字符串在其中的位置

LeetCode: Implement strStr() [027]

[题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. [题意] 实现库函数strStr(), 功能是在字符串haystack中找出目标串needle第一次出现的索引位 [思路]字符串的匹配,能够用暴力解法,但不推荐.一般使用KMP算法求解. 简要介绍一下KMP的思想: haystack是

[LeetCode] Implement strStr() [18]

题目 Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 原题链接(点我) 解题思路 字符串匹配这也是个老题了,方法主要有下面4种, 1. 暴利破解法(BF),这个没啥说的,就是一轮一轮的比较,知道遇到相匹配的,这个的时间复杂度为O(n^2). 2. KMP,这应该是字符串匹配领域中最长听说的算

【leetcode】Implement strStr() (easy)

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路: 注意,在for循环中条件有计算得到的负数时, 一定要把计算括起来转换为int, 否则会默认转换为uchar 负数就会被误认为是一个很大的数字. for(int i = 0; i < int(1 - 2); ++i) 实现很常规: int s

28. Implement strStr() [easy] (Python)

题目链接 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. 题目翻译 实现 strStr() 函数.该函数用于判断一个字符串 needle 是否是另一个字符串 haystack 的子串.如果是

leetcode | Implement strStr() | 实现字符串查找函数

Implement strStr() : https://leetcode.com/problems/implement-strstr/ Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 如:haystack = "bcbcda"; needle = "bcd" 则 return 2 解析:字符串查找函数,

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] implement strStr() (C++)

题目: 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 st

leetcode_28_ Implement strStr() (easy)

Imple ment 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 p