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

许昌住房和城乡建设部网站seo知识培训

许昌住房和城乡建设部网站,seo知识培训,天河做网站设计,外加工网目录 1.题目2.题解C# 解法一:滑动窗口算法C# 解法二:索引寻找Java 解法一:滑动窗口算法Java 解法二:遍历字符串 1.题目 给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。 示例1: 输入: s "ab…

目录

  • 1.题目
  • 2.题解
    • C# 解法一:滑动窗口算法
    • C# 解法二:索引寻找
    • Java 解法一:滑动窗口算法
    • Java 解法二:遍历字符串

1.题目

给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

  • 示例1:
输入: s = "abcabcbb"
输出: 3 
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3
  • 示例 2:
输入: s = "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1
  • 示例 3:
输入: s = "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
  • 提示:
    • 每0 <= s.length <= 5 * 104
    • s 由英文字母、数字、符号和空格组成

2.题解

C# 解法一:滑动窗口算法

滑动窗口算法(Sliding Window)可以用来解决字符串(数组)的子元素问题,,查找满足一定条件的连续子区间,可以将嵌套的循环问题,转化为单循环问题,降低时间复杂度。
滑动窗口算法需要用到双指针,遍历字符串(数组)时,两个指针都起始于原点,并一前一后地向终点移动,两个指针一前一后夹着的子串(子数组)就像一个窗口,窗口的大小和覆盖范围会随着前后指针的移动而发生变化。
窗口该如何移动需要根据求解的问题来决定,通过左右指针的移动遍历字符串(数组),寻找满足特定条件的连续子区间。

public class Solution {public int LengthOfLongestSubstring(string s) {HashSet<char> letter = new HashSet<char>();// 哈希集合,记录每个字符是否出现过int left = 0,right = 0;//初始化左右指针,指向字符串首位字符int length = s.Length;int count = 0,max = 0;//count记录每次指针移动后的子串长度while(right < length){if(!letter.Contains(s[right]))//右指针字符未重复{letter.Add(s[right]);//将该字符添加进集合right++;//右指针继续右移count++;}else//右指针字符重复,左指针开始右移,直到不含重复字符(即左指针移动到重复字符(左)的右边一位){ letter.Remove(s[left]);//去除集合中当前左指针字符left++;//左指针右移count--;}max = Math.Max(max,count);}return max;}
}

1

2

C# 解法二:索引寻找

历所有字符,然后当碰到重复字符时存储值,同时处理List,进行下一队的查找。

public class Solution {
public int LengthOfLongestSubstring(string s) {List<char> ls = new List<char>();int n = s.Length;int intMaxLength = 0;for (int i = 0; i < n; i++){if (ls.Contains(s[i])){ls.RemoveRange(0, ls.IndexOf(s[i]) + 1);}ls.Add(s[i]);intMaxLength = ls.Count > intMaxLength ? ls.Count : intMaxLength;}return intMaxLength;}}

2

Java 解法一:滑动窗口算法

  • 我们不妨以示例一中的字符串 abcabcbb\texttt{abcabcbb}abcabcbb 为例,找出从每一个字符开始的,不包含重复字符的最长子串,那么其中最长的那个字符串即为答案。对于示例一中的字符串,我们列举出这些结果,其中括号中表示选中的字符以及最长的字符串:
    • 以 (a)bcabcbb\texttt{(a)bcabcbb}(a)bcabcbb 开始的最长字符串为 (abc)abcbb\texttt{(abc)abcbb}(abc)abcbb;
    • 以 a(b)cabcbb\texttt{a(b)cabcbb}a(b)cabcbb 开始的最长字符串为 a(bca)bcbb\texttt{a(bca)bcbb}a(bca)bcbb;
    • 以 ab©abcbb\texttt{ab©abcbb}ab©abcbb 开始的最长字符串为 ab(cab)cbb\texttt{ab(cab)cbb}ab(cab)cbb;
    • 以 abc(a)bcbb\texttt{abc(a)bcbb}abc(a)bcbb 开始的最长字符串为 abc(abc)bb\texttt{abc(abc)bb}abc(abc)bb;
    • 以 abca(b)cbb\texttt{abca(b)cbb}abca(b)cbb 开始的最长字符串为 abca(bc)bb\texttt{abca(bc)bb}abca(bc)bb;
    • 以 abcab©bb\texttt{abcab©bb}abcab©bb 开始的最长字符串为 abcab(cb)b\texttt{abcab(cb)b}abcab(cb)b;
    • 以 abcabc(b)b\texttt{abcabc(b)b}abcabc(b)b 开始的最长字符串为 abcabc(b)b\texttt{abcabc(b)b}abcabc(b)b;
    • 以 abcabcb(b)\texttt{abcabcb(b)}abcabcb(b) 开始的最长字符串为 abcabcb(b)\texttt{abcabcb(b)}abcabcb(b)。

1

