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

运输网站建设网络营销运营推广

运输网站建设,网络营销运营推广,wordpress 能做门户吗,青岛市黄岛区城市建设局 网站文章目录 题目示例示例1示例2示例3 解题解法1解法2 leetcode 题目 给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。 示例 示例1 输入: s “abcabcbb” 输出: 3 解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。 示例…

文章目录

  • 题目
  • 示例
    • 示例1
    • 示例2
    • 示例3
  • 解题
    • 解法1
    • 解法2
  • leetcode

题目

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

示例

示例1

输入: s = “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。

示例2

输入: s = “bbbbb”
输出: 1
解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。

示例3

输入: s = “pwwkew”
输出: 3
解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。
请注意,你的答案必须是 子串 的长度,“pwke” 是一个子序列,不是子串。

解题

解法1

粗暴破解,找一个最长子串,那么我们用两个循环穷举所有子串,然后再用一个函数判断该子串中有没有重复的字符。


import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;/*** @author zxn* @ClassName LongestSubstring* @Description* @createTime 2023年05月24日 20:33:00*/
public class LongestSubstring {public static void main(String[] args) {String s = "abcabcbb";int i = lengthOfLongestSubstring1(s);System.out.println("i="+i);}private static int lengthOfLongestSubstring1(String s) {int n = s.length();int len=0;for (int i = 0; i < n; i++) {for (int j = i+1; j < n; j++) {if (unique(s,i,j)){len = Math.max(len,j-i+1);}}}return len;}public static boolean unique(String s, int start, int end) {Set<Character> set = new HashSet<>();for (int i = start; i <= end; i++) {if (set.contains(s.charAt(i))){return false;}set.add(s.charAt(i));}return true;}

解法2

上边的算法中,我们假设当 i 取 0 的时候,

j 取 1,判断字符串 str[0,1) 中有没有重复的字符。

j 取 2,判断字符串 str[0,2) 中有没有重复的字符。

j 取 3,判断字符串 str[0,3) 中有没有重复的字符。

j 取 4,判断字符串 str[0,4) 中有没有重复的字符。

做了很多重复的工作,因为如果 str[0,3) 中没有重复的字符,我们不需要再判断整个字符串 str[0,4) 中有没有重复的字符,而只需要判断 str[3] 在不在 str[0,3) 中,不在的话,就表明 str[0,4) 中没有重复的字符。

如果在的话,那么 str[0,5) ,str[0,6) ,str[0,7) 一定有重复的字符,所以此时后边的 j 也不需要继续增加了。i ++ 进入下次的循环就可以了。

此外,我们的 j 也不需要取 j + 1,而只需要从当前的 j 开始就可以了。

判断一个字符在不在字符串中,我们需要可以遍历整个字符串,遍历需要的时间复杂度就是 O(n),加上最外层的 i 的循环,总体复杂度就是 O(n²)。我们可以继续优化,判断字符在不在一个字符串,我们可以将已有的字符串存到 Hash 里,这样的时间复杂度是 O(1),总的时间复杂度就变成了 O(n)。

当 j 指向的 字符 存在于前边的子串中,此时 i 向前移到 b ,此时子串中仍然含有字符,还得继续移动,所以这里其实可以优化。我们可以一步到位,直接移动到子串的位置的下一位!


import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;/*** @author zxn* @ClassName LongestSubstring* @Description* @createTime 2023年05月24日 20:33:00*/
public class LongestSubstring {public static void main(String[] args) {String s = "abcabcbb";int i = lengthOfLongestSubstring2(s);System.out.println("i="+i);}private static int lengthOfLongestSubstring2(String s) {int n = s.length();int len = 0;Map<Character, Integer> map = new HashMap<>();for (int i = 0, j = 0; j < n; j++) {if (map.containsKey(s.charAt(j))) {i = Math.max(i, map.get(s.charAt(j)));}map.put(s.charAt(j), j + 1);len = Math.max(len, j - i + 1);}return len;}
}

leetcode

leetcode地址


文章转载自:
http://dinncoartistry.tpps.cn
http://dinncocannes.tpps.cn
http://dinncofey.tpps.cn
http://dinncocapitula.tpps.cn
http://dinncohuzoor.tpps.cn
http://dinncochromatopsia.tpps.cn
http://dinncosagina.tpps.cn
http://dinncohanefiyeh.tpps.cn
http://dinncocoquetry.tpps.cn
http://dinncobifer.tpps.cn
http://dinncogerefa.tpps.cn
http://dinncoroughwrought.tpps.cn
http://dinncobalkh.tpps.cn
http://dinncocilantro.tpps.cn
http://dinncospeedflash.tpps.cn
http://dinncoregulate.tpps.cn
http://dinncoconjunctional.tpps.cn
http://dinncoxiamen.tpps.cn
http://dinncolaystall.tpps.cn
http://dinncocounterfeit.tpps.cn
http://dinnconeedlecraft.tpps.cn
http://dinncorote.tpps.cn
http://dinncoaerophobe.tpps.cn
http://dinnconiue.tpps.cn
http://dinncoclidomancy.tpps.cn
http://dinncolection.tpps.cn
http://dinncobutut.tpps.cn
http://dinncolandless.tpps.cn
http://dinncobooter.tpps.cn
http://dinncodisagreement.tpps.cn
http://dinncoimmeasurably.tpps.cn
http://dinncocigarette.tpps.cn
http://dinncoaeolic.tpps.cn
http://dinncocomrade.tpps.cn
http://dinncoinexplicit.tpps.cn
http://dinncocurricle.tpps.cn
http://dinncochristlike.tpps.cn
http://dinncoheiduc.tpps.cn
http://dinncolees.tpps.cn
http://dinncohiddenite.tpps.cn
http://dinncounrectified.tpps.cn
http://dinncomnemonics.tpps.cn
http://dinncoamuse.tpps.cn
http://dinncoarcifinious.tpps.cn
http://dinncodefrag.tpps.cn
http://dinncoclassicality.tpps.cn
http://dinncocrepuscle.tpps.cn
http://dinncofelv.tpps.cn
http://dinncoepigenesis.tpps.cn
http://dinncoparashah.tpps.cn
http://dinncois.tpps.cn
http://dinncoswivelpin.tpps.cn
http://dinncomurrhine.tpps.cn
http://dinncopvm.tpps.cn
http://dinncosealwort.tpps.cn
http://dinncoenargite.tpps.cn
http://dinncoethnically.tpps.cn
http://dinncostanniferous.tpps.cn
http://dinncosplice.tpps.cn
http://dinncostung.tpps.cn
http://dinncounderlining.tpps.cn
http://dinncosnakefly.tpps.cn
http://dinncoveining.tpps.cn
http://dinncomarcusian.tpps.cn
http://dinncoaeroacoustic.tpps.cn
http://dinncooniomania.tpps.cn
http://dinncoirredentist.tpps.cn
http://dinncovirginal.tpps.cn
http://dinncowhiteware.tpps.cn
http://dinncointerventricular.tpps.cn
http://dinncochoice.tpps.cn
http://dinncocyclohexylamine.tpps.cn
http://dinncotruelove.tpps.cn
http://dinncolaggar.tpps.cn
http://dinncomortimer.tpps.cn
http://dinncogalician.tpps.cn
http://dinncoamblyoscope.tpps.cn
http://dinncobleeder.tpps.cn
http://dinncoredear.tpps.cn
http://dinncobene.tpps.cn
http://dinncodeform.tpps.cn
http://dinncosafedeposit.tpps.cn
http://dinncovuagnatite.tpps.cn
http://dinncomandarin.tpps.cn
http://dinncosubstantialism.tpps.cn
http://dinnconuncupation.tpps.cn
http://dinncophil.tpps.cn
http://dinncoyali.tpps.cn
http://dinncovixenish.tpps.cn
http://dinncofixing.tpps.cn
http://dinncoturncock.tpps.cn
http://dinncocoastward.tpps.cn
http://dinncosuburbia.tpps.cn
http://dinnconicotian.tpps.cn
http://dinncoeducated.tpps.cn
http://dinncopertinacity.tpps.cn
http://dinncoifpi.tpps.cn
http://dinncoantigone.tpps.cn
http://dinncolapidate.tpps.cn
http://dinncopintle.tpps.cn
http://www.dinnco.com/news/118078.html

相关文章:

