LeetCode热题100使用双指针的题目整理(待更)

移动零(simple难度)

https://leetcode-cn.com/problems/move-zeroes/

<方法一>

解题思路

使用双指针,左指针指向当前已经处理好的序列的尾部,右指针指向待处理序列的头部。

右指针不断向右移动,每次右指针指向非零数,则将左右指针对应的数交换,同时左指针右移。

注意到以下性质:

  1. 左指针左边均为非零数;
  2. 右指针左边直到左指针处均为零。

因此每次交换,都是将左指针的零与右指针的非零数交换,且非零数的相对顺序并未改变。

class Solution {
    public void moveZeroes(int[] nums) {
        int n = nums.length, left = 0, right = 0;
        while (right < n) {
            if (nums[right] != 0) {
                swap(nums, left, right);
                left++;
            }
            right++;
        }
    }

    public void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/move-zeroes/solution/yi-dong-ling-by-leetcode-solution/
来源:力扣(LeetCode)

<方法二>

解题思路

本题要求把0移动到数组最后且数组中其余非0数字相对位置不变。
那么我们先将数组中所有非0数字保持原来相对顺序移动到数组的前面,再把数组后面的元素全部赋为0即可。
代码实现

可以采用双指针解题,第一个指针是cur,第二个指针用for循环中的i代替。
cur指向数组前端等待赋值的位置,指针i寻找数组中非0数字。
当指针i找到非0数字后,将其赋给nums[cur],之后cur自增1,前进到下一个等待赋值的位置。
待指针i移动到数组最后的位置以后,数组遍历完毕,表示数组中所有非0数字已经按照原来的相对顺序移动到了数组的前面。数组剩余的位置全部赋0即可。

class Solution {
    public void moveZeroes(int[] nums) {
        int cur = 0;
        for(int i =0;i<nums.length;i++){
            if(nums[i] == 0){
                continue;
            }
            nums[cur] = nums[i];
            cur++;
        }
        while(cur < nums.length){
            nums[cur] = 0;
            cur++;
        }
    }
}

作者:HIT_whc
链接:https://leetcode-cn.com/problems/move-zeroes/solution/shi-yong-shuang-zhi-zhen-zhi-xing-yong-s-camx/
来源:力扣(LeetCode)

相交链表(simple难度)

https://leetcode-cn.com/problems/intersection-of-two-linked-lists/

与本题相同的题目:

剑指offer52.两个链表的第一个公共节点

<方法一>:双指针(链表拼接)

本方法思路和代码来源:
作者:jyd
链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/intersection-of-two-linked-lists-shuang-zhi-zhen-l/
来源:力扣(LeetCode)

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode ha = headA, hb = headB;
        while (ha != hb) {
            ha = ha != null ? ha.next : headB;
            hb = hb != null ? hb.next : headA;
        }
        return ha;
    }
}

作者:jyd
链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/intersection-of-two-linked-lists-shuang-zhi-zhen-l/
来源:力扣(LeetCode)

<方法二>:哈希表

本方法思路和代码来源:
作者:力扣官方题解
链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/xiang-jiao-lian-biao-by-leetcode/
来源:力扣(LeetCode)

盛最多水的容器(medium难度)

https://leetcode-cn.com/problems/container-with-most-water/

<方法>:双指针

本方法思路及代码来源:
作者:jyd
链接:https://leetcode-cn.com/problems/container-with-most-water/solution/container-with-most-water-shuang-zhi-zhen-fa-yi-do/
来源:力扣(LeetCode)

class Solution {
    public int maxArea(int[] height) {
        int i = 0, j = height.length - 1, res = 0;
        while(i < j){
            res = height[i] < height[j] ? 
                Math.max(res, (j - i) * height[i++]): 
                Math.max(res, (j - i) * height[j--]); 
        }
        return res;
    }
}

作者:jyd
链接:https://leetcode-cn.com/problems/container-with-most-water/solution/container-with-most-water-shuang-zhi-zhen-fa-yi-do/
来源:力扣(LeetCode)

找到字符串中所有字母异位词(medium难度)

https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/

<方法>:滑动窗口+左右索引指针

本方法代码和思路来源:
作者:Jasion_han
链接:https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/solution/20200321438median-by-jasion_han-r/
来源:力扣(LeetCode)

class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        char[] arrS = s.toCharArray();
        char[] arrP = p.toCharArray();
        
        // 接收最后返回的结果
        List<Integer> ans = new ArrayList<>();
        
        // 定义一个 needs 数组来看 arrP 中包含元素的个数
        int[] needs = new int[26];
        // 定义一个 window 数组来看滑动窗口中是否有 arrP 中的元素,并记录出现的个数
        int[] window = new int[26]; 
        
        // 先将 arrP 中的元素保存到 needs 数组中
        for (int i = 0; i < arrP.length; i++) {
            needs[arrP[i] - 'a'] += 1;
        }
        
        // 定义滑动窗口的两端
        int left = 0;
        int right = 0;
        
        // 右窗口开始不断向右移动
        while (right < arrS.length) {
            int curR = arrS[right] - 'a';
            right++;
            // 将右窗口当前访问到的元素 curR 个数加 1 
            window[curR] += 1;
            
            // 当 window 数组中 curR 比 needs 数组中对应元素的个数要多的时候就该移动左窗口指针 
            while (window[curR] > needs[curR]) {
                int curL = arrS[left] - 'a';
                left++;
                // 将左窗口当前访问到的元素 curL 个数减 1 
                window[curL] -= 1;            
            }
            
            // 这里将所有符合要求的左窗口索引放入到了接收结果的 List 中
            if (right - left == arrP.length) {
                ans.add(left);
            }
        }
        return ans;
    }
}

作者:Jasion_han
链接:https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/solution/20200321438median-by-jasion_han-r/
来源:力扣(LeetCode)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值