【LeetCode】28. 實現strStr()
題目連結:https://leetcode-cn.com/problems/implement-strstr/description/
題目描述
實現 strStr() 函式。
給定一個 haystack 字串和一個 needle 字串,在 haystack 字串中找出 needle 字串出現的第一個位置 (從0開始)。如果不存在,則返回 -1。
示例
輸入: haystack = “hello”, needle = “ll”
輸出: 2
輸入: haystack = “aaaaa”, needle = “bba”
輸出: -1
說明:
當 needle 是空字串時,我們應當返回什麼值呢?這是一個在面試中很好的問題。
對於本題而言,當 needle 是空字串時我們應當返回 0 。這與C語言的 strstr() 以及 Java的 indexOf() 定義相符。
解決方法
題目較簡單
class Solution {
public:
int strStr(string haystack, string needle) {
return haystack.find(needle);
}
};
相關推薦
【LeetCode】28. 實現strStr()
題目連結:https://leetcode-cn.com/problems/implement-strstr/description/ 題目描述 實現 strStr() 函式。 給定一個 haystack 字串和一個 needle 字串,在 haystack 字串中找出 ne
【LeetCode】28 實現strSTR()
實現 strStr() 函式。 給定一個 haystack 字串和一個 needle 字串,在 haystack 字串中找出 needle 字串出現的第一個位置 (從0開始)。如果不存在,則返回 -1。 示例 1: 輸入: haystack = “hello”, needle = “
【LeetCode】28. Implement strStr() - Java實現
文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Implement strStr(). Return the index of the first occurrence of needle
【leetcode】28.(Easy)Implement strStr()
題目連結 解題思路: 這道題的意思就是找到字串中一個子串的起始位置,沒有這個子串的話返回-1。 我的做法就是簡單的蠻力法,首先找到第一個匹配的字元,然後再進一步進行判決 提交程式碼: class Solution { public int strStr(Strin
【Leetcode_總結】28. 實現strStr() - python
連結:https://leetcode-cn.com/problems/implement-strstr/description/ Q: 實現 strStr() 函式。 給定一個 haystack 字串和一個 needle 字串,在 haystack 字串中
【leetcode】Python實現-122.買賣股票的最佳時機II
122.買賣股票的最佳時機II 描述 給定一個數組,它的第 i 個元素是一支給定股票第 i 天的價格。 設計一個演算法來計算你所能獲取的最大利潤。你可以儘可能地完成更多的交易(多次買賣一支股票)。 注意:你不能同時參與多筆交易(你必須在再次
【leetcode】Python實現-121.買賣股票的最佳時機
121.買賣股票的最佳時機 描述 給定一個數組,它的第 i 個元素是一支給定股票第 i 天的價格。 如果你最多隻允許完成一筆交易(即買入和賣出一支股票),設計一個演算法來計算你所能獲取的最大利潤。 注意你不能在買入股票前賣出股票。 示例
【leetcode】Python實現-67.二進位制求和
67.二進位制求和 描述 給定兩個二進位制字串,返回他們的和(用二進位制表示)。 輸入為非空字串且只包含數字 1 和 0。 示例1 輸入: a = “11”, b = “1” 輸出: “100” 示例2 輸入: a
【leetcode】Python實現-136.只出現一次的數字
136.只出現一次的數字 描述 給定一個非空整數陣列,除了某個元素只出現一次以外,其餘每個元素均出現兩次。找出那個只出現了一次的元素。 說明: 你的演算法應該具有線性時間複雜度。 你可以不使用額外空間來實現嗎? 示例 1:
【Leetcode】Python實現反轉整數
給定一個 32 位有符號整數,將整數中的數字進行反轉。 示例 1: 輸入: 123 輸出: 321 示例 2: 輸入: -123 輸出: -321 示例 3: 輸入: 120 輸
【Leetcode】Python實現正則表示式匹配
給定一個字串 (s) 和一個字元模式 (p)。實現支援 ‘.’ 和 ‘*’ 的正則表示式匹配。 ‘.’ 匹配任意單個字元。 ‘*’ 匹配零個或多個前面的元素。 匹配應該覆蓋整個字串 (s) ,而不
【Leetcode】Python實現Z字形變換
Z字形變換 題意解釋: 比如有一個字串 “0123456789ABCDEF”,轉為zigzag 當 n = 2 時: 0 2 4 6 8 A C E 1 3 5 7 9 B D F 當 n = 4 時: 0 6 C 1
【Leetcode】Python實現最長迴文子串
動態規劃實現 根據迴文的特性,一個大回文按比例縮小後的字串也必定是迴文,比如ABCCBA,那BCCB肯定也是迴文。所以我們可以根據動態規劃的兩個特點: (1)把大問題拆解為小問題 (2)重複利用之
【leetcode】Python實現-190.顛倒二進位制位
190.顛倒二進位制位 描述 顛倒給定的 32 位無符號整數的二進位制位。 示例 輸入: 43261596 輸出: 964176192 解釋: 43261596 的二進位制表示形式為 0000001010010100000111
【leetcode】Python實現-141.環形連結串列
141.環形連結串列 描述 給定一個連結串列,判斷連結串列中是否有環。 進階: 你能否不使用額外空間解決此題? 我…遍歷了以後超出時間限制,於是看大家總結的方法。一個就是設定兩個指標slow和fast,一個步長為1,一個步長為2進行遍歷
【leetcode】Python實現-225.用佇列實現棧
225.用佇列實現棧 描述 使用佇列實現棧的下列操作: push(x) – 元素 x 入棧 pop() – 移除棧頂元素 top() – 獲取棧頂元素 empty() – 返回棧是否為空 注意 你只能使用佇列的基本操
【leetcode】Python實現-203.刪除連結串列中的節點
203.刪除連結串列中的節點 描述 刪除連結串列中等於給定值 val 的所有節點。 示例 輸入: 1->2->6->3->4->5->6, val = 6 輸出: 1->2->3->
【leetcode】Python實現-13.羅馬數字轉整數
13.羅馬數字轉整數 給定一個羅馬數字,將其轉換成整數。 返回的結果要求在 1 到 3999 的範圍內。 我 d = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
【leetcode】Python實現-83.刪除排序連結串列中的重複元素
83.刪除排序連結串列中的重複元素 描述 給定一個排序連結串列,刪除所有重複的元素,使得每個元素只出現一次。 示例1 輸入: 1->1->2 輸出: 1->2 示例2 輸入: 1->1->2-
【Leetcode】Python實現兩個排序陣列的中位數
當兩個數組合並後的總元素長度是奇數時,中位數的下標是n/2。 當兩個數組合並後的總元素個數是偶數時,中位數是下標n/2-1和下標n/2兩個元素的平均值。 所以不論總長度的奇偶性,可以將n/2作為右中