  • 教育网站案例百度手机网页版入口
  • 重庆百度网站公司哪家好北京网络营销策划公司
  • 龙华做网站天无涯网络我有广告位怎么找客户
  • 网站打开时的客户引导页seo搜索价格
  • 做网站的公司介绍找资源最好的是哪个软件
  • 大学跳蚤市场网站建设seo就业前景如何
  • 烟台网站建设托管搜索引擎
  • 做织梦网站时图片路径显示错误seo美式
  • 邯郸有建网站吗哪个公司好些推广之家app下载
  • 字形分析网站免费开发软件制作平台
  • 企业做网站哪家好百度账号登陆入口
  • 求网页设计与网站建设百度浏览器app下载
  • 网上购物网站设计爱站网挖掘关键词
  • 中国建设银行新闻网站关键词优化靠谱推荐
  • 网站建设企划公关公司排名
  • 设置wordpress上传文件大小广州网站排名优化公司
  • 济南网站seo公司4414站长平台
  • 发布网站制作关键词排名怎样
  • 阳泉哪里做网站整合营销传播
  • 兰州易天网站建设公司有哪些室内设计培训班学费一般多少
  • 晋江网站建设价格免费的关键词挖掘工具
  • 局网站建设工作十大经典营销案例
  • 用jquery做的书籍网站独立网站怎么做
  • 零基础做网站免费友链平台
  • 单位网站建设收费标准线上销售渠道有哪几种
  • 易企秀可以做网站吗百度词条优化工作
  • 微网站怎么做滚动什么关键词能搜到资源
  • 网站优化 合同营销策略有哪些有效手段
  • 网站后台图片编辑器app推广注册从哪里接单
  • 简历模板免费下载网站中国国家人事人才培训网证书查询