Compare commits
No commits in common. "31b00f672fd05186ec945255b518d06ff57b3e91" and "f70a0e7c2c66ebbba0b8cafb72a49965d918cf63" have entirely different histories.
31b00f672f
...
f70a0e7c2c
|
|
@ -6,7 +6,6 @@
|
|||
<component name="ChangeListManager">
|
||||
<list default="true" id="45f625e1-36c7-4942-8f7c-ba3ebe9a7d53" name="变更" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/solution/Solution.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/solution/Solution.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/solution/test.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/solution/test.java" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
|
|
@ -78,7 +77,7 @@
|
|||
<option name="presentableId" value="Default" />
|
||||
<updated>1693822719192</updated>
|
||||
<workItem from="1693822720224" duration="5742000" />
|
||||
<workItem from="1693895305007" duration="4114000" />
|
||||
<workItem from="1693895305007" duration="634000" />
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
|
|
@ -90,9 +89,14 @@
|
|||
<breakpoints>
|
||||
<line-breakpoint enabled="true" type="java-line">
|
||||
<url>file://$PROJECT_DIR$/src/solution/test.java</url>
|
||||
<line>38</line>
|
||||
<line>24</line>
|
||||
<option name="timeStamp" value="4" />
|
||||
</line-breakpoint>
|
||||
<line-breakpoint enabled="true" type="java-line">
|
||||
<url>file://$PROJECT_DIR$/src/solution/test.java</url>
|
||||
<line>17</line>
|
||||
<option name="timeStamp" value="5" />
|
||||
</line-breakpoint>
|
||||
</breakpoints>
|
||||
</breakpoint-manager>
|
||||
</component>
|
||||
|
|
|
|||
|
|
@ -5,24 +5,13 @@ import java.util.Set;
|
|||
|
||||
public class Solution {
|
||||
public int lengthOfLongestSubstring(String s) {
|
||||
// 哈希集合,记录每个字符是否出现过
|
||||
Set<Character> occ = new HashSet<Character>();
|
||||
int n = s.length();
|
||||
// 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
|
||||
int rk = -1, ans = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (i != 0) {
|
||||
// 左指针向右移动一格,移除一个字符
|
||||
occ.remove(s.charAt(i - 1));
|
||||
if (s != null){
|
||||
Set<Character> stringList = new HashSet<>();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
stringList.add(s.charAt(i));
|
||||
}
|
||||
while (rk + 1 < n && !occ.contains(s.charAt(rk + 1))) {
|
||||
// 不断地移动右指针
|
||||
occ.add(s.charAt(rk + 1));
|
||||
++rk;
|
||||
}
|
||||
// 第 i 到 rk 个字符是一个极长的无重复字符子串
|
||||
ans = Math.max(ans, rk - i + 1);
|
||||
return stringList.size();
|
||||
}
|
||||
return ans;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,39 +1,49 @@
|
|||
package solution;
|
||||
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class test {
|
||||
public static void main(String[] args) {
|
||||
String s = "bbbbb";
|
||||
String s1 = "abcabcbb";
|
||||
String s2 = "pwwkew";
|
||||
String s3 = "ckilbkd";
|
||||
String s4 = "dvdf";
|
||||
System.out.println(lengthOfLongestSubstring(s));
|
||||
String s = "abcabcbb";
|
||||
int length = lengthOfLongestSubstring(s);
|
||||
System.out.println(length);
|
||||
|
||||
}
|
||||
|
||||
public static int lengthOfLongestSubstring(String s) {
|
||||
// 哈希集合,记录每个字符是否出现过
|
||||
Set<Character> occ = new HashSet<Character>();
|
||||
int n = s.length();
|
||||
// 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
|
||||
int rk = -1, ans = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (i != 0) {
|
||||
// 左指针向右移动一格,移除一个字符
|
||||
occ.remove(s.charAt(i - 1));
|
||||
StringBuilder longestString = new StringBuilder();
|
||||
StringBuilder lastMaxString = new StringBuilder();
|
||||
if (s != null){
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char temp = s.charAt(i);
|
||||
if (i == 0){
|
||||
longestString.append(temp);
|
||||
continue;
|
||||
}
|
||||
Character prChar = s.charAt(i-1);
|
||||
if (prChar != temp){
|
||||
boolean in = isInString(temp,longestString);
|
||||
if (!in){
|
||||
longestString.append(temp);
|
||||
}
|
||||
|
||||
} else {
|
||||
lastMaxString = longestString;
|
||||
}
|
||||
}
|
||||
while (rk + 1 < n && !occ.contains(s.charAt(rk + 1))) {
|
||||
// 不断地移动右指针
|
||||
occ.add(s.charAt(rk + 1));
|
||||
++rk;
|
||||
}
|
||||
// 第 i 到 rk 个字符是一个极长的无重复字符子串
|
||||
ans = Math.max(ans, rk - i + 1);
|
||||
return longestString.length();
|
||||
}
|
||||
return ans;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static boolean isInString(Character a,StringBuilder b){
|
||||
if (b != null) {
|
||||
for (int i = 0; i < b.length(); i++) {
|
||||
Character temp = b.charAt(i);
|
||||
if (temp == a) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue