《leetcode》longest-substring-without-repeating
生活随笔
收集整理的這篇文章主要介紹了
《leetcode》longest-substring-without-repeating
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
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;} }總結
以上是生活随笔為你收集整理的《leetcode》longest-substring-without-repeating的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《leetcode》reverse-in
- 下一篇: 《leetcode》two-sum