【一天三道算法题】代码随想录——Day12(含面试题)

一. 链表相交(面试题)

题目链接:力扣

思路:第二次看到噜——二刷,还是原思路,双指针 保持在焦点之前,俩指针同步进行遍历

代码:

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode curA = headA;
        ListNode curB = headB;

        int lenA = 0;
        int lenB = 0;
        //记录俩链表长度
        while(curA != null) {
            curA = curA.next;
            lenA++;
        }
        while(curB != null) {
            curB = curB.next;
            lenB++;
        }

        //让俩指针重回原点
        curA = headA;
        curB = headB;

        //让a保持最长
        if(lenA < lenB) {
            //交换长度
            int temp = lenA;
            lenA = lenB;
            lenB = temp;

            //交换结点
            ListNode tempCur = curA;
            curA = curB;
            curB = tempCur;
        }
        //求长度差
        int gap = lenA - lenB;

        //a先移动,让a和b保持同步移动
        while(gap-- > 0) {
            curA = curA.next;
        }
        //遍历curA和curB,遇到值相同则直接返回
        while(curA != null) {
            if(curA == curB){
                return curA;
            }
            //++实现遍历
            curA = curA.next;
            curB = curB.next;
        }
        //若未返回,返回null
        return null;
    }
}

二. 环形链表 II

题目链接:力扣

思路:也是二刷啦,双指针真是永远的神吧!

代码:

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;

        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                ListNode fastIndex = fast;
                ListNode slowIndex = head;

                while(fastIndex != slowIndex) {
                    fastIndex = fastIndex.next;
                    slowIndex = slowIndex.next;
                }
                return fastIndex;
            }
        }
        return null;
    }
}

三. 三数之和

题目链接:力扣

思路:都是二刷啊,好爽!!确实感觉到自己的进步了,加油加油!

代码:

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);

        for(int i = 0; i<nums.length; i++) {
            if(nums[i] > 0) {
                return result;
            }

            if(i > 0 && nums[i] == nums[i-1]) {
                continue;
            }

            int left = i + 1;
            int right = nums.length - 1;

            while(right > left) {
                int sum = nums[i] + nums[left] + nums[right];
                if(sum > 0) {
                    right--;
                } else if (sum < 0) {
                    left++;
                } else {
                    result.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++;
                }
            }
        }
        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值