力扣算法题简单(一)

    1. 环形链表
    public bool HasCycle(ListNode head)
    {
        if (head == null) return false;

        HashSet<ListNode> bill = new HashSet<ListNode>();

        while (head != null)
        {
            if (bill.Contains(head))
            {
                return true;
            }
            else
            {
                bill.Add(head);
            }
            head = head.next;
        }
        return false;
    }
    1. 删除排序数组中的重复项
    public int RemoveDuplicates(int[] nums)
    {
        if (nums.Length == 0) return 0;
        int i = 0;
        for (int j = 1; j < nums.Length; j++)
        {
            if (nums[j] != nums[i])
            {
                i++;
                nums[i] = nums[j];
            }
        }

        return i + 1;
    }
    1. 合并两个有序链表
    public ListNode MergeTwoLists(ListNode l1, ListNode l2)
    {
        ListNode head = new ListNode(0);
        ListNode cur = head;
        while (l1 != null && l2 != null)
        {
            if (l1.val < l2.val)
            {
                cur.next = l1;
                cur = l1;
                l1 = l1.next;
            }
            else
            {
                cur.next = l2;
                cur = l2;
                l2 = l2.next;
            }
        }

        if (l1 == null)
        {
            cur.next = l2;
        }
        if (l2 == null)
        {
            cur.next = l1;
        }

        return head.next;
    }
    1. 最大子序和
    public int MaxSubArray(int[] nums)
    {
        if (nums.Length == 0) return 0;
        int result = nums[0];
        int current = nums[0];
        for (int i = 1; i < nums.Length; i++)
        {
            if (current < 0) current = nums[i];
            else current += nums[i];
            if (current >= result)
            {
                result = current;
            }
        }
        return result;
    }
    1. 有效的括号
    public bool IsValid(string s)
    {
        if (string.IsNullOrEmpty(s)) return true;
        Dictionary<char, char> dict = new Dictionary<char, char>();
        dict.Add(')', '(');
        dict.Add(']', '[');
        dict.Add('}', '{');
        Stack<char> stack = new Stack<char>();
        for (int i = 0; i < s.Length; i++)
        {
            if (stack.Count == 0)
            {
                stack.Push(s[i]);
                continue;
            }
            char temp;
            dict.TryGetValue(s[i], out temp);
            if (stack.Peek() == temp) stack.Pop();
            else
                stack.Push(s[i]);
        }

        if (stack.Count == 0) return true;
        else
            return false;
    }
    1. 相交链表
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        /**
        定义两个指针, 第一轮让两个到达末尾的节点指向另一个链表的头部, 最后如果相遇则为交点(在第一轮移动中恰好抹除了长度差)
        两个指针等于移动了相同的距离, 有交点就返回, 无交点就是各走了两条指针的长度
        **/
        if(headA == null || headB == null) return null;
        ListNode pA = headA, pB = headB;
        // 在这里第一轮体现在pA和pB第一次到达尾部会移向另一链表的表头, 而第二轮体现在如果pA或pB相交就返回交点, 不相交最后就是null==null
        while(pA != pB) {
            pA = pA == null ? headB : pA.next;
            pB = pB == null ? headA : pB.next;
        }
        return pA;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37811712/article/details/85716484