classSolution {
publicintlengthOfLongestSubstring(String s) {
HashSet<Character> set = newHashSet<Character>();
intleft=0, ans = 0;
for (intright=0; right < s.length(); right++) {
while (set.contains(s.charAt(right)))
set.remove(s.charAt(left++));
ans = Math.max(ans, right - left + 1);
set.add(s.charAt(right));
}
return ans;
}
}
classSolution:
deflengthOfLongestSubstring(self, s: str) -> int:
st = set(); left = ans = 0for right, ch inenumerate(s):
while ch in st:
st.remove(s[left])
left += 1
ans = max(ans, right - left + 1)
st.add(ch)
return ans
classSolution {
publicintlongestSubarray(int[] nums, int limit) {
// 有序红黑树
TreeMap<Integer, Integer> map = newTreeMap<Integer, Integer>();
intn= nums.length, left = 0, ans = 0;
for (intright=0; right < n; right++) {
map.merge(nums[right], 1, Integer::sum); // 将当前值放入表中while (map.lastKey() - map.firstKey() > limit) {
// 需要依次删除左边元素 保证符合要求if (map.merge(nums[left], -1, Integer::sum) == 0)
map.remove(nums[left]);
left += 1;
}
ans = Math.max(ans, right - left + 1); // 记录
}
return ans;
}
}
classSolution:
deflongestSubarray(self, nums: List[int], limit: int) -> int:
from sortedcontainers import SortedList # Python的有序集合
s, left, ans = SortedList(), 0, 0for right inrange(len(nums)):
s.add(nums[right])
while s[-1] - s[0] > limit:
s.remove(nums[left])
left += 1
ans = max(ans, right - left + 1)
return ans
方法2:滑动窗口 + 单调队列
classSolution {
publicintlongestSubarray(int[] nums, int limit) {
// 使用单调队列保存当前窗口的最大值和最小值
ArrayDeque<Integer> Max = newArrayDeque<Integer>();
ArrayDeque<Integer> Min = newArrayDeque<Integer>();
intn= nums.length, left = 0, ans = 0;
for (intright=0; right < n; right++) {
// 1.保持单调队列的单调性while (!Max.isEmpty() && Max.peekLast() < nums[right])
Max.pollLast(); // 单调递减 队头为最大值while (!Min.isEmpty() && Min.peekLast() > nums[right])
Min.pollLast(); // 单调递增 队头为最小值// 2.添加元素
Max.offerLast(nums[right]); Min.offerLast(nums[right]);
// 3.保证当前窗口满足要求while (!Max.isEmpty() && !Min.isEmpty() && Max.peekFirst() - Min.peekFirst() > limit) {
// 依次删除左边的值if (nums[left] == Min.peekFirst())
Min.pollFirst();
if (nums[left] == Max.peekFirst())
Max.pollFirst();
left += 1;
}
// 4.记录答案
ans = Math.max(ans, right - left + 1);
}
return ans;
}
}
classSolution:
deflongestSubarray(self, nums: List[int], limit: int) -> int:
from collections import deque
Max, Min = deque(), deque()
left, ans = 0, 0for right inrange(len(nums)):
while Max and Max[-1] < nums[right]:
Max.pop()
while Min and Min[-1] > nums[right]:
Min.pop()
Max.append(nums[right]); Min.append(nums[right])
while Max and Min and Max[0] - Min[0] > limit:
if Max[0] == nums[left]:
Max.popleft()
if Min[0] == nums[left]:
Min.popleft()
left += 1
ans = max(ans, right - left + 1)
return ans
classSolution {
publicintlongestNiceSubarray(int[] nums) {
intn= nums.length, left = 0, val = 0, ans = 1;
for (intright=0; right < n; right++) {
while ((val & nums[right]) > 0) // 需要移动 left 保证其为0
val ^= nums[left++];
val |= nums[right];
ans = Math.max(ans, right - left + 1);
}
return ans;
}
}
classSolution:
deflongestNiceSubarray(self, nums: List[int]) -> int:
left, val, ans = 0, 0, 1for right inrange(len(nums)):
while val & nums[right]:
val ^= nums[left]
left += 1
val |= nums[right]
ans = max(ans, right - left + 1)
return ans
classSolution {
publicintminOperations(int[] nums, int x) {
intn= nums.length;
ints= Arrays.stream(nums).sum(); // 流// 某个前缀 + 某个后缀 == x 且 长度最小if (s < x) return -1;
intright=0, ans = n + 1; // ans求最小intlsum=0, rsum = s;
for (intleft= -1; left < n; left++) {
// (1)lsum + rsum < x 将left左移
lsum += left == -1 ? 0 : nums[left];
// (2)lsum + rsum > x 将right左移while (right < n && lsum + rsum > x)
rsum -= nums[right++];
// (3)lsum + rsum == x 记录答案if (lsum + rsum == x)
ans = Math.min(ans, left + 1 + n - right);
}
returnans== n + 1 ? -1 : ans;
}
}
classSolution:
defminOperations(self, nums: List[int], x: int) -> int:
n = len(nums)
right, ans = 0, n + 1
lsum, rsum = 0, sum(nums)
for left inrange(-1, n):
lsum += 0if left == -1else nums[left]
while right < n and lsum + rsum > x:
rsum -= nums[right]
right += 1if lsum + rsum == x:
ans = min(ans, left + 1 + n - right)
return -1if ans == n + 1else ans
方法2:滑动窗口 + 逆向思维
classSolution {
publicintminOperations(int[] nums, int x) {
intn= nums.length;
ints= Arrays.stream(nums).sum();
// 使和为 s - x 的区间长度最长if (s < x) return -1;
if (s == x) return n; // 可以保证后面必有元素被删除intleft=0, val = 0, ans = 0;
for (intright=0; right < n; right++) {
val += nums[right];
while (val > s - x)
val -= nums[left++];
if (val == s - x)
ans = Math.max(ans, right - left + 1);
}
returnans== 0 ? -1 : n - ans;
}
}
classSolution:
defminOperations(self, nums: List[int], x: int) -> int:
s = sum(nums); n = len(nums)
if s < x: return -1if s == x: return n
left = 0; val = 0; ans = 0for right inrange(n):
val += nums[right]
while val > s - x:
val -= nums[left]
left += 1if val == s - x:
ans = max(ans, right - left + 1)
return -1if ans == 0else n - ans
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?