Compare commits
3 Commits
262525db57
...
30322beecb
| Author | SHA1 | Date |
|---|---|---|
|
|
30322beecb | |
|
|
6571eb45d2 | |
|
|
955e0e3d05 |
|
|
@ -5,7 +5,8 @@
|
|||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<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>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
|
|
@ -76,22 +77,11 @@
|
|||
<option name="presentableId" value="Default" />
|
||||
<updated>1693822719192</updated>
|
||||
<workItem from="1693822720224" duration="5742000" />
|
||||
<workItem from="1693895305007" duration="5192000" />
|
||||
<workItem from="1693895305007" duration="5324000" />
|
||||
</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>
|
||||
|
|
@ -8,7 +8,7 @@ import java.util.Set;
|
|||
public class Solution {
|
||||
|
||||
//给定一个字符串 s ,找出其中不含有重复字符的 最长子串 的长度。
|
||||
public int lengthOfLongestSubstring(String s) {
|
||||
public static 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 int majorityElement(int[] nums) {
|
||||
public static int majorityElement(int[] nums) {
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
// maxNum 表示元素,maxCount 表示元素出现的次数
|
||||
int maxNum = 0, maxCount = 0;
|
||||
|
|
|
|||
|
|
@ -6,34 +6,6 @@ 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue