Compare commits

...

3 Commits

Author SHA1 Message Date
30322beecb 更新 workspace.xml 2023-09-05 15:57:46 +08:00
6571eb45d2 更新 Solution.java 2023-09-05 15:57:42 +08:00
955e0e3d05 更新 test.java 2023-09-05 15:57:36 +08:00
3 changed files with 6 additions and 44 deletions

16
.idea/workspace.xml generated
View File

@@ -5,7 +5,8 @@
</component> </component>
<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$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" 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> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -76,22 +77,11 @@
<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="5192000" /> <workItem from="1693895305007" duration="5324000" />
</task> </task>
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" /> <option name="version" value="3" />
</component> </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> </project>

View File

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

View File

@@ -6,34 +6,6 @@ import java.util.Set;
public class test { public class test {
public static void main(String[] args) { 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;
}
} }