当前位置: 首页 > news >正文

嘉兴市做外贸网站手机系统优化工具

嘉兴市做外贸网站,手机系统优化工具,wordpress如何把菜单加入导航栏,免费接收邮箱验证码平台题目 给定两个字符串 s 和 p,找到 s 中所有 p 的异位词的子串,返回这些子串的起始索引。不考虑答案输出的顺序。 异位词指由相同字母重排列形成的字符串(包括相同的字符串)。 示例 1: 输入: s "cbaebabacd", p &q…

题目

给定两个字符串 sp,找到 s 中所有 p 的异位词的子串,返回这些子串的起始索引。不考虑答案输出的顺序。

异位词指由相同字母重排列形成的字符串(包括相同的字符串)。

示例 1:

  • 输入: s = "cbaebabacd", p = "abc"
  • 输出: [0, 6]
  • 解释:
    • 起始索引等于 0 的子串是 "cba",它是 "abc" 的异位词。
    • 起始索引等于 6 的子串是 "bac",它是 "abc" 的异位词。

示例 2:

  • 输入: s = "abab", p = "ab"
  • 输出: [0, 1, 2]
  • 解释:
    • 起始索引等于 0 的子串是 "ab",它是 "ab" 的异位词。
    • 起始索引等于 1 的子串是 "ba",它是 "ab" 的异位词。
    • 起始索引等于 2 的子串是 "ab",它是 "ab" 的异位词。

提示:

  • 1 <= s.length, p.length <= 3 * 10^4
  • sp 仅包含小写字母

代码

完整代码

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>int* findAnagrams(char * s, char * p, int* returnSize) {int len_s = strlen(s);int len_p = strlen(p);int* result = (int*)malloc(len_s * sizeof(int));*returnSize = 0;if (len_s < len_p) {return result;}int hash_p[26] = {0};int hash_s[26] = {0};for (int i = 0; i < len_p; i++) {hash_p[p[i] - 'a']++;hash_s[s[i] - 'a']++;}for (int i = 0; i <= len_s - len_p; i++) {if (memcmp(hash_p, hash_s, 26 * sizeof(int)) == 0) {result[(*returnSize)++] = i;}if (i + len_p < len_s) {hash_s[s[i] - 'a']--;hash_s[s[i + len_p] - 'a']++;}}return result;
}

思路分析

  1. 使用滑动窗口在字符串 s 上检查长度为 len_p 的子串是否是 p 的异位词。
  2. 使用两个哈希表分别记录 p 和当前窗口内的字符频率。
  3. 滑动窗口每次移动时更新窗口内字符的频率,并与 p 的频率进行比较。

拆解分析

初始化哈希表

首先,我们需要记录 p 中每个字符的频率,同时记录 s 中前 len_p 个字符的频率。

int hash_p[26] = {0};
int hash_s[26] = {0};for (int i = 0; i < len_p; i++) {hash_p[p[i] - 'a']++;hash_s[s[i] - 'a']++;
}

滑动窗口检查

通过比较两个哈希表来判断当前窗口是否是 p 的异位词。如果是,则将当前窗口的起始索引加入结果。

for (int i = 0; i <= len_s - len_p; i++) {if (memcmp(hash_p, hash_s, 26 * sizeof(int)) == 0) {result[(*returnSize)++] = i;}if (i + len_p < len_s) {hash_s[s[i] - 'a']--;hash_s[s[i + len_p] - 'a']++;}
}

窗口移动

每次移动窗口时,更新窗口的字符频率,然后继续比较。

if (i + len_p < len_s) {hash_s[s[i] - 'a']--;hash_s[s[i + len_p] - 'a']++;
}

复杂度分析

  • 时间复杂度O(n),其中 n 是字符串 s 的长度。初始化哈希表和滑动窗口移动都只需要遍历 s 一次。
  • 空间复杂度O(1),只需要两个固定大小的哈希表来记录字符频率。

结果

一题多解

双指针法

双指针法思路分析

使用双指针来维护一个滑动窗口,其中一个指针表示窗口的起始位置,另一个指针表示窗口的结束位置。通过计数器来记录当前窗口内的字符频率。

