LeetCode刷题日记精选例题-双指针经典问题总结

文章目录

  • 一、移除数组元素
  • 二、反转字符串
  • 三、替换空格(剑指Offer)
  • 四、翻转字符串里的单词
  • 五、翻转链表
  • 六、删除链表的倒数第n个结点
  • 七、链表相交
  • 八、环形链表
  • 九、三数之和
  • 十、四数之和

一、移除数组元素

题目链接
LeetCode刷题日记精选例题-双指针经典问题总结_第1张图片
LeetCode刷题日记精选例题-双指针经典问题总结_第2张图片

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

二、反转字符串

LeetCode刷题日记精选例题-双指针经典问题总结_第3张图片
题目链接

    public static void reverseString(char[] s) {
     
        for (int i=0,j=s.length-1;i<s.length/2;i++,j--){
     
            char c = s[i];
            s[i]=s[j];
            s[j]=c;
        }
    }

三、替换空格(剑指Offer)

LeetCode刷题日记精选例题-双指针经典问题总结_第4张图片
题目链接

    public static String replaceSpace(String s) {
     
        if (s.length()==0){
     
            return null;
        }
        StringBuilder ss = new StringBuilder();
        for (int i=0;i<s.length();i++){
     
            if (s.charAt(i)==' '){
     
                ss.append("  ");
            }
        }
        int left = s.length();
        s+=new String(ss);
        int right = s.length();

        char[] chars = s.toCharArray();
        while (left>=0){
     
            if (chars[right]==' '){
     
                chars[right--]='0';
                chars[right--]='2';
                chars[right]='%';
            }else {
     
                chars[right]=chars[left];
            }
            left--;
            right--;
        }
        return String.valueOf(chars);
    }

四、翻转字符串里的单词

LeetCode刷题日记精选例题-双指针经典问题总结_第5张图片
LeetCode刷题日记精选例题-双指针经典问题总结_第6张图片
题目链接

    public static String reverseWords(String s) {
     
        if (s==null){
     
            return null;
        }
        String trim = s.trim();
        StringBuilder str = new StringBuilder();
        int left = trim.length()-1;
        int right = trim.length()-1;
        while (left>=0){
     
            if (left>0&&' '==trim.charAt(left-1)){
     
                str.append(trim.substring(left,right+1));
                while (' '==trim.charAt(left-1)){
     
                    left--;
                }
            }else if (' '==trim.charAt(left)){
     
                str.append(" ");
                left--;
                right=left;
            }else if (left==0){
     
                str.append(trim.substring(left,right+1));
                left--;
            }
            else  {
     
                left--;
            }
        }
        return new String(str);
    }

五、翻转链表

LeetCode刷题日记精选例题-双指针经典问题总结_第7张图片
LeetCode刷题日记精选例题-双指针经典问题总结_第8张图片
题目链接

    public static ListNode reverseList(ListNode head){
     
        if (head==null){
     
            return null;
        }
        ListNode cur = head;
        ListNode temp;
        ListNode resverNode = new ListNode();
        ListNode pre = new ListNode();
        pre.next = head;
        while (cur!=null){
     
            temp = cur.next;
            cur.next = resverNode.next;
            resverNode.next=cur;
            cur=temp;
        }
        pre.next = resverNode.next;
        return pre.next;
    }

六、删除链表的倒数第n个结点

LeetCode刷题日记精选例题-双指针经典问题总结_第9张图片
题目链接

双指针解题 设立两个指针,第一个指针向前移动n步,随后两个指针同时移动,当第一个指针到达末尾时,第二个指针刚好到达第n个位置

    public ListNode removeNthFromEnd(ListNode head, int n) {
     
        ListNode cur = head;
        ListNode pre = new ListNode(-1);
        pre.next = cur;
        ListNode fast = head;
        for (int i=0;i<n;i++){
     
            fast = fast.next;
        }
        ListNode p = new ListNode();
        while (fast.next!=null){
     
            p=cur;
            cur = cur.next;
            fast = fast.next;
        }
        //此时cur为要删除的位置,pre为要删除位置的前一个位置
        p.next = cur.next;
        return pre.next;
    }

七、链表相交

LeetCode刷题日记精选例题-双指针经典问题总结_第10张图片
LeetCode刷题日记精选例题-双指针经典问题总结_第11张图片
LeetCode刷题日记精选例题-双指针经典问题总结_第12张图片
题目链接

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
     
        int lengthA = getLinkedLength(headA);
        int lengthB = getLinkedLength(headB);
        ListNode curA = headA;
        ListNode curB = headB;
        if (lengthB>lengthA){
     
            int temp = lengthA;
            lengthA = lengthB;
            lengthB = temp;

            ListNode p = curA;
            curA = curB;
            curB = p;
        }

        int L = lengthA-lengthB;
        for (int i=0;i<L;i++){
     
            curA = curA.next;
        }
        while (curA!=null){
     
            if (curA==curB){
     
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }
        return null;
    }

    private int getLinkedLength(ListNode head) {
     
        int length = 0;
        ListNode cur = head;
        while (cur!=null){
     
            length++;
            cur=cur.next;
        }
        return length;
    }

八、环形链表

LeetCode刷题日记精选例题-双指针经典问题总结_第13张图片
LeetCode刷题日记精选例题-双指针经典问题总结_第14张图片
题目链接

        public ListNode detectCycle(ListNode head) {
     
            if (head==null){
     
                return null;
            }
            ListNode fast = head;
            ListNode slow = head;
            //快指针走2步,慢指针走1步
            while (fast!=null&&fast.next!=null){
     
                fast = fast.next.next;
                slow = slow.next;
                if (fast==slow){
     
                    ListNode node = head;
                    while (node!=fast){
     
                        fast = fast.next;
                        node = node.next;
                    }
                    return node;
                }
            }
            return null;
        }

九、三数之和

LeetCode刷题日记精选例题-双指针经典问题总结_第15张图片
题目链接

    public static List<List<Integer>> threeSum(int[] nums) {
     
        Arrays.sort(nums);
        List<List<Integer>> list = new ArrayList<>();
        //-4,-1,-1,0,1,2
        for (int i=0;i<nums.length-2;i++){
     
            int left = i+1;
            int right = nums.length-1;
            while (left<right){
     
                int value = 0-(nums[i]+nums[left]+nums[right]);
                if (value==0){
     
                    list.add(Arrays.asList(nums[i],nums[left],nums[right]));
                    while (right>left&&nums[right]==nums[right-1]) right--;
                    while (right>left&&nums[left]==nums[left+1]) left++;
                    right--;
                    left++;
                }else if (value>0){
     
                    right--;
                }else{
     
                    left++;
                }
            }
        }
        return list;
    }

十、四数之和

LeetCode刷题日记精选例题-双指针经典问题总结_第16张图片
题目链接

    public static List<List<Integer>> fourSum(int[] nums, int target) {
     
        Arrays.sort(nums);
        List<List<Integer>> list = new ArrayList<>();
        if (nums==null||nums.length<4){
     
            return list;
        }
        for (int i=0;i<nums.length-3;i++){
     
            if (i>0&&nums[i]==nums[i-1]){
     
                continue;
            }
            for (int j=i+1;j<nums.length-2;j++){
     
                if (j-1>i&&nums[j]==nums[j-1]){
     
                    continue;
                }
                int left = j+1;
                int right = nums.length-1;
                while (left<right){
     
                    int value =(nums[i]+nums[j]+nums[left]+nums[right]);
                    if (value>target){
     
                        right--;
                    }else if (value<target){
     
                        left++;
                    }else {
     
                        list.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
                        while (right>left&&nums[right]==nums[right-1]) right--;
                        while (right>left&&nums[left]==nums[left+1]) left++;
                        right--;
                        left++;
                    }
                }
            }
        }
        return list;
    }

你可能感兴趣的:(数据结构与算法,LeetCode,数据结构,java,leetcode,算法,面试)