力扣刷题记录_双指针法(自学)

1、移除元素(力扣27)

	//1.双指针
    public static int removeElement(int[] nums, int val) {

        int left = 0,right = nums.length - 1;
        while (left <= right){
            if (nums[left] == val){
                nums[left] = nums[right];
                right --;
            }else{
                left ++;
            }
        }

        //或者是返回 right + 1;
        return left;

    }

	//2.双指针(快慢指针)
    //找到一个不等于val的,就赋值到数组前面,slowIndex记录个数
    //两个指针都从左端出发
    public static int removeElement(int[] nums, int val) {

        int slowIndex = 0;
        int fastIndex;
        for (fastIndex = 0;fastIndex < nums.length;fastIndex ++){
            if (nums[fastIndex] != val){
                nums[slowIndex] = nums[fastIndex];
                slowIndex ++;
            }
        }

        return slowIndex;

    }

2、删除有序数组中的重复项(力扣26)

//快慢指针
    public int removeDuplicates(int[] nums) {
        int slow = 0,fast = 0;
        while(fast < nums.length){
            if(nums[slow] != nums[fast]){
                slow ++;
                nums[slow] = nums[fast];
            }
            fast ++;
        }
        return slow + 1;
    }

3、移动零(力扣283)

	//快慢指针
	public void moveZeroes(int[] nums) {
        int slow = 0,fast = 0;
        //每找到一个不为0的数,就将这个数移动到前面
        while(fast < nums.length){
            if(nums[fast] != 0){
                nums[slow] = nums[fast];
                slow ++;
            }
            fast ++;
        }
		//整个数组中不为0的数都已经移动到了前面,后面的部分直接赋值为0
        while(slow < nums.length){
            nums[slow ++] = 0;
        }

    }

4、比较含退格的字符串(力扣844)

//双指针 
    //两个指针分别在两个字符串的最后一个字符开始
    public static boolean backspaceCompare(String s, String t) {
        //实时记录指针的位置
        int i = s.length() - 1, j = t.length() - 1;
        //分别记录#出现的次数
        int countS = 0, countT = 0;

        while (i >= 0 || j >= 0) {
            while (i >= 0) {
                //如果当前字符是'#',计数
                if (s.charAt(i) == '#') {
                    countS++;
                    i--;
                    //如果当前字符不是'#',消耗掉'#'
                } else if (countS > 0) {
                    countS--;
                    //通过i--实现退格
                    i--;
                    //说明当前字符是保留下来的,直接跳出,进行比较
                } else {
                    break;
                }
            }
            while (j >= 0) {
                if (t.charAt(j) == '#') {
                    countT++;
                    j--;
                } else if (countT > 0) {
                    countT--;
                    j--;
                } else {
                    break;
                }
            }
            //返回false的两种情况:
            //1.从后往前进行比较,如果不相等,直接返回false
            if (i >= 0 && j >= 0) {
                if (s.charAt(i) != t.charAt(j)) {
                    return false;
                }
            } else {
                //2.一个已经遍历完成,一个还没遍历完,直接返回false
                if (i >= 0 || j >= 0) {
                    return false;
                }
            }
            //及时调整指针,遍历字符串
            i--;
            j--;
        }
        return true;
    }

5、有序数组的平方(力扣977)

	public int[] sortedSquares(int[] nums) {
        int len = nums.length;
        int[] res = new int[len];
        int index = len - 1;
		//从两边往中间,平方的最大值必然出现在数组的两端
        int left = 0,right = len - 1;
        while (left <= right){
            if (nums[left] * nums[left] >= nums[right] * nums[right]){
                res[index --] = nums[left] * nums[left];
                left ++;
            }else{
                res[index --] = nums[right] * nums[right];
                right --;
            }
        }

        return res;
    }

6、反转字符串(力扣344)

	public void reverseString(char[] s) {
        int left = 0,right = s.length - 1;
        while(left < right){
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left ++;
            right --;
        }
    }

7、反转链表(力扣206)

	public ListNode reverseList(ListNode head) {
        ListNode preNode = null;
        ListNode curNode = head;
        while (curNode != null){
            //在更改引用之前,还需要另一个指针来存储下一个节点
            ListNode nextNode = curNode.next;
            //在遍历列表时,将当前节点的next指针改为指向前一个元素
            curNode.next = preNode;
            preNode = curNode;
            curNode = nextNode;
        }
        //不要忘记在最后返回新的头引用!
        //此时curNode == null,preNode指向最后一个元素
        return preNode;
    }

8、 删除链表的倒数第 N 个结点(力扣19)

//1.双指针(自己想的)
    //让快指针和慢指针相差n步,当快指针到达链表末尾时,慢指针刚好到达要删除的结点的前一个结点
    public static ListNode removeNthFromEnd(ListNode head, int n) {
        //设置哨兵结点,保留头结点信息
        ListNode node = new ListNode(0);
        node.next = head;
        ListNode first = node;
        ListNode second = node;
        int count = 0;
        while (first.next != null) {
            if (count >= n) {
                second = second.next;
            }
            first = first.next;
            count++;
        }

        second.next = second.next.next;

        return node.next;

    }

//2.双指针
    /*
     * 初始时,first -> head,second -> node(便于节点删除),让first先走n步,这时first和second之间
     * 相差n+1步,再让first和second一起走,当first=null时,second到达被删除节点的前
     * 一个节点。
     * */
    public static ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode node = new ListNode(0, head);
        ListNode first = head, second = node;
        for (int i = 0; i < n; i++) {
            first = first.next;
        }

        while (first != null) {
            first = first.next;
            second = second.next;
        }

        second.next = second.next.next;
        return node.next;
    }

9、 链表相交(面试题 02.07)

//1.设交集链表长c,链表1除交集的长度为a,链表2除交集的长度为b,有:
    //1.有交集,a + c + b = b + c + a
    //2.无交集,则a + b = b + a
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        ListNode node1 = headA;
        ListNode node2 = headB;

        while (node1 != node2) {
            node1 = (node1 == null) ? headB : node1.next;
            node2 = (node2 == null) ? headA : node2.next;
        }

        //如果有交集,返回交点,如果无交集,返回null
        return node1;

    }

10、环形链表 II(力扣142)

//快慢指针
    /*
     * 两个指针第一次相遇之后,将fast指针重新指向head,当两指针再次相遇时,此节点即为入口节点
     * */
    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 (slow == fast) {
                break;
            }
        }
        //调整fast指针
        fast = head;
        //再次相遇
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }

        return fast;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值