分段双指针
分隔链表(medium难度)
https://leetcode-cn.com/problems/partition-list/
本方法思路及代码来源:
作者:Booooo_
链接:https://leetcode-cn.com/problems/partition-list/solution/fen-ge-lian-biao-lian-biao-jiang-yuan-li-2w5t/
来源:力扣(LeetCode)
解题思路
将链表按条件分成两个链表,然后连接起来即可。
需要为两个链表创建两个值为 0 的虚拟头节点,然后遍历原链表,若当前遍历的节点值大于或等于 x,则 large 指针将该节点 p 与 large 链表连接起来,并 large 指针后移;否则,就是对 small 链表进行操作。
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode small = new ListNode(0);
ListNode smallHead = small;
ListNode large = new ListNode(0);
ListNode largeHead = large;
ListNode p = head;
while (p != null) {
if (p.val >= x) {
large.next = p;
large = large.next;
} else {
small.next = p;
small = small.next;
}
p = p.next;
}
small.next = largeHead.next;
large.next = null;
return smallHead.next;
}
}
作者:Booooo_
链接:https://leetcode-cn.com/problems/partition-list/solution/fen-ge-lian-biao-lian-biao-jiang-yuan-li-2w5t/
来源:力扣(LeetCode)
合并两个有序数组(simple难度)
https://leetcode-cn.com/problems/merge-sorted-array/
本题思路算法及代码来源:
作者:guanpengchn
链接:https://leetcode-cn.com/problems/merge-sorted-array/solution/hua-jie-suan-fa-88-he-bing-liang-ge-you-xu-shu-zu-/
来源:力扣(LeetCode)
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int len1 = m - 1;
int len2 = n - 1;
int len = m + n - 1;
while(len1 >= 0 && len2 >= 0) {
// 注意--符号在后面,表示先进行计算再减1,这种缩写缩短了代码
nums1[len--] = nums1[len1] > nums2[len2] ? nums1[len1--] : nums2[len2--];
}
while (len2 >=0) {
nums1[len--] = nums2[len2--];
}
// 表示将nums2数组从下标0位置开始,拷贝到nums1数组中,从下标0位置开始,长度为len2+1
//System.arraycopy(nums2, 0, nums1, 0, len2 + 1);
}
}
作者:guanpengchn
链接:https://leetcode-cn.com/problems/merge-sorted-array/solution/hua-jie-suan-fa-88-he-bing-liang-ge-you-xu-shu-zu-/
来源:力扣(LeetCode)
奇偶链表(medium难度)
https://leetcode-cn.com/problems/odd-even-linked-list/
11
class Solution {
public ListNode oddEvenList(ListNode head) {
if (head == null) {
return head;
}
ListNode evenHead = head.next;
ListNode odd = head, even = evenHead;
while (even != null && even.next != null) {
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = evenHead;
return head;
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/odd-even-linked-list/solution/qi-ou-lian-biao-by-leetcode-solution/
来源:力扣(LeetCode)
头尾指针
两数之和Ⅱ - 输入有序数组(simple难度)
https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/
class Solution {
public int[] twoSum(int[] numbers, int target) {
if (numbers.length == 2) {
return new int[]{1,2};
}
int i = 0, j = numbers.length - 1;
while (i < j) {
int sum = numbers[i] + numbers[j];
if(sum < target) {
while(i < j && numbers[i] == numbers[++i]);
} else if(sum > target){
while(i < j && numbers[j] == numbers[--j]);
} else {
return new int[]{i+1,j+1};
}
}
return new int[]{-1, -1};
}
}
三数之和(medium难度)
https://leetcode-cn.com/problems/3sum/
本方法思路及代码来源:
作者:jyd
链接:https://leetcode-cn.com/problems/3sum/solution/3sumpai-xu-shuang-zhi-zhen-yi-dong-by-jyd/
来源:力扣(LeetCode)
class Solution:
def threeSum(self, nums: [int]) -> [[int]]:
nums.sort()
res, k = [], 0
for k in range(len(nums) - 2):
if nums[k] > 0: break # 1. because of j > i > k.
if k > 0 and nums[k] == nums[k - 1]: continue # 2. skip the same `nums[k]`.
i, j = k + 1, len(nums) - 1
while i < j: # 3. double pointer
s = nums[k] + nums[i] + nums[j]
if s < 0:
i += 1
while i < j and nums[i] == nums[i - 1]: i += 1
elif s > 0:
j -= 1
while i < j and nums[j] == nums[j + 1]: j -= 1
else:
res.append([nums[k], nums[i], nums[j]])
i += 1
j -= 1
while i < j and nums[i] == nums[i - 1]: i += 1
while i < j and nums[j] == nums[j + 1]: j -= 1
return res
作者:jyd
链接:https://leetcode-cn.com/problems/3sum/solution/3sumpai-xu-shuang-zhi-zhen-yi-dong-by-jyd/
来源:力扣(LeetCode)
最接近三数之和(medium难度)
https://leetcode-cn.com/problems/3sum-closest/
本题思路及代码来源:
作者:guanpengchn
链接:https://leetcode-cn.com/problems/3sum-closest/solution/hua-jie-suan-fa-16-zui-jie-jin-de-san-shu-zhi-he-b/
来源:力扣(LeetCode)
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int ans = nums[0] + nums[1] + nums[2];
for(int i=0;i<nums.length;i++) {
int start = i+1, end = nums.length - 1;
while(start < end) {
int sum = nums[start] + nums[end] + nums[i];
if(Math.abs(target - sum) < Math.abs(target - ans))
ans = sum;
if(sum > target)
end--;
else if(sum < target)
start++;
else
return ans;
}
}
return ans;
}
}
作者:guanpengchn
链接:https://leetcode-cn.com/problems/3sum-closest/solution/hua-jie-suan-fa-16-zui-jie-jin-de-san-shu-zhi-he-b/
来源:力扣(LeetCode)
四数之和(medium难度)
https://leetcode-cn.com/problems/4sum/
代码实现:
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
//剪枝
if(nums==null||nums.length<4){
return res;
}
Arrays.sort(nums);
for(int m = 0; m < nums.length - 3; m++) {
//剪枝,获取当前最小值,如果最小值比目标值大,说明后面的值不成立
if(nums[m] + nums[m+1] + nums[m+2]+ nums[m+3] > target) break; //剪枝
if(m > 0 && nums[m] == nums[m-1]) continue;
for(int n = nums.length - 1; n > 2; n--){
//剪枝,获取当前最大值,如果最大值比目标值小,说明后面的值不成立
if(nums[n] + nums[n-1] + nums[n-2]+ nums[n-3] < target) break;
if(n < nums.length - 1 && nums[n] == nums[n+1]) continue;
int i = m + 1, j = n - 1;
while (i < j) {
int sum = nums[m] + nums[i] + nums[j]+ nums[n];
if (sum < target){
//跳过所有重复的nums[i]
while(i < j && nums[i] == nums[++i]);
} else if (sum > target) {
//跳过所有重复的nums[j]
while(i < j && nums[j] == nums[--j]);
} else { // sum == target
//记录组合[m, i, j,n]至res,执行i += 1和j -= 1并跳过所有重复的nums[i]和nums[j]
//防止记录到重复组合。
res.add(new ArrayList<Integer>(Arrays.asList(nums[m],nums[i],nums[j],nums[n])));
while(i < j && nums[i] == nums[++i]);
while(i < j && nums[j] == nums[--j]);
}
}
}
}
return res;
}
}
作者:HIT_whc
链接:https://leetcode-cn.com/problems/4sum/solution/fang-zhao-san-shu-zhi-he-de-javatou-wei-kal2t/
来源:力扣(LeetCode)
有一些细节需要注意,例如: 不要判断nums[k] > target
就返回了,三数之和可以通过nums[i] > 0
就返回了,因为0
已经是确定的数了,四数之和这道题目target
是任意值。(亲自写代码就能感受出来)
另一种代码实现:
class Solution {
public List<List<Integer>> fourSum(int[] nums,int target){
/*定义一个返回值*/
List<List<Integer>> result=new ArrayList<>();
/*当数组为null或元素小于4个时,直接返回*/
if(nums==null||nums.length<4){
return result;
}
/*对数组进行从小到大排序*/
Arrays.sort(nums);
/*数组长度*/
int length=nums.length;
/*定义4个指针k,i,j,h k从0开始遍历,i从k+1开始遍历,留下j和h,j指向i+1,h指向数组最大值*/
for(int k=0;k<length-3;k++){
/*当k的值与前面的值相等时忽略*/
if(k>0&&nums[k]==nums[k-1]){
continue;
}
/*获取当前最小值,如果最小值比目标值大,说明后面越来越大的值根本没戏*/
int min1=nums[k]+nums[k+1]+nums[k+2]+nums[k+3];
if(min1>target){
break;
}
/*获取当前最大值,如果最大值比目标值小,说明后面越来越小的值根本没戏,忽略*/
int max1=nums[k]+nums[length-1]+nums[length-2]+nums[length-3];
if(max1<target){
continue;
}
/*第二层循环i,初始值指向k+1*/
for(int i=k+1;i<length-2;i++){
/*当i的值与前面的值相等时忽略*/
if(i>k+1&&nums[i]==nums[i-1]){
continue;
}
/*定义指针j指向i+1*/
int j=i+1;
/*定义指针h指向数组末尾*/
int h=length-1;
/*获取当前最小值,如果最小值比目标值大,说明后面越来越大的值根本没戏*/
int min=nums[k]+nums[i]+nums[j]+nums[j+1];
if(min>target){
break;
}
/*获取当前最大值,如果最大值比目标值小,说明后面越来越小的值根本没戏,忽略*/
int max=nums[k]+nums[i]+nums[h]+nums[h-1];
if(max<target){
continue;
}
/*开始j指针和h指针的表演,计算当前和,如果等于目标值,j++并去重,h--并去重,当当前和大于目标值时h--,当当前和小于目标值时j++*/
while (j<h){
int curr=nums[k]+nums[i]+nums[j]+nums[h];
if(curr==target){
result.add(Arrays.asList(nums[k],nums[i],nums[j],nums[h]));
j++;
while(j<h&&nums[j]==nums[j-1]){
j++;
}
h--;
while(j<h&&i<h&&nums[h]==nums[h+1]){
h--;
}
}else if(curr>target){
h--;
}else {
j++;
}
}
}
}
return result;
}
作者:you-wei-wu
链接:https://leetcode-cn.com/problems/4sum/solution/ji-bai-9994de-yong-hu-you-dai-ma-you-zhu-shi-by-yo/
来源:力扣(LeetCode)
快慢指针
环形链表(simple难度)
https://leetcode-cn.com/problems/linked-list-cycle/
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null) {
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/linked-list-cycle/solution/huan-xing-lian-biao-by-leetcode-solution/
来源:力扣(LeetCode)
判断环的长度:
快慢指针相遇后继续移动,直到第二次相遇。两次相遇间的移动次数即为环的长度。
环形链表Ⅱ(medium难度)
https://leetcode-cn.com/problems/linked-list-cycle-ii/
本方法思路和代码来源:
作者:jyd
链接:https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/linked-list-cycle-ii-kuai-man-zhi-zhen-shuang-zhi-/
来源:力扣(LeetCode)
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;
while (true) {
if (fast == null || fast.next == null) return null;
fast = fast.next.next;
slow = slow.next;
if (fast == slow) break;
}
fast = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return fast;
}
}
作者:jyd
链接:https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/linked-list-cycle-ii-kuai-man-zhi-zhen-shuang-zhi-/
来源:力扣(LeetCode)
重排链表(medium难度)
https://leetcode-cn.com/problems/reorder-list/
本题方法思路和代码来源:
作者:windliang
链接:https://leetcode-cn.com/problems/reorder-list/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-34/
来源:力扣(LeetCode)
public void reorderList(ListNode head) {
if (head == null) {
return;
}
//存到 list 中去
List<ListNode> list = new ArrayList<>();
while (head != null) {
list.add(head);
head = head.next;
}
//头尾指针依次取元素
int i = 0, j = list.size() - 1;
while (i < j) {
list.get(i).next = list.get(j);
i++;
//偶数个节点的情况,会提前相遇
if (i == j) {
break;
}
list.get(j).next = list.get(i);
j--;
}
list.get(i).next = null;
}
作者:windliang
链接:https://leetcode-cn.com/problems/reorder-list/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-34/
来源:力扣(LeetCode)
解法二:(快慢指针 + 翻转链表 + 合并链表)
主要是三步,举个例子:
-
第一步找中点:可以应用快慢指针。快指针一次走两步,慢指针一次走一步,当快指针走到终点的话,慢指针会刚好到中点。如果节点个数是偶数的话,
slow
走到的是左端点,利用这一点,我们可以把奇数和偶数的情况合并,不需要分开考虑。 -
第二步链表逆序:有迭代和递归的两种方式,迭代的话主要利用两个指针,依次逆转。
-
第三步:个指针分别向后移动就可以。
public void reorderList(ListNode head) {
if (head == null || head.next == null || head.next.next == null) {
return;
}
//找中点,链表分成两个
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode newHead = slow.next;
slow.next = null;
//第二个链表倒置
newHead = reverseList(newHead);
//链表节点依次连接
while (newHead != null) {
ListNode temp = newHead.next;
newHead.next = head.next;
head.next = newHead;
head = newHead.next;
newHead = temp;
}
}
private ListNode reverseList(ListNode head) {
if (head == null) {
return null;
}
ListNode tail = head;
head = head.next;
tail.next = null;
while (head != null) {
ListNode temp = head.next;
head.next = tail;
tail = head;
head = temp;
}
return tail;
}
作者:windliang
链接:https://leetcode-cn.com/problems/reorder-list/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-34/
环形数组是否存在循环(medium难度)
https://leetcode-cn.com/problems/circular-array-loop/
本题方法和代码来源:
作者:yy2509
链接:https://leetcode-cn.com/problems/circular-array-loop/solution/shu-zu-xun-huan-lian-biao-you-huan-by-yy2509/
来源:力扣(LeetCode)
class Solution {
public boolean circularArrayLoop(int[] nums) {
if(nums.length == 0 || nums.length == 1 || nums == null) return false;//处理特殊情况
for(int i = 0; i < nums.length; i++){
//慢指针j从i的位置开始移动(类似 slow = node),快指针k从i移动一次以后的位置开始移动(类似 fast =node.next)
int j = i, k = next(nums, i);
//保证快慢指针始终向着nums[i]表示的同一个方向移动,>0始终右,反之始终左
while(nums[i]*nums[j] > 0 && nums[i]*nums[k] > 0 && nums[i]*nums[next(nums,k)] > 0){
//System.out.println("i"+i);
if(j == k){//快慢指针相遇(在循环中相遇,类似于在环形链表中相遇)
if(j == next(nums, j)){break;}//循环长度未1不能true
return true;
}
j = next(nums,j);//移动慢指针 类似于slow=slow.next;
//System.out.println("j"+j);
k = next(nums,next(nums,k));//移动快指针,类似于fast= fast.next.next;
//System.out.println("k"+k);
}
}
return false;
}
//计算下一个位置时,避免越界
private int next(int[] nums, int i){
int next = i + nums[i];
if(next >= 0) return next%nums.length; //合并0~nums.length的部分
else {return nums.length+next%nums.length;}
}
}
作者:yy2509
链接:https://leetcode-cn.com/problems/circular-array-loop/solution/shu-zu-xun-huan-lian-biao-you-huan-by-yy2509/
来源:力扣(LeetCode)
快乐数(simple难度)
https://leetcode-cn.com/problems/happy-number/
class Solution {
private int getNext(int n) {
int totalSum = 0;
while (n > 0) {
int d = n % 10;
n = n / 10;
totalSum += d * d;
}
return totalSum;
}
public boolean isHappy(int n) {
Set<Integer> seen = new HashSet<>();
while (n != 1 && !seen.contains(n)) {
seen.add(n);
n = getNext(n);
}
return n == 1;
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/happy-number/solution/kuai-le-shu-by-leetcode-solution/
来源:力扣(LeetCode)
class Solution {
public int getNext(int n) {
int totalSum = 0;
while (n > 0) {
int d = n % 10;
n = n / 10;
totalSum += d * d;
}
return totalSum;
}
public boolean isHappy(int n) {
int slowRunner = n;
int fastRunner = getNext(n);
while (fastRunner != 1 && slowRunner != fastRunner) {
slowRunner = getNext(slowRunner);
fastRunner = getNext(getNext(fastRunner));
}
return fastRunner == 1;
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/happy-number/solution/kuai-le-shu-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
同向双指针、滑动窗口
删除有序数组中的重复项(simple难度)
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
本题思路及代码来源:
作者:AC_OIer
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/solution/shua-chuan-lc-jian-ji-shuang-zhi-zhen-ji-2eg8/
来源:力扣(LeetCode)
class Solution {
public int removeDuplicates(int[] nums) {
return process(nums, 1);
}
int process(int[] nums, int k) {
int idx = 0;
for (int x : nums) {
if (idx < k || nums[idx - k] != x) nums[idx++] = x;
}
return idx;
}
}
作者:AC_OIer
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/solution/shua-chuan-lc-jian-ji-shuang-zhi-zhen-ji-2eg8/
来源:力扣(LeetCode)
删除有序数组中的重复项Ⅱ(medium难度)
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/
本题思路和代码来源:
作者:AC_OIer
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/solution/gong-shui-san-xie-guan-yu-shan-chu-you-x-glnq/
来源:力扣(LeetCode)
class Solution {
public int removeDuplicates(int[] nums) {
return process(nums, 2);
}
int process(int[] nums, int k) {
int u = 0;
for (int x : nums) {
if (u < k || nums[u - k] != x) nums[u++] = x;
}
return u;
}
}
作者:AC_OIer
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/solution/gong-shui-san-xie-guan-yu-shan-chu-you-x-glnq/
来源:力扣(LeetCode)
移除元素(simple难度)
思路一:
代码:
class Solution {
public int removeElement(int[] nums, int val) {
//记录不等于val的元素应该被保存的位置
int index = 0;
//遍历nums数组
for(int i = 0; i < nums.length; i++){
//遇到等于val的元素则跳过
if(nums[i] == val){
continue;
}
//将等于val的元素记录在数组,实现原地修改
nums[index++] = nums[i];
}
return index;
}
}
作者:HIT_whc
链接:https://leetcode-cn.com/problems/remove-element/solution/javashuang-zhi-zhen-jian-yi-jie-fa-shi-j-ywyt/
来源:力扣(LeetCode)
思路二:
本方法思路和代码来源:
作者:AC_OIer
链接:https://leetcode-cn.com/problems/remove-element/solution/shua-chuan-lc-shuang-bai-shuang-zhi-zhen-mzt8/
来源:力扣(LeetCode)
class Solution {
public int removeElement(int[] nums, int val) {
int j = nums.length - 1;
for (int i = 0; i <= j; i++) {
if (nums[i] == val) {
swap(nums, i--, j--);
}
}
return j + 1;
}
void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
作者:AC_OIer
链接:https://leetcode-cn.com/problems/remove-element/solution/shua-chuan-lc-shuang-bai-shuang-zhi-zhen-mzt8/
来源:力扣(LeetCode)
删除排序链表中的重复元素(simple难度)
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
本方法思路及代码来源:
作者:guanpengchn
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/solution/hua-jie-suan-fa-83-shan-chu-pai-xu-lian-biao-zhong/
来源:力扣(LeetCode)
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode cur = head;
while(cur != null && cur.next != null) {
if(cur.val == cur.next.val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return head;
}
}
作者:guanpengchn
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/solution/hua-jie-suan-fa-83-shan-chu-pai-xu-lian-biao-zhong/
来源:力扣(LeetCode)
另一种代码:
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) return head;
ListNode dummy = new ListNode(-109);
ListNode tail = dummy;
while (head != null) {
// 值不相等才追加,确保了相同的节点只有第一个会被添加到答案
if (tail.val != head.val) {
tail.next = head;
tail = tail.next;
}
head = head.next;
}
tail.next = null;
return dummy.next;
}
}
作者:AC_OIer
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/solution/tong-yong-shan-chu-zhong-fu-jie-dian-lia-od9g/
来源:力扣(LeetCode)
删除排序链表中的重复元素Ⅱ(medium难度)
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/
本题方法思路及代码来源:
作者:AC_OIer
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/solution/tong-yong-shan-chu-zhong-fu-jie-dian-lia-od9g/
来源:力扣(LeetCode)
代码:
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode();
ListNode tail = dummy;
while (head != null) {
// 进入循环时,确保了 head 不会与上一节点相同
if (head.next == null || head.val != head.next.val) {
tail.next = head;
tail = head;
}
// 如果 head 与下一节点相同,跳过相同节点
while (head.next != null && head.val == head.next.val) head = head.next;
head = head.next;
}
tail.next = null;
return dummy.next;
}
}
作者:AC_OIer
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/solution/tong-yong-shan-chu-zhong-fu-jie-dian-lia-od9g/
来源:力扣(LeetCode)
长度最小的子数组(medium难度)
https://leetcode-cn.com/problems/minimum-size-subarray-sum/
代码:
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int sum = 0;
//答案可能取到的最大值为nums.length,初始化len为答案可能取到的最大值+1
int len = nums.length + 1;
//滑动窗口的前指针
int pre = 0;
for (int i = 0;i < nums.length; i++) {
sum += nums[i];
if (sum < target) {
continue;
} else { //sum >= target
//sum >= target以后开始缩小窗口,直至sum < target 或 pre > i
while(sum >= target && pre <= i){
sum -= nums[pre++];
}
//窗口长度为(i - pre + 1)
//因为窗口循环缩小后不满足sum >= target,所以将此时窗口长度+1则满足要求
//len的值因为缩小后的窗口的值+1,即(i - pre + 1) + 1
len = Math.min(len,(i - pre + 1)+1);
}
}
return len != nums.length + 1 ? len : 0;
}
}
作者:HIT_whc
链接:https://leetcode-cn.com/problems/minimum-size-subarray-sum/solution/javahua-dong-chuang-kou-jie-fa-shi-jian-w0c11/
来源:力扣(LeetCode)
字符串的排列(medium难度)
https://leetcode-cn.com/problems/permutation-in-string/
11
class Solution {
public boolean checkInclusion(String s1, String s2) {
int n = s1.length(), m = s2.length();
if (n > m) {
return false;
}
int[] cnt1 = new int[26];
int[] cnt2 = new int[26];
for (int i = 0; i < n; ++i) {
++cnt1[s1.charAt(i) - 'a'];
++cnt2[s2.charAt(i) - 'a'];
}
if (Arrays.equals(cnt1, cnt2)) {
return true;
}
for (int i = n; i < m; ++i) {
++cnt2[s2.charAt(i) - 'a'];
--cnt2[s2.charAt(i - n) - 'a'];
if (Arrays.equals(cnt1, cnt2)) {
return true;
}
}
return false;
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/permutation-in-string/solution/zi-fu-chuan-de-pai-lie-by-leetcode-solut-7k7u/
来源:力扣(LeetCode)
class Solution {
public boolean checkInclusion(String s1, String s2) {
int n = s1.length(), m = s2.length();
if (n > m) {
return false;
}
int[] cnt = new int[26];
for (int i = 0; i < n; ++i) {
--cnt[s1.charAt(i) - 'a'];
++cnt[s2.charAt(i) - 'a'];
}
int diff = 0;
for (int c : cnt) {
if (c != 0) {
++diff;
}
}
if (diff == 0) {
return true;
}
for (int i = n; i < m; ++i) {
int x = s2.charAt(i) - 'a', y = s2.charAt(i - n) - 'a';
if (x == y) {
continue;
}
if (cnt[x] == 0) {
++diff;
}
++cnt[x];
if (cnt[x] == 0) {
--diff;
}
if (cnt[y] == 0) {
++diff;
}
--cnt[y];
if (cnt[y] == 0) {
--diff;
}
if (diff == 0) {
return true;
}
}
return false;
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/permutation-in-string/solution/zi-fu-chuan-de-pai-lie-by-leetcode-solut-7k7u/
来源:力扣(LeetCode)
class Solution {
public boolean checkInclusion(String s1, String s2) {
int n = s1.length(), m = s2.length();
if (n > m) {
return false;
}
int[] cnt = new int[26];
for (int i = 0; i < n; ++i) {
--cnt[s1.charAt(i) - 'a'];
}
int left = 0;
for (int right = 0; right < m; ++right) {
int x = s2.charAt(right) - 'a';
++cnt[x];
while (cnt[x] > 0) {
--cnt[s2.charAt(left) - 'a'];
++left;
}
if (right - left + 1 == n) {
return true;
}
}
return false;
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/permutation-in-string/solution/zi-fu-chuan-de-pai-lie-by-leetcode-solut-7k7u/
来源:力扣(LeetCode)