LeetCode #28 Find the Index of the First Occurrence in a String 在字串中找最早出現的位置/索引

LeetCode題目翻譯

英文原文如下

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

中文翻譯

給予兩個字串 needle 跟 haystack,回傳 needle 在 haystack 中最早出現的位置 (索引)

needle 非 haystack 的一部分則回傳 -1

範例及題目限制

LeetCode #28 範例及題目限制

解題思路

這一題其實蠻好玩的,從命名就能看出來一些巧思

needle 的中文是針,haystack 是乾草推

【a needle in a haystack】是英文的一個片語

而在乾草堆裡找針是相當困難的,類似中文中的「大海撈針」

例 : Finding a girlfriend is like looking for a needle in a haystack

咳咳,跑題了

還是來看看題目吧

題目感覺非常單純,我們直接用 for loop來試試吧

C# 解決方案

方案1

public class Solution {
    public int StrStr(string haystack, string needle) {
        for(int i =0;i<haystack.Length-needle.Length+1;i++)
        {
            if(haystack.Substring(i,needle.Length) == needle)
            {
                return i;
             }
        }
        return -1;
    }
}

用一個迴圈寫出來了,可是找不到索引回傳 -1 是不是感覺很熟悉啊

沒錯,內建的 IndexOf 好像做了完全一樣的事

讓我們把程式改寫看看

方案2 – IndexOf

public class Solution {
    public int StrStr(string haystack, string needle) {
        return  haystack.IndexOf(needle); 
    }
}

Java解決方案

方案1

class Solution {
    public int strStr(String haystack, String needle) {
        for(int i =0;i<haystack.length()-needle.length()+1;i++)
        {
            if(haystack.substring(i,i+needle.length()).equals(needle))
            {
                return i;
             }
        }
        return -1;
    }
}

方案2 – indexOf

class Solution {
    public int strStr(String haystack, String needle) {
        return  haystack.indexOf(needle);
    }
}

Python3 解決方案

方案1

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        hl = len(haystack)
        nl = len(needle)
        
        for i in range(0,hl-nl+1):
            if haystack[i:i+nl] == needle:
                return i
        return -1;

方案2 – 不是indexOf,是find

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        return haystack.find(needle)
Python 內建函式- find

JavaScript 解決方案

方案1

/**
 * @param {string} haystack
 * @param {string} needle
 * @return {number}
 */
var strStr = function(haystack, needle) {
        for(let i =0;i<haystack.length-needle.length+1;i++)
        {
            if(haystack.substr(i,needle.length) == needle)
            {
                return i;
             }
        }
        return -1;
};

方案2

/**
 * @param {string} haystack
 * @param {string} needle
 * @return {number}
 */
var strStr = function(haystack, needle) {
     return  haystack.indexOf(needle); 
};

結論

這一題想考的其實應該是邏輯而不是用內建的函式哈哈

但在題目中又沒特別說明,故算是有點瑕疵

🧡如果這篇文章有幫上你的一點點忙,那是我的榮幸

🧡可以的話,幫我的FaceBook 粉絲專頁按個讚,我會很感謝的

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Find the Index of the First Occurrence in a String – LeetCode

最新文章

1 則留言

  1. Simpl desirre too say your aarticle is ass surprising.
    The clarity in your post is skmply great aand i can assume you’re aan expert onn his
    subject. Welll wijth your permission let mee to grab your
    RSS feed to keep up too ate witgh forthoming post.

    Thanks a million aand please carry on thee
    ennjoyable work.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *