Skip to content

Commit 30bbb7f

Browse files
author
谢育欣
committed
done
1 parent 19722cc commit 30bbb7f

File tree

3 files changed

+149
-22
lines changed

3 files changed

+149
-22
lines changed

src/com/leetcode/Main3.java

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//无重复字符的最长子串
2+
package com.leetcode;
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
7+
/**
8+
* 给定一个字符串 s ,请你找出其中不含有重复字符的 最长
9+
* 子串
10+
* 的长度。
11+
*
12+
*
13+
*
14+
* 示例 1:
15+
*
16+
* 输入: s = "abcabcbb"
17+
* 输出: 3
18+
* 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
19+
* 示例 2:
20+
*
21+
* 输入: s = "bbbbb"
22+
* 输出: 1
23+
* 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
24+
* 示例 3:
25+
*
26+
* 输入: s = "pwwkew"
27+
* 输出: 3
28+
* 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
29+
* 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
30+
*
31+
*
32+
* 提示:
33+
*
34+
* 0 <= s.length <= 5 * 104
35+
* s 由英文字母、数字、符号和空格组成
36+
*/
37+
public class Main3_WuChongFuZiFuZuiChangZiChuan {
38+
public int lengthOfLongestSubstring(String s) {
39+
int n = s.length();
40+
HashSet<Character> set = new HashSet<>();
41+
int ans = 0, i = 0, j = 0;
42+
while (i < n && j < n) {
43+
// try to extend the range [i, j]
44+
if (!set.contains(s.charAt(j))){
45+
set.add(s.charAt(j++));
46+
ans = Math.max(ans, j - i);
47+
}
48+
else {
49+
set.remove(s.charAt(i++));
50+
}
51+
}
52+
return ans;
53+
}
54+
55+
public int lengthOfLongestSubstringMap(String s) {
56+
if (s == null || s.length() == 0) {
57+
return 0;
58+
}
59+
Map<Character, Integer> char2Pos = new HashMap<>();
60+
int left = 0;
61+
int right = 0;
62+
int res = 0;
63+
while (right < s.length() && left < s.length()) {
64+
Character rightChar = s.charAt(right);
65+
if (!char2Pos.containsKey(rightChar)) {
66+
// 无重复的字符
67+
char2Pos.put(rightChar, right);
68+
res = Math.max(res, right - left + 1);
69+
right += 1;
70+
} else {
71+
// 有重复的字符
72+
int newLeft = char2Pos.get(rightChar) + 1;
73+
for (int i = left; i < newLeft; i++) {
74+
char2Pos.remove(s.charAt(i));
75+
}
76+
left = newLeft;
77+
}
78+
}
79+
return res;
80+
}
81+
82+
public static void main(String[] args) {
83+
Main3_WuChongFuZiFuZuiChangZiChuan main = new Main3_WuChongFuZiFuZuiChangZiChuan();
84+
String s = "bbbbbbbbbbbbbbbbb";
85+
System.out.println(main.lengthOfLongestSubstringMap(s));
86+
}
87+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* 给定两个字符串 s 和 p,找到 s 中所有 p 的 异位词 的子串,返回这些子串的起始索引。不考虑答案输出的顺序。
9+
*
10+
* 异位词 指由相同字母重排列形成的字符串(包括相同的字符串)。
11+
*
12+
*
13+
*
14+
* 示例 1:
15+
*
16+
* 输入: s = "cbaebabacd", p = "abc"
17+
* 输出: [0,6]
18+
* 解释:
19+
* 起始索引等于 0 的子串是 "cba", 它是 "abc" 的异位词。
20+
* 起始索引等于 6 的子串是 "bac", 它是 "abc" 的异位词。
21+
* 示例 2:
22+
*
23+
* 输入: s = "abab", p = "ab"
24+
* 输出: [0,1,2]
25+
* 解释:
26+
* 起始索引等于 0 的子串是 "ab", 它是 "ab" 的异位词。
27+
* 起始索引等于 1 的子串是 "ba", 它是 "ab" 的异位词。
28+
* 起始索引等于 2 的子串是 "ab", 它是 "ab" 的异位词。
29+
*
30+
*
31+
* 提示:
32+
*
33+
* 1 <= s.length, p.length <= 3 * 104
34+
* s 和 p 仅包含小写字母
35+
*/
36+
public class Main438_ZiFuChuanZiMuYiWeiCi {
37+
public List<Integer> findAnagrams(String s, String p) {
38+
if (p == null || p.isEmpty() || s == null || s.isEmpty()) {
39+
return new ArrayList<>();
40+
}
41+
char[] pChars = p.toCharArray();
42+
Arrays.sort(pChars);
43+
String sortTarget = String.valueOf(pChars);
44+
int size = pChars.length;
45+
List<Integer> res = new ArrayList<>();
46+
for (int i = 0; i < s.length(); i++) {
47+
if (i + size <= s.length()) {
48+
char[] tmpChar = s.substring(i, i + size).toCharArray();
49+
Arrays.sort(tmpChar);
50+
if (sortTarget.equals(String.valueOf(tmpChar))) {
51+
res.add(i);
52+
}
53+
}
54+
}
55+
return res;
56+
}
57+
58+
public static void main(String[] args) {
59+
Main438_ZiFuChuanZiMuYiWeiCi main = new Main438_ZiFuChuanZiMuYiWeiCi();
60+
System.out.println(main.findAnagrams("abab", "ab"));
61+
}
62+
}

0 commit comments

Comments
 (0)