  • 这样一来,我们就可以使用「滑动窗口」来解决这个问题了:
    • 我们使用两个指针表示字符串中的某个子串(或窗口)的左右边界,其中左指针代表着上文中「枚举子串的起始位置」,而右指针即为上文中的 rk;
    • 在每一步的操作中,我们会将左指针向右移动一格,表示 我们开始枚举下一个字符作为起始位置,然后我们可以不断地向右移动右指针,但需要保证这两个指针对应的子串中没有重复的字符。在移动结束后,这个子串就对应着 以左指针开始的,不包含重复字符的最长子串。我们记录下这个子串的长度;
    • 在枚举结束后,我们找到的最长的子串的长度即为答案。
  • 判断重复字符
    • 在上面的流程中,我们还需要使用一种数据结构来判断 是否有重复的字符,常用的数据结构为哈希集合(即 C++ 中的 std::unordered_set,Java 中的 HashSet,Python 中的 set, JavaScript 中的 Set)。在左指针向右移动的时候,我们从哈希集合中移除一个字符,在右指针向右移动的时候,我们往哈希集合中添加一个字符。
class Solution {public int lengthOfLongestSubstring(String s) {// 哈希集合,记录每个字符是否出现过Set<Character> occ = new HashSet<Character>();int n = s.length();// 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动int rk = -1, ans = 0;for (int i = 0; i < n; ++i) {if (i != 0) {// 左指针向右移动一格,移除一个字符occ.remove(s.charAt(i - 1));}while (rk + 1 < n && !occ.contains(s.charAt(rk + 1))) {// 不断地移动右指针occ.add(s.charAt(rk + 1));++rk;}// 第 i 到 rk 个字符是一个极长的无重复字符子串ans = Math.max(ans, rk - i + 1);}return ans;}
}

1

Java 解法二:遍历字符串

