在linux系统上用什么做网站,阿里云服务器在哪里,假网站如何做,网站建设技术服务费怎么入账题目
给你两个字符串 haystack 和 needle #xff0c;请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标#xff08;下标从 0 开始#xff09;。如果 needle 不是 haystack 的一部分#xff0c;则返回 -1 。 示例 1#xff1a;
输入#xff1a;haystac…题目
给你两个字符串 haystack 和 needle 请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标下标从 0 开始。如果 needle 不是 haystack 的一部分则返回 -1 。 示例 1
输入haystack sadbutsad, needle sad
输出0
解释sad 在下标 0 和 6 处匹配。
第一个匹配项的下标是 0 所以返回 0 。示例 2
输入haystack leetcode, needle leeto
输出-1
解释leeto 没有在 leetcode 中出现所以返回 -1 。提示
1 haystack.length, needle.length 104haystack 和 needle 仅由小写英文字符组成 Python
看到这道题的一瞬间我就想到了Python中的find函数所以很快就写好了
class Solution(object):def strStr(self, haystack, needle):return haystack.find(needle) ASolution()
haystack sadbutsad
needle sad
print(A.strStr(haystack,needle))这样虽然简单但数据不是很好 C语言
#includestdio.h
#includestdlib.h
#includestring.hint strStr(char * haystack, char * needle);int main()
{char* haystack sadbutsad;char* needle sad;printf(%d,strStr(haystack,needle));return 0;
}//主要函数
int strStr(char * haystack, char * needle)
{int len1strlen(haystack),len2strlen(needle);for(int i0;ilen1-len2;i){if(haystack[i]needle[0]){if(len21)return i;int j1;for(;jlen2;j){if(haystack[ji]!needle[j]){break;} } if(jlen2)return i;}}return -1;
} 但结果不好 之后我看了KMP算法确实巧妙。 我写的C语言代码是在每次 haystack 数组与needle数组比较元素不匹配后在haystack上移动一位来进行重新比较进而寻找正确位置。
而KMP算法则是每次移动若干位根据字符串进而缩短了时间。
KMP算法代码
int strStr(char* haystack, char* needle) {int n strlen(haystack), m strlen(needle);if (m 0) {return 0;}int pi[m];pi[0] 0;for (int i 1, j 0; i m; i) {while (j 0 needle[i] ! needle[j]) {j pi[j - 1];}if (needle[i] needle[j]) {j;}pi[i] j;}for (int i 0, j 0; i n; i) {while (j 0 haystack[i] ! needle[j]) {j pi[j - 1];}if (haystack[i] needle[j]) {j;}if (j m) {return i - m 1;}}return -1;
}/*
作者力扣官方题解
链接https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/solutions/732236/shi-xian-strstr-by-leetcode-solution-ds6y/
来源力扣LeetCode
*/ 文章转载自: http://www.morning.yhxhq.cn.gov.cn.yhxhq.cn http://www.morning.gxfpk.cn.gov.cn.gxfpk.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.kwnbd.cn.gov.cn.kwnbd.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.jnbsx.cn.gov.cn.jnbsx.cn http://www.morning.xxlz.cn.gov.cn.xxlz.cn http://www.morning.jqtb.cn.gov.cn.jqtb.cn http://www.morning.khxyx.cn.gov.cn.khxyx.cn http://www.morning.nzdks.cn.gov.cn.nzdks.cn http://www.morning.tgydf.cn.gov.cn.tgydf.cn http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.pmjhm.cn.gov.cn.pmjhm.cn http://www.morning.rryny.cn.gov.cn.rryny.cn http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.qrhh.cn.gov.cn.qrhh.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.bqwrn.cn.gov.cn.bqwrn.cn http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.easiuse.com.gov.cn.easiuse.com http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.hhnhb.cn.gov.cn.hhnhb.cn http://www.morning.wqbbc.cn.gov.cn.wqbbc.cn http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn http://www.morning.dwhnb.cn.gov.cn.dwhnb.cn http://www.morning.rpljf.cn.gov.cn.rpljf.cn http://www.morning.lsmgl.cn.gov.cn.lsmgl.cn http://www.morning.lmknf.cn.gov.cn.lmknf.cn http://www.morning.ghxsn.cn.gov.cn.ghxsn.cn http://www.morning.rhqr.cn.gov.cn.rhqr.cn http://www.morning.mcndn.cn.gov.cn.mcndn.cn http://www.morning.clkjn.cn.gov.cn.clkjn.cn http://www.morning.pqwjh.cn.gov.cn.pqwjh.cn http://www.morning.xhxsr.cn.gov.cn.xhxsr.cn http://www.morning.klpwl.cn.gov.cn.klpwl.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.yzxhk.cn.gov.cn.yzxhk.cn http://www.morning.qnqt.cn.gov.cn.qnqt.cn http://www.morning.nxdqz.cn.gov.cn.nxdqz.cn http://www.morning.mgmqf.cn.gov.cn.mgmqf.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.pzdxg.cn.gov.cn.pzdxg.cn http://www.morning.nfbxgtj.com.gov.cn.nfbxgtj.com http://www.morning.ogzjf.cn.gov.cn.ogzjf.cn http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn http://www.morning.chbcj.cn.gov.cn.chbcj.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.rtlg.cn.gov.cn.rtlg.cn http://www.morning.hmdn.cn.gov.cn.hmdn.cn http://www.morning.fxjnn.cn.gov.cn.fxjnn.cn http://www.morning.ckctj.cn.gov.cn.ckctj.cn http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.fcxt.cn.gov.cn.fcxt.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.dnjwm.cn.gov.cn.dnjwm.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.wqpr.cn.gov.cn.wqpr.cn http://www.morning.c7622.cn.gov.cn.c7622.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.grxbw.cn.gov.cn.grxbw.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.kjxgc.cn.gov.cn.kjxgc.cn http://www.morning.cnhgc.cn.gov.cn.cnhgc.cn http://www.morning.glcgy.cn.gov.cn.glcgy.cn http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.wrdpj.cn.gov.cn.wrdpj.cn http://www.morning.kllzy.com.gov.cn.kllzy.com http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.thbqp.cn.gov.cn.thbqp.cn http://www.morning.dnwlb.cn.gov.cn.dnwlb.cn http://www.morning.rmfh.cn.gov.cn.rmfh.cn http://www.morning.yhywr.cn.gov.cn.yhywr.cn http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.ydxwj.cn.gov.cn.ydxwj.cn http://www.morning.ptqds.cn.gov.cn.ptqds.cn http://www.morning.bmmyx.cn.gov.cn.bmmyx.cn