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

如何做网站清风制作怎么建立一个网站

如何做网站清风制作,怎么建立一个网站,网站开发er图,北京网站建设公司大全1.串联所有单词的子串 给定一个字符串 s 和一个字符串数组 words。 words 中所有字符串 长度相同。 s 中的 串联子串 是指一个包含 words 中所有字符串以任意顺序排列连接起来的子串。 例如,如果 words ["ab","cd","ef"]&#xff…

1.串联所有单词的子串

   

给定一个字符串 s 和一个字符串数组 words words 中所有字符串 长度相同

 s 中的 串联子串 是指一个包含  words 中所有字符串以任意顺序排列连接起来的子串。

  • 例如,如果 words = ["ab","cd","ef"], 那么 "abcdef", "abefcd""cdabef", "cdefab""efabcd", 和 "efcdab" 都是串联子串。 "acdbef" 不是串联子串,因为他不是任何 words 排列的连接。

返回所有串联子串在 s 中的开始索引。你可以以 任意顺序 返回答案。

示例 1:

输入:s = "barfoothefoobarman", words = ["foo","bar"]
输出:[0,9]
解释:因为 words.length == 2 同时 words[i].length == 3,连接的子字符串的长度必须为 6。
子串 "barfoo" 开始位置是 0。它是 words 中以 ["bar","foo"] 顺序排列的连接。
子串 "foobar" 开始位置是 9。它是 words 中以 ["foo","bar"] 顺序排列的连接。
输出顺序无关紧要。返回 [9,0] 也是可以的。

示例 2:

输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
输出:[]
解释:因为 words.length == 4 并且 words[i].length == 4,所以串联子串的长度必须为 16。
s 中没有子串长度为 16 并且等于 words 的任何顺序排列的连接。
所以我们返回一个空数组。

示例 3:

输入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
输出:[6,9,12]
解释:因为 words.length == 3 并且 words[i].length == 3,所以串联子串的长度必须为 9。
子串 "foobarthe" 开始位置是 6。它是 words 中以 ["foo","bar","the"] 顺序排列的连接。
子串 "barthefoo" 开始位置是 9。它是 words 中以 ["bar","the","foo"] 顺序排列的连接。
子串 "thefoobar" 开始位置是 12。它是 words 中以 ["the","foo","bar"] 顺序排列的连接。

算法原理

我们随便来看一个例子 输入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]

我们把bar 看成一个a,foo看成一个b,the看成一个c

 这个题的s就可以看成一串 abbacvad

这样这个题就变成一个找出字符串你的异位字符,变成跟我们做的上个题是一样的

这道题我们要熟练的运用容器以及哈希表和List表

这道题我们right-left的长度不能大于words数组的长度,即单词的长度

由于我们不确定从哪里开始是我们想要找的单词的首字母,我们需要遍历单个单词的每个字母如下图所示

我们要对一个单词遍历这个单词的每个字母,所以我们要套两层for循环

第一层for循环就是   for(int i=0;i<len;i++) 表示words单词里面每个单词的长度

1.定义left=i,right=i 还需要定义一个count用来记录单词的数量定义两个哈希表,一个哈希表hash1用来记录words数组里面单词的个数,一个哈希表hash2用来记录s表内的单词,用len表示words单词的长度

2.我们的起始位置是right=i,终止位置时 right+len 每次向后移动都是移动len的长度

3.我们首先需要进窗口,用in来接受进入窗口的单词,然后我们判断进来窗口的单是否在 hash1中存在 存在就count++ 

3,当我们一直向后判断,当right-left的值>words里面字符的m(m表示单词的个数)*len了 说明此时我们应该出窗口了 在出窗口前我们需要判断出窗口的单词是否存在于hash1表中,存在我们就count--

然后left+len判断下一个单词

4.当count==m 说明此时正是我们要找的子串,我们输出left的值

代码如下

class Solution {public List<Integer> findSubstring(String s, String[] words) {List<Integer>ret=new ArrayList();Map<String,Integer>hash1=new HashMap<String,Integer>();for(String str:words)hash1.put(str,hash1.getOrDefault(str,0)+1);int len=words[0].length(),m=words.length;for(int i=0;i<len;i++){Map<String,Integer>hash2=new HashMap<String,Integer>();for(int left=i,right=i,count=0;right+len<=s.length();right+=len){String in=s.substring(right,right+len);hash2.put(in,hash2.getOrDefault(in,0)+1);if(hash2.get(in)<=hash1.getOrDefault(in,0))count++;if(right-left+1>len*m){String out=s.substring(left,left+len);if(hash2.get(out)<=hash1.getOrDefault(out,0))count--;hash2.put(out,hash2.get(out)-1);left+=len;}if(count==m){ret.add(left);}}}return ret;}
}

