LeetCode Longest Substring Without Repeating Characters

来源:互联网 发布:windows bat脚本 示例 编辑:程序博客网 时间:2024/05/17 07:47

链接: https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/


字符串中最长连续不重复子序列,用JAVA中的哈希表比较方便

public class Solution {    public int lengthOfLongestSubstring(String s) {int n=s.length();int end=0;int start=0;int ans=0;HashMap<Character,Integer> map=new HashMap<Character,Integer>();while(end<n){char c=s.charAt(end);Integer index=map.get(c);if(index!=null&&index>=start)//发现重复元素{ans=Math.max(ans,end-start);start=index+1;}else{map.put(c, end);end++;}}if(end-start>ans)    ans=end-start;return ans;        }}


0 0
原创粉丝点击