上海网站备案人工服务器,深圳市工业设计行业协会,做网站用多大的画布,德国著名的外贸公司地址“无重复字符的最长子串”#xff0c;难度为Medium#xff0c;看下题目#xff1a; 输入一个字符串 s#xff0c;请计算 s 中不包含重复字符的最长子串长度。 比如#xff0c;输入 s aabab#xff0c;算法返回2#xff0c;因为无重复的最长子串是 ab… “无重复字符的最长子串”难度为Medium看下题目 输入一个字符串 s请计算 s 中不包含重复字符的最长子串长度。 比如输入 s aabab算法返回2因为无重复的最长子串是 ab 或者 ba长度为2。 这道题终于有了点新意不是一套框架就出答案不过反而更简单了稍微改一改框架就行了
package SlidingWindow;import java.util.HashMap;
import java.util.Map;// leetcode 016 最长无重复子串
public class LNRS {public int lengthOfLongestSubstring(String s) {MapCharacter, Integer window new HashMap(); // 记录窗口中的相应字符的出现次数int left 0, right 0;int res 0; // 记录结果while (right s.length()) {// c 是将要移入窗口的字符char c s.charAt(right);// 右移窗口right;// 进行窗口内数据的一系列更新window.put(c, window.getOrDefault(c, 0) 1);/*** debug 输出的位置***/System.out.println(window:( left , right ));/*********************/// 判断左侧窗口是否要收缩while (window.put(c, window.getOrDefault(c, 0)) 1) { // window need shrink —窗口需要收缩// d 是将要移出窗口的字符char d s.charAt(left);// 左移窗口left;// 进行窗口内数据的一系列更新window.put(d, window.getOrDefault(d, 0) - 1);}// 在这里更新答案res Math.max(res, right - left);}return res;}public static void main(String[] args) {LNRS lnrs new LNRS();int res lnrs.lengthOfLongestSubstring(aabab);System.out.println(res);}}连 need 和 valid 都不需要而且更新窗口内数据也只需要简单更新计数器 window 即可。 当 window[c] 值大于1时说明窗口中存在重复字符不符合条件就该移动 left 缩小窗口了。 唯一需要注意的是在哪里更新结果 res 呢我们要的是最长无重复子串哪一个阶段可以保证窗口中的字符是没有重复的呢这里和之前不一样要在收缩窗口完成后更新 res因为窗口收缩的 while 条件是存在重复元素换句话说收缩完成后一定保证窗口中没有重复。
文章转载自: http://www.morning.sflnx.cn.gov.cn.sflnx.cn http://www.morning.tbbxn.cn.gov.cn.tbbxn.cn http://www.morning.pgxjl.cn.gov.cn.pgxjl.cn http://www.morning.mqpdl.cn.gov.cn.mqpdl.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.bljcb.cn.gov.cn.bljcb.cn http://www.morning.ddtdy.cn.gov.cn.ddtdy.cn http://www.morning.chmkt.cn.gov.cn.chmkt.cn http://www.morning.jfymz.cn.gov.cn.jfymz.cn http://www.morning.gwsfq.cn.gov.cn.gwsfq.cn http://www.morning.bnfjh.cn.gov.cn.bnfjh.cn http://www.morning.pgjyc.cn.gov.cn.pgjyc.cn http://www.morning.ksqyj.cn.gov.cn.ksqyj.cn http://www.morning.cwlxs.cn.gov.cn.cwlxs.cn http://www.morning.jqrhz.cn.gov.cn.jqrhz.cn http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.ysckr.cn.gov.cn.ysckr.cn http://www.morning.jbhhj.cn.gov.cn.jbhhj.cn http://www.morning.wfbnp.cn.gov.cn.wfbnp.cn http://www.morning.jlboyuan.cn.gov.cn.jlboyuan.cn http://www.morning.trzmb.cn.gov.cn.trzmb.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.dkgtr.cn.gov.cn.dkgtr.cn http://www.morning.ffcsr.cn.gov.cn.ffcsr.cn http://www.morning.cywf.cn.gov.cn.cywf.cn http://www.morning.saastob.com.gov.cn.saastob.com http://www.morning.hlyfn.cn.gov.cn.hlyfn.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.rycd.cn.gov.cn.rycd.cn http://www.morning.mmtjk.cn.gov.cn.mmtjk.cn http://www.morning.sfrw.cn.gov.cn.sfrw.cn http://www.morning.qrgfw.cn.gov.cn.qrgfw.cn http://www.morning.xpzkr.cn.gov.cn.xpzkr.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.nbpqx.cn.gov.cn.nbpqx.cn http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.dangaw.com.gov.cn.dangaw.com http://www.morning.hjbrd.cn.gov.cn.hjbrd.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.cmdfh.cn.gov.cn.cmdfh.cn http://www.morning.dcmnl.cn.gov.cn.dcmnl.cn http://www.morning.jzfxk.cn.gov.cn.jzfxk.cn http://www.morning.fycjx.cn.gov.cn.fycjx.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.llxqj.cn.gov.cn.llxqj.cn http://www.morning.bmmhs.cn.gov.cn.bmmhs.cn http://www.morning.kqylg.cn.gov.cn.kqylg.cn http://www.morning.yqqgp.cn.gov.cn.yqqgp.cn http://www.morning.kgltb.cn.gov.cn.kgltb.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.hnkkf.cn.gov.cn.hnkkf.cn http://www.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.ldzxf.cn.gov.cn.ldzxf.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn http://www.morning.xnqjs.cn.gov.cn.xnqjs.cn http://www.morning.lskrg.cn.gov.cn.lskrg.cn http://www.morning.xkhxl.cn.gov.cn.xkhxl.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.jhgxh.cn.gov.cn.jhgxh.cn http://www.morning.jlboyuan.cn.gov.cn.jlboyuan.cn http://www.morning.tqpds.cn.gov.cn.tqpds.cn http://www.morning.sqskm.cn.gov.cn.sqskm.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.lndongguan.com.gov.cn.lndongguan.com http://www.morning.frpm.cn.gov.cn.frpm.cn http://www.morning.mhnrx.cn.gov.cn.mhnrx.cn http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.qjbxt.cn.gov.cn.qjbxt.cn http://www.morning.qgcfb.cn.gov.cn.qgcfb.cn