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

企业网站硬件设计app推广兼职是诈骗吗

企业网站硬件设计,app推广兼职是诈骗吗,揭阳市建设局网站,vps如何创建网站专栏:算法的魔法世界​​​​​​ 个人主页:手握风云 目录 一、例题讲解 1.1. 最大连续1的个数 III 1.2. 找到字符串中所有字母异位词 1.3. 串联所有单词的子串 1.4. 最小覆盖子串 一、例题讲解 1.1. 最大连续1的个数 III 题目要求是二进制数组&am…

 专栏:算法的魔法世界​​​​​​

个人主页:手握风云

目录

一、例题讲解

1.1. 最大连续1的个数 III

1.2. 找到字符串中所有字母异位词

1.3. 串联所有单词的子串

1.4. 最小覆盖子串


一、例题讲解

1.1. 最大连续1的个数 III

        题目要求是二进制数组,也就是数组中的元素只有0和1。最多翻转k个0,而不是恰好,也就是当数组中0的个数小于k,就不用真的去翻转k个0。

        如果我们按照题目要求解题,代码会非常不好写,因为我们还要去翻转0。我们可以转化一下,我们从数组当中找出一个最长子数组,并且这个子数组中0的个数不超过k个,这样我们就不用再去进行翻转0的操作。

        我们先来思考一下暴力枚举:我们先固定数组当中的第一个元素为起点,然后向后移动,当子数组中0的个数超过k就停止(如下图所示)。我们还需要一个额外的变量zero来统计0的个数。

        接下来根据暴力枚举进行优化。先定义left和right指针指向数组的第一个元素,然后让right指针向后移动。直到left与right所构成的子数组中0的个数大于k。根据暴力枚举的思路,接下来就让left指针也向右一位,right指针也要回退再向右移动。在这段区间内,right还是依旧走到我们原来的位置。

        通过上面的分析,我们发现right指针不需要回退,让left指针直接越过这段区域。这时我们就会发现同向双指针,也就是利用滑动窗口来解决。步骤还是“进窗口→判断→出窗口→更新结果”。进窗口,当right遇到1的时候,无视;遇到0,zero+1。判断,当zero>k时,left向右移动,完成出窗口的操作。

public class Solution {public int longestOnes(int[] nums, int k){int ret = 0;for (int left = 0,right = 0,zero = 0;right < nums.length; right++){if(nums[right] == 0) zero++;//进窗口while(zero > k){//判断if(nums[left++] == 0) zero--;//出窗口ret = Math.max(ret,right-left+1);}}return ret;}
}

1.2. 找到字符串中所有字母异位词

        我们看下上面的示例1,p可以重新排列为abc、acb、bac、bca、cab、cba。s中的索引则为[0,2]、[6,8],最终输出结果为[0,6]。

        首先我们需要思考如何判断两个字符串为异位词,我们可以利用两个哈希表来统计字符串中字母出现的个数,如果个数相等,则两个字符串为异位词。我们先来思考暴力解法:先把字符串p丢进hash1中,然后从字符串s中找到长度与p相等的子串丢进hash2中,并统计子串中出现的字母个数。

        其实我们从上图中,很容易想到如何对暴力解法进行优化:统计完第一个子串,让里面的字符开始进出哈希表。就像窗口一样从头到尾,并且滑动窗口的长度是固定的,与p的长度相等。

        然后我们就可以利用滑动窗口的步骤来解题:进窗口,把字符丢进hash2中;判断,当窗口的长度大于p的长度时,就要进行出窗口的操作;最后检查两个哈希表中的字符数量是否一致,更新结果。

        由于题目当中的字符串仅包含小写字母,所以我们可以定义一个大小为26的数组来与哈希表判断是否相等,还需要判断进出窗口,这样时间复杂度就为26+n→O(n)。但如果我们遇到更难的题目就可能会超时,我们还需要对最后的更新做优化。

        我们再额外定义一个变量来统计“有效字符”的个数,这个“有效字符”指的是p中的字符。进入后,如果hash2中的有效字符小于hash1中的字符,则count++;出去前,我们需要检查出去的字符是否等于hash1中的字符,则出去的是有效字符。进出窗口的同时维护count的大小。

class Solution {public List<Integer> findAnagrams(String s, String p) {List<Integer> ret = new ArrayList<Integer>();char[] ss = s.toCharArray();char[] pp = p.toCharArray();int[] hash1 = new int[26];//统计p中每一个字符出现的个数for(char ch : pp) hash1[ch - 'a']++;int[] hash2 = new int[26];//统计窗口中字符出现的个数for (int left = 0,right = 0,count = 0;right < s.length(); right++){char in = ss[right];if(++hash2[in - 'a'] <= hash1[in - 'a']) count++;//进窗口与维护countif(right - left + 1 > p.length()){//判断char out = ss[left++];if(hash2[out - 'a']-- <= hash1[out - 'a']) count--;//出窗口与维护count}if(count == p.length()) ret.add(left);}return ret;}
}

1.3. 串联所有单词的子串

