Datawhole第四天打卡

第一题:最接近的三数之和

给定一个包括n个整数的数组nums和一个目标值target。找出nums中的三个整数,使得它们的和与target最接近。返回这三个数的和。假定每组输入只存在唯一答案。

示例 :

例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.

与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).

public int ThreeSumClosest(int[] nums, int target)

    {

        nums = nums.OrderBy(a => a).ToArray();

        int result = nums[0] + nums[1] + nums[2];

        for (int i = 0; i < nums.Length - 2; i++)

        {

            int start = i + 1, end = nums.Length - 1;

            while (start < end)

            {

                int sum = nums[start] + nums[end] + nums[i];

                if (Math.Abs(target - sum) < Math.Abs(target - result))

                    result = sum;

                if (sum > target)

                    end--;

                else if (sum < target)

                    start++;

                else

                    return result;

            }

        }

        return result;       

    }

第二题:有效的括号

给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。

有效字符串需满足:

左括号必须用相同类型的右括号闭合。

左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

示例 1:

输入: "()"

输出: true

示例 2:

输入: "()[]{}"

输出: true

示例 3:

输入: "(]"

输出: false

示例 4:

输入: "([)]"

输出: false

示例 5:

输入: "{[]}"

输出: true

示例 6:

输入: ""

输出: true

示例 7:

输入: "(("

输出: false

public bool IsValid(string s)

    {

        if (string.IsNullOrEmpty(s))

            return true;

        int count = s.Length;

        if(count%2 == 1)

            return false;


        Stack stack = new Stack();

        for (int i = 0; i < count; i++)

        {

            char c = s[i];

            if (stack.Count == 0)

            {

                stack.Push(c);

            }

            else if(c == '[' || c == '{' || c == '(')

            {

                stack.Push(c);

            }

            else if (stack.Peek() == GetPair(c))

            {

                stack.Pop();

            }

            else

            {

                return false;

            }

        }

        return stack.Count == 0;       

    }


    public static char GetPair(char c)

    {

        if (c == ')')

            return '(';

        if (c == '}')

            return '{';

        if (c == ']')

            return '[';

        return '\0';

    }   

第三题:合并两个有序链表

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例:

输入:1->2->4, 1->3->4

输出:1->1->2->3->4->4

public ListNode MergeTwoLists(ListNode l1, ListNode l2)

    {

        ListNode pHead = new ListNode(int.MaxValue);

        ListNode temp = pHead;

        while (l1 != null && l2 != null)

        {

            if (l1.val < l2.val)

            {

                temp.next = l1;

                l1 = l1.next;

            }

            else

            {

                temp.next = l2;

                l2 = l2.next;

            }

            temp = temp.next;

        }

        if (l1 != null)

            temp.next = l1;

        if (l2 != null)

            temp.next = l2;

        return pHead.next;

    }

你可能感兴趣的:(Datawhole第四天打卡)