本帖最后由 silence_shadow 于 2016-6-9 21:19 编辑
3. 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. 我的JAVA代码: - public class Solution {
- public int lengthOfLongestSubstring(String s) {
- if(s.length() == 0)
- return s.length();
- int max = 1;
- for(int i = 0; (i+1)<s.length(); i++){
- int j = i+1;
- int count = 1;
-
- while(j < s.length() && !s.substring(i,i+1).equals(s.substring(j,j+1)) ){
- boolean dup = false;
- int tempI = i+1;
- while(tempI < j){
- if(s.substring(tempI, tempI+1).equals(s.substring(j,j+1))){
- dup = true;
- break;
- }
- tempI ++;
- }
- if(dup == true)
- break;
- count ++;
- j ++;
- }
- if(count > max)
- max = count;
- }
- return max;
- }
- }
复制代码 正常字符串或者带?,.的都没问题,但无法处理像“\qwertq”这样的带了“\”的字符。。过不了testcase。。
求大神解答~~谢谢~~~   
|