Compare commits

..

No commits in common. "30322beecb1ae8b3ba0a324929add1f5c9de4486" and "262525db57cc93444befc260874e018db6f13c83" have entirely different histories.

3 changed files with 44 additions and 6 deletions

View File

@ -5,8 +5,7 @@
</component>
<component name="ChangeListManager">
<list default="true" id="45f625e1-36c7-4942-8f7c-ba3ebe9a7d53" name="变更" comment="">
<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$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -77,11 +76,22 @@
<option name="presentableId" value="Default" />
<updated>1693822719192</updated>
<workItem from="1693822720224" duration="5742000" />
<workItem from="1693895305007" duration="5324000" />
<workItem from="1693895305007" duration="5192000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/solution/test.java</url>
<line>38</line>
<option name="timeStamp" value="4" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
</project>

View File

@ -8,7 +8,7 @@ import java.util.Set;
public class Solution {
//给定一个字符串 s ,找出其中不含有重复字符的 最长子串 的长度。
public static int lengthOfLongestSubstring(String s) {
public int lengthOfLongestSubstring(String s) {
// 哈希集合,记录每个字符是否出现过
Set<Character> occ = new HashSet<Character>();
int n = s.length();
@ -52,7 +52,7 @@ public class Solution {
//哈希映射算法Hash Mapping Algorithm用于找出一个数组中的众数majority element
public static int majorityElement(int[] nums) {
public int majorityElement(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
// maxNum 表示元素maxCount 表示元素出现的次数
int maxNum = 0, maxCount = 0;

View File

@ -6,6 +6,34 @@ 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(s3));
}
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));
}
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 ans;
}
}