《leetcode》longest-substring-without-repeating

来源:互联网 发布:mac怎么写java 编辑:程序博客网 时间:2024/06/05 11:17

题目描述

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.

解析:题目的意思是求最大不重复的连续的子串,注意字符不能再前面重复。

public class Solution {    public int lengthOfLongestSubstring(String s) {        int max=0;        for(int i=0;i<s.length();i++){            int count=1;//当前字符计数1次            for(int j=i+1;j<s.length();j++){                int start=i;                boolean flag=false;                while (start<j){                    if(s.charAt(j)==s.charAt(start)){                        flag=true;                     break;                    }                    start++;                }                if(!flag){//在之前的字符串里没有出现过                    count++;                }else {//出现后就没必要继续了                    break;                }            }            if(count>=max){                max=count;            }        }        return max;    }}
阅读全文
0 0