實現strstr()

實現 strStr() 函數。

給定一個 haystack 字符串和一個 needle 字符串,在 haystack 字符串中找出 needle 字符串出現的第一個位置 (從0開始)。如果不存在,則返回 -1。

  • 示例 1:
輸入: haystack = "hello", needle = "ll"
輸出: 2
  • 示例 2:
輸入: haystack = "aaaaa", needle = "bba"
輸出: -1
  • 說明:

當 needle 是空字符串時,我們應當返回什么值呢?這是一個在面試中很好的問題。

對于本題而言,當 needle 是空字符串時我們應當返回 0 。這與C語言的 strstr() 以及 Java的 indexOf() 定義相符。

class Solution {
public:int strStr(string haystack, string needle) {if (haystack.size() < needle.size()) {return -1;}int i, j;for (i=0; i<haystack.size() - needle.size()+1; i++) {for (j=0; j<needle.size(); j++) {if (haystack[i+j] != needle[j]) {break;}}if (j == needle.size()) {return i;}}return -1;}
};
  • Sunday 算法
class Solution {
public:int strStr(string haystack, string needle) {int hsize = haystack.size();int nsize = needle.size();unordered_map<char, int> offset;for(int i=0; i<nsize; i++) {offset[needle[i]] = nsize - i;}int i=0;while(i<=hsize-nsize) {if (haystack.substr(i, nsize) == needle) {return i;} else {if (i+nsize > hsize-1) {return -1;} else {if (offset.find(haystack[i+nsize]) != offset.end()) {i += offset[haystack[i+nsize]];} else {i+= nsize+1;}}}}return -1;}
};
  • KMP 算法
class Solution {
public:int strStr(string haystack, string needle) {if (needle.empty()) {return 0;}int hSize = haystack.size();int nSize = needle.size();vector<int> next(nSize, 0);get_next(next, needle);int i=0;int j=0;while(i<hSize) {if (haystack[i] == needle[j]) {i++;j++;} else {if (j == 0) {i++;} else {j=next[j-1];}}if (j==nSize) {return i-j;}}return -1;}void get_next(vector<int> &next, const string &needle) {int n = needle.size();int i=1, j=0;while(i<n) {if (needle[i] == needle[j]) {j++;next[i]=j;i++;} else {if (j == 0) {next[i]=0;i++;} else {j = next[j-1];}}}}
};

參考鏈接

bilibili KMP講解
來源:力扣(LeetCode)

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/375537.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/375537.shtml
英文地址,請注明出處:http://en.pswp.cn/news/375537.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

有效電話號碼

給定一個包含電話號碼列表&#xff08;一行一個電話號碼&#xff09;的文本文件 file.txt&#xff0c;寫一個 bash 腳本輸出所有有效的電話號碼。 你可以假設一個有效的電話號碼必須滿足以下兩種格式&#xff1a; (xxx) xxx-xxxx 或 xxx-xxx-xxxx。&#xff08;x 表示一個數字…

JSP中RequestDispatcher的用法

RequestDispatcher是一個Web資源的包裝器&#xff0c;可以用來把當前request傳遞到該資源&#xff0c;或者把新的資源包括到當前響應中。RequestDispatcher接口中定義了兩個方法&#xff1a;include/forward 由于<jsp:include>只能指定固定的jsp文件名&#xff0c;不能動…

TCP/IP三次握手與四次握手

原文地址 http://blog.csdn.net/whuslei/article/details/6667471 http://blog.csdn.net/wo2niliye/article/details/48447933 建立TCP需要三次握手才能建立&#xff0c;而斷開連接則需要四次握手。整個過程如下圖所示&#xff1a; 先來看看如何建立連接的。 首先Client端發送連…

vim支持nginx語法高亮

下載nginx源碼&#xff0c;解壓之后&#xff0c;將contribu/vim/*拷貝到~/.vim/目錄&#xff0c;如果沒有~/.vim/目錄&#xff0c;則創建即可。 cp -r contrib/vim/* ~/.vim/或 mkdir -p ~/.vim/ cp -r contrib/vim/* ~/.vim/此時再打開conf/nginx.conf就可以看到已經語法高亮…

Delphi 正則表達式語法(4): 常用轉義字符與 .

Delphi 正則表達式語法(4): 常用轉義字符與 . // \d 匹配所有數字, 相當于 [0-9] varreg: TPerlRegEx; beginreg : TPerlRegEx.Create(nil);reg.Subject : 期待Delphi 2008 for Win32&#xff01;;reg.RegEx : \d;reg.Replacement : ◆;reg.ReplaceAll;ShowMessage(reg.Subje…

C語言操作mysql

php中 mysqli, pdo 可以用 mysqlnd 或 libmysqlclient 實現 前者 從 php 5.3.0起已內置到php中, 并且支持更多的特性&#xff0c;推薦用 mysqlnd mysqlnd &#xff0c; libmysqlclient 對比&#xff1a;http://php.net/manual/en/mysqlinfo.library.choosing.php mysqlnd 目前是…

