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