找到没有重复字符的最长子串 - Java
Find the Longest Substring without Repeating Characters - Java
我试图解决一个 leetcode 问题,但我的解决方案没有为其中一个测试用例返回正确的值。我想为这个解决方案实现两指针技术(如果我使用的技术有误,欢迎提出建议来解释正确的方法)。详情如下:
Leetcode 题目:
Longest Substring Without Repeating Characters.
问题:
Given a string, find the length of the longest substring without repeating characters. ( return int )
我的解决方案:
public int lengthOfLongestSubstring(String s) {
//place holder for me to create a "sub-string"
String sub = "";
//counter variable to count the characters in the substring
int count = 0;
int maxCount = 0;
//my attempt at a TWO POINTER TECHNIQUE
//pointers - to determine when the loop should break
int head = 0;
int tail = s.length();
//this variable was intended to be used with the "indexOf" method, as the "start from" index...
int index = 0;
while(head < tail){
//check if the next character in the string was already added to my substring.
int val = sub.indexOf(s.charAt(head), index);
//we proceed if it is -1 because this means the substring didnt previously contain that character
if(val == -1){
//added character to my substring
sub+= s.charAt(head);
count++;
head++;
} else {
//reset data to default conditions, while continuing at the "head index" and check the rest of the substring
count = 0;
sub = "";
}
//determine what the length of the longest substring.
maxCount = count > maxCount ? count : maxCount;
}
return maxCount;
}
我通过了测试用例:
> "" = expect 0
> " " = expect 1
> "abcabcbb" = expect 3
> "bbbbb" = expect 1
> "pwwkew" = expect 3
> "aab" = expect 2
我失败的测试用例:
> "dvdgd" = expect 3, but got 2
> "dvjgdeds" = expect 5, but got 4
> "ddvbgdaeds" = expect 6, but got 4
可能的问题:
I believe the issue occurred because it moves pass the index with "v", and then processes the substring "dg". So I attempted to change the solution, but fixing that issue caused all my other cases to return errors so I figured that attempt at a fix should be tossed out.
我也尝试过:
I also attempted to manipulate the "indexOf" method to change the start index, whenever the character was found in the string. This however generated an infinite loop.
我已经尝试解决这个问题几个小时了,我被踩住了。因此,我们将不胜感激任何帮助。如果可以的话,请详细说明,我是 DSA 和编程的新手,非常感谢。如果需要我提供更多信息,请告诉我我会尽力回答。
好了,给你:
public static int lengthOfLongestSubstring(String s) {
//place holder for me to create a "sub-string"
String sub = "";
//counter variable to count the characters in the substring
int count = 0;
int maxCount = 0;
//my attempt at a TWO POINTER TECHNIQUE
//pointers - to determine when the loop should break
int head = 0;
int tail = s.length();
//this variable shows where to start from
int index = 0;
while(head < tail){
//check if the next character in the string was already added to my substring.
int val = sub.indexOf(s.charAt(head)); //search whole substing
//we proceed if it is -1 because this means the substring didnt previously contain that character
if(val == -1){
//added character to my substring
sub+= s.charAt(head);
count++;
head++;
//determine what the length of the longest substring.
maxCount = count > maxCount ? count : maxCount;
System.out.println(sub); //let's see what we got so far
} else {
//reset data to default conditions, while continuing at the "head index" and check the rest of the substring
count = 0;
sub = "";
head=index+1; //begin from the next letter
index++; //move
}
}
return maxCount;
}
我试图解决一个 leetcode 问题,但我的解决方案没有为其中一个测试用例返回正确的值。我想为这个解决方案实现两指针技术(如果我使用的技术有误,欢迎提出建议来解释正确的方法)。详情如下:
Leetcode 题目:
Longest Substring Without Repeating Characters.
问题:
Given a string, find the length of the longest substring without repeating characters. ( return int )
我的解决方案:
public int lengthOfLongestSubstring(String s) {
//place holder for me to create a "sub-string"
String sub = "";
//counter variable to count the characters in the substring
int count = 0;
int maxCount = 0;
//my attempt at a TWO POINTER TECHNIQUE
//pointers - to determine when the loop should break
int head = 0;
int tail = s.length();
//this variable was intended to be used with the "indexOf" method, as the "start from" index...
int index = 0;
while(head < tail){
//check if the next character in the string was already added to my substring.
int val = sub.indexOf(s.charAt(head), index);
//we proceed if it is -1 because this means the substring didnt previously contain that character
if(val == -1){
//added character to my substring
sub+= s.charAt(head);
count++;
head++;
} else {
//reset data to default conditions, while continuing at the "head index" and check the rest of the substring
count = 0;
sub = "";
}
//determine what the length of the longest substring.
maxCount = count > maxCount ? count : maxCount;
}
return maxCount;
}
我通过了测试用例:
> "" = expect 0
> " " = expect 1
> "abcabcbb" = expect 3
> "bbbbb" = expect 1
> "pwwkew" = expect 3
> "aab" = expect 2
我失败的测试用例:
> "dvdgd" = expect 3, but got 2
> "dvjgdeds" = expect 5, but got 4
> "ddvbgdaeds" = expect 6, but got 4
可能的问题:
I believe the issue occurred because it moves pass the index with "v", and then processes the substring "dg". So I attempted to change the solution, but fixing that issue caused all my other cases to return errors so I figured that attempt at a fix should be tossed out.
我也尝试过:
I also attempted to manipulate the "indexOf" method to change the start index, whenever the character was found in the string. This however generated an infinite loop.
我已经尝试解决这个问题几个小时了,我被踩住了。因此,我们将不胜感激任何帮助。如果可以的话,请详细说明,我是 DSA 和编程的新手,非常感谢。如果需要我提供更多信息,请告诉我我会尽力回答。
好了,给你:
public static int lengthOfLongestSubstring(String s) {
//place holder for me to create a "sub-string"
String sub = "";
//counter variable to count the characters in the substring
int count = 0;
int maxCount = 0;
//my attempt at a TWO POINTER TECHNIQUE
//pointers - to determine when the loop should break
int head = 0;
int tail = s.length();
//this variable shows where to start from
int index = 0;
while(head < tail){
//check if the next character in the string was already added to my substring.
int val = sub.indexOf(s.charAt(head)); //search whole substing
//we proceed if it is -1 because this means the substring didnt previously contain that character
if(val == -1){
//added character to my substring
sub+= s.charAt(head);
count++;
head++;
//determine what the length of the longest substring.
maxCount = count > maxCount ? count : maxCount;
System.out.println(sub); //let's see what we got so far
} else {
//reset data to default conditions, while continuing at the "head index" and check the rest of the substring
count = 0;
sub = "";
head=index+1; //begin from the next letter
index++; //move
}
}
return maxCount;
}