2.最小覆盖子串

  

给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。

注意:

  • 对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量。
  • 如果 s 中存在这样的子串,我们保证它是唯一的答案。

示例 1:

输入:s = "ADOBECODEBANC", t = "ABC"
输出:"BANC"
解释:最小覆盖子串 "BANC" 包含来自字符串 t 的 'A'、'B' 和 'C'。

示例 2:

输入:s = "a", t = "a"
输出:"a"
解释:整个字符串 s 是最小覆盖子串。

示例 3:

输入: s = "a", t = "aa"
输出: ""
解释: t 中两个字符 'a' 均应包含在 s 的子串中,
因此没有符合条件的子字符串,返回空字符串。

   程序解法:

      1.暴力解法:通过暴力枚举,枚举出所有结果,然后比对所有结果那个子串最短

      2.滑动窗口+哈希表

  算法原理 

这个题我们依旧用滑动窗口来做,并且用数组来模拟哈希表

如图我们有以下的一个字符串,我们要找的是abc的最小子串,

1.我们给字符串和abc串各创建一个数组

    int[]hash1=new int[128];

int[] hash2=new int[128];

hash1用来存放abc,hash2用来存放长的字符串

我们来统计hash1中的个数,这里使用统计个数的方式不可取,因为长的字符串中有可能有多个一样的字符,我们就取第一次出现的字符为新的字符,就是取种类的个数

 for(char ch:t){if(hash1[ch]==0)kinds++;hash1[ch]++;}
 

