1. 程式人生 > >字串--無重複最多

字串--無重複最多

  無重複字元的最長子串   https://leetcode-cn.com/explore/interview/card/bytedance/242/string/1012/

 

輸入: "abcabcbb"
輸出: 3 
解釋: 因為無重複字元的最長子串是 

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
    	
    	Solution s = new Solution();
        int a =  s.lengthOfLongestSubstring("aab");
        System.out.println(a);
          
    }
}
class Solution {
    public int lengthOfLongestSubstring(String s) {
        char[] arr = s.toCharArray();
        int result = 0;
        int point = 0;
        int jump = -1;
        int start = 0;
        for(int i=1; i<arr.length; i++){
       
        	for(int j=start; j<i; j++){

        	   if(arr[i] == arr[j]){
        		   result = i - start>result?i - start:result;   
        	   
        		   i = j+1;
        		   start = j+1;
        		   break;
        	   }
        	} 
        	if(i==arr.length-1){

        		 result = i - start+1>result?i - start+1:result;   
        	}
        }
    	return result==0?arr.length:result;
    }
}
"abc",所以其長度為 3。

   思路:雙重迴圈比較,切記for迴圈中i++結束迴圈後+1