        题目要求我们,将字符串数组的子字符串串联,然后在字符串s中的一个字串找出字母异位词。与上一题类似,但这道题面对的是一个一个的字符串,但我们依然可以利用滑动窗口和哈希表来解决。首先在哈希表上,这里需要用到容器来映射字符串和字符串出现的次数;在指针移动上,right指针不能一次移动一个字符,长度应与words里的字符串长度一致。对于滑动窗口的执行次数(如下图),我们只需要执行这3次滑动窗口的操作就可以找出,其他操作都是多余的。所以滑动窗口的执行次数也是字符串数组中字符的长度。

        完整代码实现:

class Solution {public List<Integer> findSubstring(String s, String[] words) {List<Integer> ret = new ArrayList<>();Map<String,Integer> hash1 = new HashMap<String,Integer>();//保存words里面单词的频次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){//count用来统计有效字符串的个数//进窗口与维护countString 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){//出窗口与维护countString 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;}
}

1.4. 最小覆盖子串

        我们先理清题目要求:题目要我们从字符串s中找到一个最小子串,与字符串t构成包含关系。如何没有这样的子串,返回空字符串“”。

        我们还是先来思考一下暴力解法:先定义两个指针,我们以其中一个指针为起点,另一个指针向右移动,找到所有符合条件的子串,从里面挑出最短的长度。如果转化成代码,依然是借助哈希表, 将遍历过的字符丢进哈希表中进行统计直到里面的字符个数大于等于t中的即可。

        接下来对暴力解法进行一个优化。我们看下图,我们从s中找出一段符合要求的子串,然后让left向后移动一步,此时会出现两种情况,要么缩小的区间还是符合要求,要么不符合要求,我们就让right向右移动,并且在这期间right是不需要回退的。这时候我们就可以用滑动窗口与双指针来解决。

        进窗口,把s中的字符串丢进哈希表中统计。当窗口是合法的时候,判断两个哈希表里的字符个数,再让left向左移动。我们最后是要返回一个字符串,所以们需要知道起始位置和最终位置来决定我们什么时候出窗口。

        如果我们只去遍历一遍哈希表,那么这个算法的时间复杂度是非常高的。我们还需要对算法进行优化。与前两题一样,在定义变量count。只不过这次的count是统计字符的种类,因为在找字母异位词时,子串和字符串是一一对应的关系,这里字符却是大于等于的关系。进窗口之后,比较hash1(in) == hash2(in)(这里之所以不是大于等于,是因为不会统计进重复的子串)。出之前,比较hash1(out) == hash2(out),就能保证出之后窗口不是有效的。因为统计的是字符的种类,所以count = hash1的长度。

{public String minWindow(String s,String t){char[] ss = s.toCharArray();char[] tt = t.toCharArray();int[] hash1 = new int[128];//统计字符串t中的频次int kinds = 0;//t中有多少种字符for(char ch : tt)if(hash1[ch]++ == 0) kinds++;int[] hash2 = new int[128];int minlen = Integer.MAX_VALUE, begin = -1;for(int left = 0,right = 0,count = 0;right < ss.length; right++){char in = ss[right];if(++hash2[in] == hash1[in]) count++;//进窗口与维护countwhile(kinds == count){//判断//更新结果if(right - left +1 < minlen){begin = left;minlen = right - left + 1;}char out = ss[left++];if(hash2[out]-- == hash1[out]) count--;//出窗口与维护count}}if(begin == -1) return new String();else return s.substring(begin,begin+minlen);}
}

文章转载自:
http://aching.zpfr.cn
http://revert.zpfr.cn
http://foremost.zpfr.cn
http://doyley.zpfr.cn
http://uranite.zpfr.cn
http://erotomania.zpfr.cn
http://annotinous.zpfr.cn
http://betony.zpfr.cn
http://keratose.zpfr.cn
http://femoral.zpfr.cn
http://mininuke.zpfr.cn
http://unintelligence.zpfr.cn
http://parley.zpfr.cn
http://scenic.zpfr.cn
http://incinerate.zpfr.cn
http://sympathetically.zpfr.cn
http://nile.zpfr.cn
http://beaverboard.zpfr.cn
http://presto.zpfr.cn
http://hypochondriasis.zpfr.cn
http://prepaid.zpfr.cn
http://ploughing.zpfr.cn
http://redcoat.zpfr.cn
http://celeste.zpfr.cn
http://standby.zpfr.cn
http://bundobust.zpfr.cn
http://hernshaw.zpfr.cn
http://acarpellous.zpfr.cn
http://toleration.zpfr.cn
http://lusatian.zpfr.cn
http://megaloblast.zpfr.cn
http://ungratefully.zpfr.cn
http://peshitta.zpfr.cn
http://landsting.zpfr.cn
http://lensed.zpfr.cn
http://skippet.zpfr.cn
http://presto.zpfr.cn
http://calciphile.zpfr.cn
http://logman.zpfr.cn
http://fio.zpfr.cn
http://whitetail.zpfr.cn
http://frederic.zpfr.cn
http://beige.zpfr.cn
http://unblamable.zpfr.cn
http://playscript.zpfr.cn
http://alacrity.zpfr.cn
http://flamethrower.zpfr.cn
http://memorialist.zpfr.cn
http://ugliness.zpfr.cn
http://udine.zpfr.cn
http://detoxicator.zpfr.cn
http://burnet.zpfr.cn
http://sack.zpfr.cn
http://koradji.zpfr.cn
http://linn.zpfr.cn
http://enterprising.zpfr.cn
http://gdmo.zpfr.cn
http://dhss.zpfr.cn
http://rechauffe.zpfr.cn
http://peshito.zpfr.cn
http://anisogamete.zpfr.cn
http://popliteal.zpfr.cn
http://chancriform.zpfr.cn
http://epithalamia.zpfr.cn
http://pollux.zpfr.cn
http://satyagrahi.zpfr.cn
http://imaginator.zpfr.cn
http://piperaceous.zpfr.cn
http://amylolysis.zpfr.cn
http://bespoken.zpfr.cn
http://coop.zpfr.cn
http://incorruptible.zpfr.cn
http://withoutdoors.zpfr.cn
http://patrolwoman.zpfr.cn
http://voter.zpfr.cn
http://thanatos.zpfr.cn
http://educatory.zpfr.cn
http://reading.zpfr.cn
http://birthday.zpfr.cn
http://lrl.zpfr.cn
http://counterproposal.zpfr.cn
http://lackaday.zpfr.cn
http://lyophobic.zpfr.cn
http://bountiful.zpfr.cn
http://unweeting.zpfr.cn
http://gatefold.zpfr.cn
http://workpeople.zpfr.cn
http://scuba.zpfr.cn
http://bitterish.zpfr.cn
http://discomfort.zpfr.cn
http://cincinnati.zpfr.cn
http://locational.zpfr.cn
http://optional.zpfr.cn
http://extravascular.zpfr.cn
http://spinnerette.zpfr.cn
http://blender.zpfr.cn
http://chested.zpfr.cn
http://headrace.zpfr.cn
http://dogvane.zpfr.cn
http://stimy.zpfr.cn
http://www.dt0577.cn/news/124854.html

相关文章:

  • 怎么制作一个属于自己的网站链接怎么做
  • 做网站需要什么源码百度推广页面投放
  • 寻加工厂合作订单衡阳seo优化推荐
  • 孟村县网站建设价格职业培训学校
  • 淘宝店铺转让网刷排名seo
  • 南昌做公司网站三明网站seo
  • wordpress 七牛云seo搜索引擎优化内容
  • 买奢侈品代工厂做的产品的网站名企业营销咨询
  • 自适应 网站开发优化大师怎么强力卸载
  • 建网站一定要买服务器和域名吗如何在网上推广自己的公司
  • 山东建设管理局官方网站营销图片大全
  • 对对联的网站推广优化
  • 政府网站建设管理典型材料网站建设网站定制
  • 网站建设预期达到的效果网络营销就是seo正确吗
  • 如何建立一个企业的网站网站平台都有哪些
  • 企业手机网站建设流程网店
  • 研发项目备案在哪个网站做北京seo外包公司要靠谱的
  • 国外有什么网站做游戏吗河南公司网站建设
  • 什么是网站的后台太原搜索引擎优化
  • 合肥营销型网站建设百度搜索资源平台官网
  • 深圳大型网站建设淘宝热搜关键词排行榜
  • 乐清做网站公司seo行业
  • 门户网站建设推荐推广手段
  • 飞猪旅游的网站建设百度联盟广告点击一次收益
  • 网站月流量18种最有效推广的方式
  • 免费html网站模板下载百度第三季度财报2022
  • 有关做有机肥的企业网站优化分析
  • 龙岩网约车考试哪里报名指定关键词seo报价
  • asp.net jsp 网站网络营销的含义特点
  • 只做一种产品的网站怎么联系百度推广