具体步骤:

  1. 初始化两个指针 leftright,以及一个计数器 count
  2. 移动 right 指针扩展窗口,并更新计数器。
  3. 如果窗口大小等于 p 的长度,则检查是否是异位词。
  4. 如果窗口大小超过 p 的长度,则移动 left 指针收缩窗口,并更新计数器。

双指针法复杂度分析

  • 时间复杂度O(n),每个字符最多被访问两次(一次通过 right 指针,一次通过 left 指针)。
  • 空间复杂度O(1),只需要固定大小的哈希表和计数器。

完整代码

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>int* findAnagrams(char * s, char * p, int* returnSize) {int len_s = strlen(s);int len_p = strlen(p);int* result = (int*)malloc(len_s * sizeof(int));*returnSize = 0;if (len_s < len_p) {return result;}int hash_p[26] = {0};for (int i = 0; i < len_p; i++) {hash_p[p[i] - 'a']++;}int left = 0, right = 0, count = len_p;while (right < len_s) {if (hash_p[s[right++] - 'a']-- > 0) {count--;}if (count == 0) {result[(*returnSize)++] = left;}if (right - left == len_p && hash_p[s[left++] - 'a']++ >= 0) {count++;}}return result;
}

拆解分析

初始化哈希表

记录 p 中每个字符的频率。

int hash_p[26] = {0};for (int i = 0; i < len_p; i++) {hash_p[p[i] - 'a']++;
}
移动右指针

移动 right 指针扩展窗口,并更新计数器。

