【算法实战】双指针

文章目录

    • [剑指 Offer 18. 删除链表的节点 - 力扣(LeetCode)](https://leetcode.cn/problems/shan-chu-lian-biao-de-jie-dian-lcof/)
    • [剑指 Offer 22. 链表中倒数第k个节点 - 力扣(LeetCode)](https://leetcode.cn/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/)
    • [剑指 Offer 25. 合并两个排序的链表 - 力扣(LeetCode)](https://leetcode.cn/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/)
    • * [剑指 Offer 52. 两个链表的第一个公共节点 - 力扣(LeetCode)](https://leetcode.cn/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/submissions/)
    • [剑指 Offer 21. 调整数组顺序使奇数位于偶数前面 - 力扣(LeetCode)](https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/submissions/)
    • [剑指 Offer 57. 和为s的两个数字 - 力扣(LeetCode)](https://leetcode.cn/problems/he-wei-sde-liang-ge-shu-zi-lcof/)
    • *[剑指 Offer 58 - I. 翻转单词顺序 - 力扣(LeetCode)](https://leetcode.cn/problems/fan-zhuan-dan-ci-shun-xu-lcof/)

剑指 Offer 18. 删除链表的节点 - 力扣(LeetCode)

法一:双指针法

	ListNode pre = head;
	ListNode temp = head.next;
	if(head.val == val){
	    head = head.next;
	    return head;
	}
	while(temp != null){
	    if(temp.val == val){
	        break;
	    }
	    temp = temp.next;
	    pre = pre.next;
	}
	pre.next = temp.next;
	return head;

法二:添加额外头节点法

设置一个新的头节点指向链表头节点。

	ListNode newhead = new ListNode(-1);
	newhead.next = head;
	ListNode temp = newhead;
	while(temp!= null && temp.next != null){
	    if(temp.next.val == val){
	        temp.next = temp.next.next;
	    }else{
	        temp = temp.next;
	    }
	}
	return newhead.next;

剑指 Offer 22. 链表中倒数第k个节点 - 力扣(LeetCode)

双指针法:

ListNode newhead = head;
ListNode temp = head;
while(k-- > 0){
    temp = temp.next;
}
while(temp != null){
    temp = temp.next;
    newhead = newhead.next;
}
return newhead;

剑指 Offer 25. 合并两个排序的链表 - 力扣(LeetCode)

双指针法

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode t1 = l1;
        ListNode t2 = l2;
        ListNode newhead = new ListNode(-1);
        ListNode temp = newhead;
        while(t1 != null && t2 != null){
            if(t1.val > t2.val){
                temp.next = t2;
                t2 = t2.next;
            }else{
                temp.next = t1;
                t1 = t1.next;
            }
            temp = temp.next;
        }
        if(t1 != null){
            temp.next = t1;
        }else{
            temp.next = t2;
        }
        return newhead.next;
    }
}

* 剑指 Offer 52. 两个链表的第一个公共节点 - 力扣(LeetCode)

【算法实战】双指针_第1张图片

class Solution {
    ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pA = headA;
        ListNode pB = headB;
        while(pA != pB){
            if(pA == null){
                pA = headB;
            }else{
                pA = pA.next;
            }
            if(pB == null){
                pB = headA;
            }else{
                pB = pB.next;
            }
        }
        return pA;
    }
}

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面 - 力扣(LeetCode)

左右指针

class Solution {
    public int[] exchange(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        while(left < right){
            //两边都不符合条件,交换两边
            if(nums[left] % 2 == 0 && nums[right] % 2 != 0){
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
            }
            //说明左边是奇数,该数符合条件
            if(nums[left] % 2 != 0){
                left ++;
            }
            //说明右边是偶数,该数符合条件
            if(nums[right] % 2 == 0){
                right --;
            }
        }
        return nums;
    }
}

剑指 Offer 57. 和为s的两个数字 - 力扣(LeetCode)

左右指针

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while(left < right){
            int sum = nums[left] + nums[right];
            if(sum > target){
                right --;
            }else if(sum < target){
                left ++;
            }else{
                return new int []{nums[left],nums[right]};
            }
        }
        return new int []{};
    }
}

*剑指 Offer 58 - I. 翻转单词顺序 - 力扣(LeetCode)

法一:步骤拆分:

  1. 删除多余空格
  2. 全部反转
  3. 单个单词反转
class Solution {
    StringBuffer sb = new StringBuffer();
    public String reverseWords(String s) {
        //删除多余空格
        del(s);
        //全部反转
        reverseStr(sb,0,sb.length() - 1);
        //单个单词反转
        reverseWord(sb);
        return sb.toString();
    }
    // 去除多余的空格 ——> 先清除两边的空格,在清除中间连续的空格
        //清除两边的空格——>使用双指针。
        //清除中间连续的空格——> 上一个不是空格,追加。
    void del(String s){
        char [] ch = s.toCharArray();
        int start = 0;
        int end = s.length() - 1;
        while(start < ch.length && ch[start] == ' ') start ++;
        while(end >= 0 && ch[end] == ' ') end --;
        for(int i = start ; i <= end ; i ++){
            if(ch[i] != ' ' || ch[i - 1] != ' '){
                sb.append(ch[i]);
            }
        }
    }
    void reverseStr(StringBuffer s,int left , int right){
        while(left < right){
            char temp = s.charAt(left);
            s.setCharAt(left , s.charAt(right));
            s.setCharAt(right , temp);
            left ++;
            right --;
        }
    }
    //翻转单个单词
    void reverseWord(StringBuffer s){
        int left = 0;
        int right = 0;
        while(left < s.length()){
            while(right < s.length() && sb.charAt(right) != ' '){
                right ++;
            }
            reverseStr(s,left,right - 1);
            left = right + 1;
            right = left + 1;
        }
    }
}

法二:双指针,利用 i 和 j 倒序遍历来反转字符串。

class Solution {
    public String reverseWords(String s) {
        //去除首位空格
        s.trim();
        int i = s.length() - 1;
        int j = i;
        StringBuffer sb = new StringBuffer();
        while(i >= 0){
            //找到第一个空格
            while(i >= 0 && s.charAt(i) != ' ') i --;
            //追加
            sb.append(s.substring(i + 1,j + 1) + " ");
            //跳过中间连续的空格
            while(i >= 0 && s.charAt(i) == ' ') i --;
            
            j = i;
        }
        return sb.toString().trim();
    }
}

你可能感兴趣的:(算法实战专栏,算法,链表,java)