class Solution {
public int dismantlingAction(String arr) {
Map<Character, Integer> dic = new HashMap<>();
int l = -1, n = arr.length(), res = 0;
for(int r = 0; r < n; r++) {
if(dic.containsKey(arr.charAt(r))) {
if(dic.get(arr.charAt(r)) < l) l = l;
else l = dic.get(arr.charAt(r));
//l = Math.max(l, dic.get(arr.charAt(r))); // 将l移动到无重复值的区间左侧,且要最大
} // 取最大,是因为可能此时arr[r]对应的字符已经是在l左侧了,不在当前的无重复区间,因此l无需改变
// 为什么已经在左侧了呢?因为其他字符重复了,且在它右边,就把它割掉了,此时该字符其实根本就不重复,因此不用更新左边界
dic.put(arr.charAt(r), r);
res = Math.max(res, r - l);
}
return res;
}
}
class Solution {
public int dismantlingAction(String arr) {
if(arr == null || arr.length() <= 0){
return 0;
}
Map<Character,Integer> map = new HashMap<>();
//含义:以i结尾的字符串中的最大无重复子字符串的长度
int[] dp = new int[arr.length()];
//初始值
dp[0]=1;
map.put(arr.charAt(0), 0);
int res = 1;
for(int i=1;i<arr.length();i++){
if(!map.containsKey(arr.charAt(i))){
dp[i]=dp[i-1]+1;
}else{
int k = map.get(arr.charAt(i));
dp[i]= i-k<=dp[i-1]? i-k : dp[i-1]+1;
}
res=Math.max(dp[i],res);
map.put(arr.charAt(i),i);
}
return res;
}
}
时间复杂度:O(n)
空间复杂度:O(n)
优化做法
// 优化版本
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() <= 0){
return 0;
}
// 优化 => 单个变量
Map<Character,Integer> map = new HashMap<>();
//int[] dp = new int[s.length()];
int a = 1;
map.put(s.charAt(0), 0);
int res = 1;
for(int i = 1; i < s.length(); i++){
if(!map.containsKey(s.charAt(i))){
a = a + 1;// 就是没有刷新 a 之前,a表示dp[i-1]
}else{
int k = map.get(s.charAt(i));
a = i - k <= a ? i - k : a + 1;
}
res = Math.max(res, a);
map.put(s.charAt(i), i);
}
return res;
// 时间On,空间On
}
普通版本:
class Solution {
public int dismantlingAction(String arr) {
if(arr==null||arr.length()<=0){
return 0;
}
Map<Character,Integer> map = new HashMap<>();
int[] dp = new int[arr.length()];
dp[0] = 1;
map.put(arr.charAt(0),0);
int max = 1;
for(int i=1;i<arr.length();i++){
if(!map.containsKey(arr.charAt(i))){
dp[i] = dp[i-1]+1;
}else{
int k = map.get(arr.charAt(i));
dp[i] = i-k<=dp[i-1]?i-k:dp[i-1]+1;
}
max = Math.max(max,dp[i]);
map.put(arr.charAt(i),i);
}
return max;
}
}
时间复杂度:O(n)
空间复杂度:O(n)
优化版本:
class Solution {
public int dismantlingAction(String arr) {
if(arr==null||arr.length()<=0){
return 0;
}
Map<Character,Integer> map = new HashMap<>();
int a = 1;
map.put(arr.charAt(0),0);
int max = 1;
for(int i=1;i<arr.length();i++){
if(!map.containsKey(arr.charAt(i))){
a = a+1;
}else{
int k = map.get(arr.charAt(i));
a = i-k<=a?i-k:a+1;
}
max = Math.max(max,a);
map.put(arr.charAt(i),i);
}
return max;
}
}
时间复杂度:O(n)
空间复杂度:因为用了哈希表,所以感觉也是O(n)
自己写的,算是滑动窗口双指针吧:
class Solution {
Set<Character> set = new HashSet<>();
public int dismantlingAction(String arr) {
int left = 0;
int right = 0;
int length = arr.length();
int max = 0;
while(right<length){
char temp = arr.charAt(right);
if(set.contains(temp)){
set.remove(arr.charAt(left));
left++;
}else{
set.add(temp);
max = Math.max(max,set.size());
right++;
}
}
return max;
}
}
共以下 10 个回答
打卡
class Solution {
public int dismantlingAction(String arr) {
int n = arr.length();
int ans = 0;
}
时间:O(n)
空间:O(n)
class Solution {
public:
int dismantlingAction(string arr) {
unordered_map<char, int> dic;
int res = 0, tmp = 0, len = arr.size(), i;
for(int j = 0; j < len; j++) {
if (dic.find(arr[j]) dic.end()) i = – 1;
else i = dic.find(arr[j])->second; // 获取索引 i
dic[arr[j]] = j; // 更新哈希表
tmp = tmp < j – i ? tmp + 1 : j – i; // dp[j – 1] -> dp[j]
res = max(res, tmp); // max(dp[j – 1], dp[j])
}
return res;
}
};
打卡
时间复杂度O(n),空间复杂度O(1)
时间复杂度:O(n)
空间复杂度:O(n)
优化做法
时间复杂度:O(n)
空间复杂度:O(n)
时间复杂度$O(n)$,空间复杂度$O(1)$
时间复杂度:O(n)
空间复杂度:O(n)
思路:滑动窗口
“`c++ m; 1) {
class Solution {
public:
int dismantlingAction(string arr) {
if (arr.size() == 0) return 0;
int maxi = 0, left = 0;
map
for (int i=0; i
// maxi = max(maxi, i – left);
while (left < i && arr[left] != arr[i]) { m[arr[left]]--; left++; } left++; m[arr[i]]--; } maxi = max(maxi, i - left + 1); } return maxi; } }; ```
//滑动窗口 set = new HashSet<>();
class Solution {
public int dismantlingAction(String arr) {
HashSet
int maxLength = 0;
int fastPtr = -1;
int slowPtr = 0;
while(fastPtr < arr.length()-1){
fastPtr++;
while(set.contains(arr.charAt(fastPtr))){
set.remove(arr.charAt(slowPtr));
slowPtr++;
}
set.add(arr.charAt(fastPtr));
maxLength = Math.max(maxLength,fastPtr – slowPtr + 1);
}
return maxLength;
}
}
//动态规划
class Solution {
public int dismantlingAction(String arr) {
if(arr.length() 0)
return 0;
HashMap<Character,Integer> lastLoc = new HashMap<>();
int max = 1;
int[] dp = new int[arr.length()];
dp[0] = 1;
lastLoc.put(arr.charAt(0),0);
for(int i = 1; i < arr.length(); i++){
if(!lastLoc.containsKey(arr.charAt(i))){
dp[i] = dp[i-1] + 1;
}else{
int j = lastLoc.get(arr.charAt(i));
if(i – j > dp[i-1]){
dp[i] = dp[i-1] + 1;
}
else{
dp[i] = i – j;
}
}
lastLoc.put(arr.charAt(i),i);
max = Math.max(max,dp[i]);
}
return max;
}
}
//两种方法 时间复杂度均为O1 空间复杂度均为On