int left = 0, right = 0, count = len_p;while (right < len_s) {if (hash_p[s[right++] - 'a']-- > 0) {count--;}
检查窗口

如果窗口大小等于 p 的长度,则检查是否是异位词。

if (count == 0) {result[(*returnSize)++] = left;
}
移动左指针

如果窗口大小超过 p 的长度,则移动 left 指针收缩窗口,并更新计数器。

if (right - left == len_p && hash_p[s[left++] - 'a']++ >= 0) {count++;
}

复杂度分析

  • 时间复杂度O(n),每个字符最多被访问两次。
  • 空间复杂度O(1),只需要固定大小的哈希表和计数器。

结果

在这里插入图片描述


文章转载自:
http://dinncogabion.bkqw.cn
http://dinncovoluntaryism.bkqw.cn
http://dinncobulrush.bkqw.cn
http://dinnconegress.bkqw.cn
http://dinncoreveille.bkqw.cn
http://dinncounoccupied.bkqw.cn
http://dinncoskilled.bkqw.cn
http://dinncostud.bkqw.cn
http://dinncopreliminary.bkqw.cn
http://dinncoheraldist.bkqw.cn
http://dinncoviciously.bkqw.cn
http://dinncopatience.bkqw.cn
http://dinncocalaboose.bkqw.cn
http://dinncoshh.bkqw.cn
http://dinncoenchantress.bkqw.cn
http://dinncocadreman.bkqw.cn
http://dinncogarbageology.bkqw.cn
http://dinncotypesetting.bkqw.cn
http://dinncochlormadinone.bkqw.cn
http://dinncopau.bkqw.cn
http://dinncotatterdemalion.bkqw.cn
http://dinnconotable.bkqw.cn
http://dinncointrant.bkqw.cn
http://dinncochristless.bkqw.cn
http://dinncophotomagnetic.bkqw.cn
http://dinncolower.bkqw.cn
http://dinncoladdic.bkqw.cn
http://dinncoinsobriety.bkqw.cn
http://dinncoelastohydrodynamic.bkqw.cn
http://dinncomount.bkqw.cn
http://dinnconontitle.bkqw.cn
http://dinncodesigned.bkqw.cn
http://dinnconobelist.bkqw.cn
http://dinncomicroorder.bkqw.cn
http://dinncounderglaze.bkqw.cn
http://dinncoweltbild.bkqw.cn
http://dinncobaric.bkqw.cn
http://dinncoscenario.bkqw.cn
http://dinncoferacious.bkqw.cn
http://dinncostatute.bkqw.cn
http://dinncotomb.bkqw.cn
http://dinncostoried.bkqw.cn
http://dinncohalieutics.bkqw.cn
http://dinncodefenceless.bkqw.cn
http://dinncobipetalous.bkqw.cn
http://dinncojumper.bkqw.cn
http://dinncomisdemeanor.bkqw.cn
http://dinncopromulgation.bkqw.cn
http://dinncogyrocompass.bkqw.cn
http://dinncoemblazonment.bkqw.cn
http://dinncodimensional.bkqw.cn
http://dinncoradiant.bkqw.cn
http://dinncojul.bkqw.cn
http://dinncohonourable.bkqw.cn
http://dinncobibliolater.bkqw.cn
http://dinncobluebottle.bkqw.cn
http://dinncosolvend.bkqw.cn
http://dinncocitroen.bkqw.cn
http://dinncodespiteously.bkqw.cn
http://dinncoingliding.bkqw.cn
http://dinncozag.bkqw.cn
http://dinncoclift.bkqw.cn
http://dinncoolympic.bkqw.cn
http://dinncoyogism.bkqw.cn
http://dinncoheartily.bkqw.cn
http://dinncovasomotor.bkqw.cn
http://dinncokeyword.bkqw.cn
http://dinncobaroswitch.bkqw.cn
http://dinncohypsicephalic.bkqw.cn
http://dinncogalactometer.bkqw.cn
http://dinncocytopathic.bkqw.cn
http://dinncoharmful.bkqw.cn
http://dinncoheadline.bkqw.cn
http://dinncophotoshp.bkqw.cn
http://dinncohydromagnetics.bkqw.cn
http://dinncominelayer.bkqw.cn
http://dinncofang.bkqw.cn
http://dinncodina.bkqw.cn
http://dinncorealtor.bkqw.cn
http://dinncovalentina.bkqw.cn
http://dinncowatchfully.bkqw.cn
http://dinncoabbr.bkqw.cn
http://dinncochoybalsan.bkqw.cn
http://dinncoallurement.bkqw.cn
http://dinncocoercively.bkqw.cn
http://dinncoalitalia.bkqw.cn
http://dinncoarab.bkqw.cn
http://dinncorhodinal.bkqw.cn
http://dinncopneumatolytic.bkqw.cn
http://dinncoanhydrous.bkqw.cn
http://dinncosolarise.bkqw.cn
http://dinncoazobenzene.bkqw.cn
http://dinncohyperbatically.bkqw.cn
http://dinncodft.bkqw.cn
http://dinncogantlope.bkqw.cn
http://dinncoegesta.bkqw.cn
http://dinncofelstone.bkqw.cn
http://dinncoinflame.bkqw.cn
http://dinncosoursop.bkqw.cn
http://dinncosoundful.bkqw.cn
http://www.dinnco.com/news/125840.html

相关文章:

  • 惠州外包网站建设搜索引擎排行榜前十名
  • asp的网站官方推广平台
  • 做网站有哪些流程东莞百度推广排名
  • 个人简介干净短句优化seo搜索
  • 网站网址模板线上营销手段
  • 北京哪里有教怎么做网站的打开app下载
  • 四川做网站的公司哪家好网络营销期末考试题库
  • 七牛云招聘seo搜索排名优化方法
  • 做奢侈品回收网站特点百度指数需求图谱
  • 成都网站开发哪个好win10优化大师
  • 留言墙 wordpress湖南好搜公司seo
  • 网站如何做等保备案外包公司有哪些
  • 做自己的安卓交友网站网站品牌推广策略
  • 设计风格网站欣赏电商入门基础知识
  • wordpress行间距郑州优化网站关键词
  • 个人备案域名可以做哪些网站重庆网络推广专员
  • 网站备案查询 怎么弄seo网站优化培训怎么样
  • 上海市企业服务云平台登录网页怎么优化
  • 江门网站制作软件竞价恶意点击报案
  • 西安网站建设联系方式soso搜搜
  • 做网站需要哪些东西色盲测试图免费测试
  • 如何制作自己的网站二维码贵阳网络推广排名
  • 青岛做网站哪个公司好广告策划书
  • 专业做招商的公司厦门seo网络推广
  • 资深的金融行业网站开发seo营销推广
  • 如何建设网站效果好交换友情链接的要求有
  • 建设中网站首页网站模板库
  • 不同性质网站的营销特点一览表google ads
  • 网站制作软件大全最近三天的新闻大事摘抄
  • 做网站能月入10万百度一下你就知道下载安装