Hadoop DistributedCache分布式緩存的使用

轉載請注明&#xff1a;http://www.cnblogs.com/demievil/p/4059141.html 我的github博客&#xff1a;http://demievil.github.io/ 做項目的時候遇到一個問題&#xff0c;在Mapper和Reducer方法中處理目標數據時&#xff0c;先要去檢索和匹配一個已存在的標簽庫&#xff0c;再對…

每日溫度

根據每日 氣溫 列表&#xff0c;請重新生成一個列表&#xff0c;對應位置的輸出是需要再等待多久溫度才會升高超過該日的天數。如果之后都不會升高&#xff0c;請在該位置用 0 來代替。 例如&#xff0c;給定一個列表 temperatures [73, 74, 75, 71, 69, 72, 76, 73]&#xf…

什么是Modbus

什么是Modbus 1. Modbus如何工作 Modbus是通過設備之間的幾根連線來傳遞數據&#xff0c;最簡單的設置就是主站和從站之間用一跟串口線相連。數據通過一串0或者1來傳遞&#xff0c;也就是位。0為正電壓&#xff0c;1為負電壓。位數據傳遞速度非常快&#xff0c;常見的傳輸速度為…

博客剛剛開通!

今天老賊開播了&#xff01;以后請大家多多關照&#xff01; 轉載于:https://www.cnblogs.com/xiaosayi/p/4065313.html

Android實例-拍攝和分享照片、分享文本(XE8+小米2)

結果&#xff1a; 1.分享文本不好使&#xff0c;原因不明。有大神了解的&#xff0c;請M我&#xff0c;在此十分感謝。 2.如果想支持圖片編輯&#xff0c;將Action事件的Editable改為True。 相關資料&#xff1a; 官網地址&#xff1a;http://docwiki.embarcadero.com/RADStudi…

go語言 expected ; found a

錯誤代碼&#xff0c;這是一段測試go語言類型轉換的代碼 package type_testimport "testing"type MyInt int64func TestImplicit(t *testing.T) {var a int32 1var b int64 3b (int64)avar c MyInt 4// c bt.Log(a, b, c) }報錯代碼 b (int64)a改正 b int6…

win8 metro 調用攝像頭拍攝照片并將照片保存在對應的位置

剛剛做過這類開發&#xff0c;所以就先獻丑了&#xff0c;當然所貼上的源代碼都是經過驗證過的&#xff0c;已經執行成功了&#xff0c;希望能夠給大家一些借鑒&#xff1a; 以下是metro UI代碼&#xff1a; <Pagex:Class"Camera.MainPage"xmlns"http://sche…

poj 3678 Katu Puzzle(2-sat)

Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean operator op (one of AND, OR, XOR) and an integer c (0 ≤ c ≤ 1). One Katu is solvable if one can find each vertex Vi a value Xi (0 ≤ Xi ≤ 1) suc…

go 語言 first argument to append must be slice

錯誤代碼 func TestSliceGrowing(t *testing.T) {s : [4]int{1, 2, 3, 4}for i :0; i<10; i {s append(s, i)t.Log(len(s), cap(s))} }報錯代碼 s append(s, i)原因&#xff1a;append的第一個參數必須是切片 更正 func TestSliceGrowing(t *testing.T) {s : []int{1,…

豆瓣網靜態頁面

divcss網站登錄注冊豆瓣讀書視頻 音樂同城小組閱讀 豆瓣FM東西更多豆瓣視頻 影訊&購票電視劇排行榜 分類影評預告片 向后向前3/5正在熱映全部正在熱映>>即將上映 烈日灼心 4.7終結者&#xff1a;創世紀... 4.7百團大戰 4.7刺客&#xff1a;聶隱娘 4.7近期熱門更多影視…

C++并發編程實戰(豆瓣評分5.4)

評分已說明一切&#xff0c;切勿踩坑&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01; 推薦的翻譯 C并發編程實戰 關注公眾號回復【C并發編程實…

Please use boost/bind/bind.hpp + using namespace boost::placeholders

The practice of declaring the Bind placeholders (_1, _2, …) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior. 提示w…

奔跑吧,兄弟

10月底的時候&#xff0c;不能忍受老婆的奚落&#xff0c;開始了我的跑步計劃。 說說&#xff0c;跑步需要注意的事項&#xff0c;首先你得有雙跑步鞋&#xff0c;我有一次是穿了薄底鞋跑的&#xff0c;結果&#xff0c;打滿了水泡。跑步前控制飲水&#xff0c;最好在飲食后2個…

2299 Ultra-QuickSort(歸并)

合并排序第一次。連環畫看著合并看著別人的博客的想法。http://poj.org/problem?id2299 #include <stdio.h> #include <stdlib.h>#define MAX 500001int n,a[MAX], t[MAX]; long long int sum;//歸并 void Merge(int l, int m, int r) {int p0;int il, jm1;while…