请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
数据范围:
"abcabcbb"
3
因为无重复字符的最长子串是"abc",所以其长度为 3。
"bbbbb"
1
因为无重复字符的最长子串是"b",所以其长度为 1。
"pwwkew"
3
因为无重复字符的最长子串是 "wke",所以其长度为 3。 请注意,你的答案必须是子串的长度,"pwke" 是一个子序列,不是子串。
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型
*/
public int lengthOfLongestSubstring (String s) {
// write code here
if(s == null){
return 0;
}
Set<Character> distinct = new HashSet<>();
int slow = 0;
int fast = 0;
int longest = 0;
while (fast < s.length()) {
if (distinct.contains(s.charAt(fast))) {
distinct.remove(s.charAt(slow));
slow++;
//↑可合并为distinct.remove(s.charAt(slow++));
} else {
distinct.add(s.charAt(fast));
fast++;
//↑可合并为distinct.add(s.charAt(fast++));
longest = Math.max(longest, fast - slow);
}
}
return longest;
}
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型
*/
public int lengthOfLongestSubstring (String s) {
int n = s.length();
Map<Character, Integer> map = new HashMap<>();
int res = 0;
int i = 0;
for (int j = 0; j < n; j++) {
char ch1 = s.charAt(j);
map.put(ch1, map.getOrDefault(ch1, 0) + 1);
if (map.size() == j - i + 1) {
res = Math.max(res, j - i + 1);
}
if (map.size() < j - i + 1) {
char ch2 = s.charAt(i);
map.put(ch2, map.get(ch2) - 1);
if (map.get(ch2) == 0) {
map.remove(ch2);
}
i++;
}
}
return res;
}
} public int lengthOfLongestSubstring(String s) {
// write code here
// 典型的额双指针问题
int len = s.length();
if(len <= 1)
return s.length();
int maxLen = 0;
ArrayList<Character> charList = new ArrayList<>();
for (int i = 0; i < len; i++) {
for (int j = i; j < len; j++) {
char c = s.charAt(j);
if(!charList.contains(c))
charList.add(c);
else {
maxLen = Math.max(maxLen, charList.size());
charList.clear();
break;
}
}
}
return maxLen;
} public int lengthOfLongestSubstring(String s) {
// write code here
int len = 0;
int[] hash = new int[256];
int j = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
hash[c]++; // 初始化时,每个元素对应 ASCII 的数字的值为 1
while (hash[c] > 1) { // 发现有重复的字符串
// 寻找重复元素的下一个下标,退出就是为 j
hash[s.charAt(j)] --;
j++;
}
// 当前的最大值
len = Math.max(i - j + 1, len);
// 继续往下寻找...
}
return len;
} public int lengthOfLongestSubstring (String s) {
// write code here
HashMap<Character, Integer> mp = new HashMap<>();
int res = 0;
if(s.length() <= 1){
return s.length();
}
int left = 0, right = 0;
while(right < s.length()){
if(mp.containsKey(s.charAt(right))){
mp.put(s.charAt(right), mp.get(s.charAt(right)) + 1);
} else {
mp.put(s.charAt(right), 1);
}
if(mp.get(s.charAt(right)) > 1){
// 因为每次subring提取出来的字符串索引值都是从0开始,
// 所以需要加上左边界left,才能表示s.charAT(right)字符在s中的索引位置
int index = s.substring(left, right).indexOf(s.charAt(right)) + left;
mp.put(s.charAt(right), mp.get(s.charAt(right))-1); // 保证字符left,right区间的字符只出现一次。如果不做这一步,假设abcac,此时a重复了,在区间[0,4]之间,a出现两次,移动left到1时,a在[1,4]之间只出现一次,如果不-1,则还是两次,就会判定错误。
res = Math.max(res, right - left);
left = index + 1;
}
right++;
}
// 会出现一种情况,right一直到结束,都没有重复,此时就无法进入if条件判断长度
// 需要在循环结束时,判断最后一个是否重复? 重复,说明已经进入循环内的if判断过
// 不重复,说明还需要再进行判断一次,判断res 与 当前right - left 长度
return mp.get(s.charAt(right-1)) == 1 ? res = Math.max(res, right - left) : res; import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型
*/
public int lengthOfLongestSubstring (String s) {
//不重复最长字符串长度
int maxCount = 0;
//前指针
int frontIndex = 0;
//转成char数组
char[] cs = s.toCharArray();
//字重复判别数组
int[] intArr = new int[256];
//记录单次不重复最长字符串长度
int count = 0;
for (int i = 0; i < cs.length; i++) {
if (intArr[cs[i]] >= 1) {
//重置为0
intArr = new int[256];
if (maxCount < count) {
//获取不重复最长字符串长度
maxCount = count;
}
count = 0;
//下标重新设置
i = (frontIndex++);
} else {
//记录字符
intArr[cs[i]]++;
count++;
}
}
return maxCount > count ? maxCount : count;
}
} public int lengthOfLongestSubstring (String s) {
Set<Character> charSet = new HashSet<>();//感觉用set就足够了
int res = 0;
int left = 0, right = 0;
for (; right < s.length(); right++) {
while (charSet.contains(s.charAt(right))) {
charSet.remove(s.charAt(left));
left++;
}
charSet.add(s.charAt(right));
res = Math.max(res, right - left + 1);
}
return res;
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型
*/
public int lengthOfLongestSubstring (String s) {
// write code here
ArrayDeque<Character> queue = new ArrayDeque<>();
int res = 0;
for (int i = 0; i < s.length(); i ++) {
if (queue.contains(s.charAt(i))) {
while (queue.peek() != s.charAt(i)) {
queue.remove();
}
queue.remove();
}
queue.add(s.charAt(i));
res = Math.max(res, queue.size());
}
return res;
}
} public static int longestSubStringWithoutDuplication(String str) {
Map<Character, Integer> map = new HashMap<>();
int maxLen = 1;
int mark = 0;//左边界
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (map.containsKey(c)) {
mark = Math.max(mark, map.get(c) + 1);
}
map.put(c, i);
maxLen = Math.max(maxLen, i - mark + 1);
}
return maxLen;
} public int lengthOfLongestSubstring(String s) {
// 记录字符上一次出现的位置
int[] last = new int[128];
for (int i = 0; i < 128; i++) last[i] = -1;
int start = 0;
int res = 0;
for (int i = 0; i < s.length(); i++) {
int index = s.charAt(i);
// 控制窗口开始位置
start = Math.max(start, last[index] + 1);
// 判断哪个窗口更长
res = Math.max(res, i - start + 1);
// 记录出现位置
last[index] = i;
}
return res;
}
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型
*/
public int lengthOfLongestSubstring (String s) {
// write code here
if(s.length() <= 1) return s.length();
int[] arr = new int[100010];
int res = 0;
for(int i=0,j=0;i<s.length();i++){
arr[s.charAt(i)]++;
while(arr[s.charAt(i)] > 1){
arr[s.charAt(j)]--;
j++;
}
res = Math.max(res,i-j+1);
}
return res;
}
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型
*/
public int lengthOfLongestSubstring (String s) {
// write code here
int res=0;
for(int i=0;i<s.length();i++){
int sum=1;
for(int j=i+1;j<s.length();j++){
if(s.substring(i,j).lastIndexOf(s.charAt(j)+"")>-1){
break;
}
sum++;
}
res=Math.max(res,sum);
}
return res;
}
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型
*/
public int lengthOfLongestSubstring (String s) {
// write code here
int n = s.length();
int max = Integer.MIN_VALUE;
for(int i = 0; i < n; i++){
int count = 0;
Set<Character> set= new HashSet<>();
for(int j = i; j < n; j++){
if(set.contains(s.charAt(j))){
break;
}
set.add(s.charAt(j));
count++;
}
max = Math.max(count,max);
}
return max;
}
} public int lengthOfLongestSubstring (String s) {
// write code here
HashMap<Character, Integer> map = new HashMap<>();
int res = 0;
int l = 0;
for (int i = 0; i < s.length(); i++) {
if (map.containsKey(s.charAt(i))) {
map.put(s.charAt(i), map.get(s.charAt(i)) + 1);
} else {
map.put(s.charAt(i), 1);
}
//重复了就一直移动左边界 直到不重复
while (map.get(s.charAt(i)) > 1) {
map.put(s.charAt(l), map.get(s.charAt(l)) - 1);
l++;
}
res = Math.max(res, i - l + 1);
}
return res;
}
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型
*/
public int lengthOfLongestSubstring (String s) {
// write code here
Map<Character,Integer>map=new HashMap<>();
int left=0,maxlength=0;
for(int i=0;i<s.length();i++){
if(map.containsKey(s.charAt(i))){
left=Math.max(left,map.get(s.charAt(i))+1);
}
maxlength=Math.max(i-left+1,maxlength);
map.put(s.charAt(i),i);
}
return maxlength;
}
} //双端队列
public int lengthOfLongestSubstring (String s) {
// write code here
Deque<Character> queue = new ArrayDeque<>();
int len = s.length();
int ret = 0;
for(int i=0; i<len; i++){
char c = s.charAt(i);
while(queue.contains(c)){
queue.pollFirst();
}
queue.offerLast(c);
ret = Math.max(ret, queue.size());
}
return ret;
}