Given a string, find the length of the longest substring without repeating characters

来源:互联网 发布:青年网络公开课有几集 编辑:程序博客网 时间:2024/04/29 17:28

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given “abcabcbb”, the answer is “abc”, which the length is 3.

Given “bbbbb”, the answer is “b”, with the length of 1.

Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

/**     * 每次出现重复的字符的时候,计算两个字符之间的长度,和上一次保存的长度取最大值     * @param s     * @return     */    public static int lengthOfLongestSubstring(String s) {        if (s.length()==0) return 0;        int max=0;        HashMap<Character, Integer> map = new HashMap<Character, Integer>();        for (int i=0, j=0; i<s.length(); ++i){            if (map.containsKey(s.charAt(i))){                j = Math.max(j,map.get(s.charAt(i))+1);            }            map.put(s.charAt(i),i);//每次更新了新出现从字符的位置            max = Math.max(max,i-j+1);        }        return max;    }
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 我叫mt 大姐大 我叫mt 口袋巴士 我叫mt光棍节大礼包 我叫mt光棍节礼包 我叫mt情人节礼包 我就爱淘 我就爱战歌网 我靠谱你随意 我靠谱你随意txt 我靠谱你随意txt下载 我可能不会爱你花絮 我们都爱笑tfboys 我们都是坏孩子片尾曲 我们结婚了121215 我们结婚了130105 我们结婚了130112 我们结婚了20110716 我们结婚了20120414 我们结婚了20120728 我们结婚了20121229 我们结婚了20130503 我们结婚了20130614 我们结婚了20130615 我们结婚了20130907 我们结婚了20130928 我们结婚了20131012 我们相遇时星光灿烂 我们小区艺体能140107 我们约会吧 萧敬腾 我们约会吧百度影音 我们约会吧陈娟 我们约会吧戴清 我们约会吧黄剑 我们约会吧明月 我们约会吧女嘉宾出场音乐 我们约会吧王紫藤 我们约会吧阴魁 我们约会吧最新一期百度影音 我是大魔鬼txt 我是黑道女生 我是马尔福夫人