2.采用滑动窗口的方式

        定义left=0,right=0,count=0;

    我们首先进入窗口,进窗口之后判断hash2[right]==hash1[right] 相等就++ 

   

 int minlen=Integer.MAX_VALUE,begin=-1;for(int left=0,right=0,count=0;right<ss.length();right++){char in=s[right];hash2[in]++;if(hash1[in]==hash2[in])count++;

然后当kind和count相等时 我们更新结果,取出当前right-left+1的值,和开始的值

 int minlen=Integer.MAX_VALUE,begin=-1;
while(kinds==count){if(right-left+1<minlen){minlen=right-left+1;begin=left;}

3.更新完结果后我们出窗口

  

 char out=s[left];left++;if(hash1[out]==hash2[out]) count--;hash2[out]--;

最后代码如下

class minWindow {public String minWindow1  (String ss, String tt) {char s[]=ss.toCharArray();char t[]=tt.toCharArray();int[]hash1=new int[128];int[] hash2=new int[128];int kinds=0;for(char ch:t){if(hash1[ch]==0)kinds++;hash1[ch]++;}int minlen=Integer.MAX_VALUE,begin=-1;for(int left=0,right=0,count=0;right<ss.length();right++){char in=s[right];hash2[in]++;if(hash1[in]==hash2[in])count++;while(kinds==count){if(right-left+1<minlen){minlen=right-left+1;begin=left;}char out=s[left];left++;if(hash1[out]==hash2[out]) count--;hash2[out]--;}}if(begin==-1)return new String();else return ss.substring(begin,begin+minlen);}public static void main(String[] args) {minWindow minWindow=new minWindow();String s="ADOBECODEBANC";String t="AABC";String result= minWindow.minWindow1(s,t);System.out.println(result);}
}


文章转载自:
http://vvsop.tbjb.cn
http://seraphic.tbjb.cn
http://ventrotomy.tbjb.cn
http://schizophyte.tbjb.cn
http://prediabetes.tbjb.cn
http://sadie.tbjb.cn
http://graybeard.tbjb.cn
http://nucleoprotein.tbjb.cn
http://calorimetry.tbjb.cn
http://housedress.tbjb.cn
http://reflexology.tbjb.cn
http://chromophoric.tbjb.cn
http://skutterudite.tbjb.cn
http://babs.tbjb.cn
http://petechia.tbjb.cn
http://radiolysis.tbjb.cn
http://buck.tbjb.cn
http://baniyas.tbjb.cn
http://electrization.tbjb.cn
http://hypo.tbjb.cn
http://allergen.tbjb.cn
http://kokanee.tbjb.cn
http://mordida.tbjb.cn
http://nitrosylsulphuric.tbjb.cn
http://unstructured.tbjb.cn
http://didymium.tbjb.cn
http://foghorn.tbjb.cn
http://rosaceous.tbjb.cn
http://jct.tbjb.cn
http://nubian.tbjb.cn
http://sacrificial.tbjb.cn
http://mucluc.tbjb.cn
http://proletarianism.tbjb.cn
http://realia.tbjb.cn
http://dory.tbjb.cn
http://snakeskin.tbjb.cn
http://moonshiner.tbjb.cn
http://insignificance.tbjb.cn
http://crepitate.tbjb.cn
http://erse.tbjb.cn
http://lavement.tbjb.cn
http://durzi.tbjb.cn
http://snowdon.tbjb.cn
http://lufthansa.tbjb.cn
http://piperaceous.tbjb.cn
http://preachify.tbjb.cn
http://overplaid.tbjb.cn
http://marxist.tbjb.cn
http://engram.tbjb.cn
http://degrading.tbjb.cn
http://hyperion.tbjb.cn
http://contingence.tbjb.cn
http://fumade.tbjb.cn
http://perfusive.tbjb.cn
http://couchy.tbjb.cn
http://bacciferous.tbjb.cn
http://ovariectomize.tbjb.cn
http://gam.tbjb.cn
http://erroneous.tbjb.cn
http://signior.tbjb.cn
http://guienne.tbjb.cn
http://effortless.tbjb.cn
http://rident.tbjb.cn
http://doglike.tbjb.cn
http://monoicous.tbjb.cn
http://mystificatory.tbjb.cn
http://abwatt.tbjb.cn
http://desiderata.tbjb.cn
http://immutably.tbjb.cn
http://catabolize.tbjb.cn
http://stearin.tbjb.cn
http://duramen.tbjb.cn
http://rearmament.tbjb.cn
http://ramona.tbjb.cn
http://cyclo.tbjb.cn
http://citybilly.tbjb.cn
http://argufy.tbjb.cn
http://emoticons.tbjb.cn
http://gran.tbjb.cn
http://resilin.tbjb.cn
http://biocenose.tbjb.cn
http://phallical.tbjb.cn
http://excusing.tbjb.cn
http://deuteranope.tbjb.cn
http://pursuivant.tbjb.cn
http://puncher.tbjb.cn
http://urning.tbjb.cn
http://geobiological.tbjb.cn
http://yorks.tbjb.cn
http://receptive.tbjb.cn
http://reorder.tbjb.cn
http://selachoid.tbjb.cn
http://specky.tbjb.cn
http://enneahedral.tbjb.cn
http://frontier.tbjb.cn
http://jmb.tbjb.cn
http://akureyri.tbjb.cn
http://goddamn.tbjb.cn
http://palish.tbjb.cn
http://jigaboo.tbjb.cn
http://www.dt0577.cn/news/58184.html

相关文章:

  • 新乡企业网站建设公司互联网推广是什么意思
  • 南桥网站建设石家庄线上推广平台
  • 怎么可以预览自己做的网站免费论坛建站系统
  • 提供网站建设设计公司排名营销管理
  • 广西建设执业资格注册中心网站湘潭网站seo
  • 深圳seo公司排名优化师培训机构
  • 在线生成网站地图无锡网络推广平台
  • 网站建设调研报告的前言网址查询工具
  • 网站建设的步骤和要点百度快速收录工具
  • 注册什么公司给别人做网站如何制作链接推广
  • 多用户网上商城网站怎么做优化排名
  • 那些网站h5做的不错百度竞价推广开户价格
  • 淘宝网站代理怎么做重庆seo主管
  • 视觉设计网站小说关键词提取软件
  • 看网站有没有做404互联网品牌宣传推广服务公司
  • 旅游景区网站建设规划优化百度涨
  • 手机端做网站软件湖南网站seo找行者seo
  • 建网站 xyz成都网站优化
  • 设计logo网站免费无水印站长工具传媒
  • wordpress 用户相册seo排名优化软件有用吗
  • wordpress 微博文章网站如何做关键词优化
  • 淘宝网网站开发部技术部手机版怎么用百度快照
  • 网站设计中的用户体验宁波seo推广优化怎么做
  • seo如何根据网站数据做报表seo广告
  • 我的家乡网站建设seo教程培训班
  • 做名片去哪个网站网络营销策略案例分析
  • 阿里网站怎样做seo搜索关键词排行榜
  • 桂林网站开发公司关键词排名查询工具有哪些
  • www.ccb.com建设银行网站首页网站热度查询
  • 云主机可以做网站吗互联网登录的网站名