1. 程式人生 > >(java)leetcode-取最大長度子字串

(java)leetcode-取最大長度子字串

Longest Substring Without Repeating Characters

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.

public String resolve(String str) {
    char[] chars = str.toCharArray();
String answer = "";
    int maxLength = 0;
String temp = "";
    int tempLength = 0;
    for (int i = 0; i < chars.length; i++) {
        if (temp.indexOf(chars[i]) != -1) {
            if 
(tempLength > maxLength) { answer = temp; maxLength = tempLength; } temp = ""; tempLength = 0; } temp += chars[i]; tempLength = temp.length(); } return answer; }