日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LintCode 字符串查找

發布時間:2025/3/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LintCode 字符串查找 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

對于一個給定的 source 字符串和一個 target 字符串,你應該在 source 字符串中找出 target 字符串出現的第一個位置(從0開始)。如果不存在,則返回?-1。

說明

在面試中我是否需要實現KMP算法?

  • 不需要,當這種問題出現在面試中時,面試官很可能只是想要測試一下你的基礎應用能力。當然你需要先跟面試官確認清楚要怎么實現這個題。
樣例

如果 source =?"source"?和 target =?"target",返回?-1。

如果 source =?"abcdabcdefg"?和 target =?"bcd",返回?1。

class Solution { public:/*** Returns a index to the first occurrence of target in source,* or -1 if target is not part of source.* @param source string to be scanned.* @param target string containing the sequence of characters to match.*/int strStr(const char *source, const char *target) {// write your code hereif(source==NULL||target==NULL)return -1;if(*target=='\0')//查找的是空串return 0;int i=0;int j=-1;while(*source!='\0'){j++;if(*source==*target)//找首字母相同的{i=j;const char *p=source;//用兩個指針const char *q=target;while(*p==*q){if(*(q+1)=='\0')return i;else {p++;q++;}}}source++;}return -1;} };

  

轉載于:https://www.cnblogs.com/lelelelele/p/6135741.html

總結

以上是生活随笔為你收集整理的LintCode 字符串查找的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。