维持一个滑动窗口 使其中的数各不一样 不断记录更新滑动窗口中和的最大值
class Solution {
public int maximumUniqueSubarray(int[] nums) {
int n = nums.length;
int left = 0;
Set<Integer> set = new HashSet<>();
int total = 0;
int ans = 0;
for(int right = 0; right < n;right++){
while(set.contains(nums[right])){
set.remove(nums[left]);
total -= nums[left];
left += 1;
}
set.add(nums[right]);
total += nums[right];
ans = Math.max(ans,total);
}
return ans;
}
}