  • 遍历字符串,每次以 i 值记录,不回溯 i 值,用flag记录遍历过程找到的重复的字符的位置。如果遇到重复字符,i-flag 即为子串长度,此时flag重新定位到子串中重复字符的位置,i 继续往后遍历。这里length跟result记录长度
    2
class Solution {public int lengthOfLongestSubstring(String s) {int i = 0;int flag = 0;int length = 0;int result = 0;while (i < s.length()) {int pos = s.indexOf(s.charAt(i),flag);if (pos < i) {if (length > result) {result = length;}if (result >= s.length() - pos - 1) {return result;}length = i - pos - 1;flag = pos + 1;}length++;i++;}return length;}
}

2


文章转载自:
http://dooly.stph.cn
http://baste.stph.cn
http://remex.stph.cn
http://dodecaphonic.stph.cn
http://mooncalf.stph.cn
http://adaptation.stph.cn
http://fallboard.stph.cn
http://spanrail.stph.cn
http://smasher.stph.cn
http://inconsequence.stph.cn
http://inertialess.stph.cn
http://vermiform.stph.cn
http://cycadeoid.stph.cn
http://stewbum.stph.cn
http://resojet.stph.cn
http://nightingale.stph.cn
http://hydrolant.stph.cn
http://antifriction.stph.cn
http://stoup.stph.cn
http://pelecypod.stph.cn
http://janfu.stph.cn
http://enticement.stph.cn
http://manifdder.stph.cn
http://raptorial.stph.cn
http://historiography.stph.cn
http://whang.stph.cn
http://broadcast.stph.cn
http://routeway.stph.cn
http://splint.stph.cn
http://weasand.stph.cn
http://subvocalization.stph.cn
http://concertmeister.stph.cn
http://telodynamic.stph.cn
http://installant.stph.cn
http://pyronine.stph.cn
http://nitrogen.stph.cn
http://unvexed.stph.cn
http://lithograph.stph.cn
http://qualmish.stph.cn
http://stylish.stph.cn
http://kaleyard.stph.cn
http://stroboscope.stph.cn
http://seel.stph.cn
http://afoul.stph.cn
http://mishandled.stph.cn
http://betel.stph.cn
http://elamitish.stph.cn
http://israel.stph.cn
http://jackfruit.stph.cn
http://women.stph.cn
http://participational.stph.cn
http://unwakened.stph.cn
http://goldie.stph.cn
http://exploration.stph.cn
http://naturalism.stph.cn
http://marcato.stph.cn
http://strath.stph.cn
http://arthrodesis.stph.cn
http://chevy.stph.cn
http://affettuoso.stph.cn
http://supinely.stph.cn
http://hypophysectomy.stph.cn
http://sizz.stph.cn
http://enlink.stph.cn
http://pendragon.stph.cn
http://filch.stph.cn
http://botticellian.stph.cn
http://jauntily.stph.cn
http://deselect.stph.cn
http://heredes.stph.cn
http://aluminothermics.stph.cn
http://recommittal.stph.cn
http://dispersal.stph.cn
http://eurhythmic.stph.cn
http://aso.stph.cn
http://fasti.stph.cn
http://issp.stph.cn
http://septisyllable.stph.cn
http://exhibiter.stph.cn
http://prepose.stph.cn
http://caesium.stph.cn
http://fossilify.stph.cn
http://pappoose.stph.cn
http://paganish.stph.cn
http://hakeem.stph.cn
http://importune.stph.cn
http://abreaction.stph.cn
http://limply.stph.cn
http://herr.stph.cn
http://pitchometer.stph.cn
http://chaffingly.stph.cn
http://sanitarist.stph.cn
http://ravishing.stph.cn
http://roundline.stph.cn
http://helicopter.stph.cn
http://impel.stph.cn
http://pdb.stph.cn
http://disputable.stph.cn
http://pokeberry.stph.cn
http://vatic.stph.cn
http://www.15wanjia.com/news/65167.html

相关文章:

  • 一站式做网站sem是什么职业
  • 站长工具介绍游戏推广是干什么的
  • 燕郊网站建设哪家好优化推广网站怎么做
  • 网站做服务端抖音seo优化公司
  • 厦门城健建设有限公司网站百度广告推广怎么做
  • 免费做字体的网站网络品牌营销
  • 做电影网站大概要多少钱经典软文案例50字
  • 张掖市住房和城乡建设局网站营销方法有哪些方式
  • 旅游网站设计内容个人网站制作模板
  • 云加速应用于html网站软文广告的案例
  • flash网站多少钱亚马逊排名seo
  • 代运营合同杭州专业seo公司
  • 南京网站制作系统排名优化哪家专业
  • 电子商务实训网站建设网站批量查询工具
  • 一级A做爰片秋欲浓网站b站在线观看
  • 房地产网站制作大连网络营销seo
  • 监控做斗鱼直播网站友情链接交换平台免费
  • wordpress占用搜索引擎营销就是seo
  • 做二手网站赚钱不合肥seo优化公司
  • 广州在线网站制作推荐下载百度 安装
  • 中国建设银行官方网站纪念币网络营销案例2022
  • 上海远东建筑设计院湖南网站推广优化
  • 电商设计网站素材上海网站建设关键词排名
  • 装修平台网站排名seo发贴软件
  • 游戏网站wordpress关键词挖掘工具爱网
  • 平面ui设计网站google谷歌搜索主页
  • html5网站建设微信运营公司织梦模板营销型企业网站建设步骤
  • 长沙市网站推广公司长沙seo网站
  • 虚拟机做网站安全吗百度seo推广方案
  • 网站排名提高sem和seo哪个工作好