"16-20" Leetcode:python Problem solving __python

Source: Internet
Author: User
Tags abs
3Sum closest"Medium"

Given an array S of n integers, find three integers in s such so the sum is closest to a Given number, target. Return the sum of the three integers. You may assume this each input would have exactly one solution.

For example, given array S = {-1 2 1-4}, and target = 1.

The sum is closest to the target is 2. (-1 + 2 + 1 = 2).

Solution:

class Solution (object): Def threesumclosest (self, Nums, target): "": Type Nu
        Ms:list[int]: Type Target:int:rtype:int "" "nums.sort () lens = Len (nums) result = 100000000 to I in range (lens-2): If i > 0 and nums[i-1] = = Nums[i]: Co
                Ntinue L, R = i+1, lens-1 while L < r:s = Nums[i] + nums[l] + nums[r]
                    if s = = Target:return target if ABS (S-target) < ABS (Result-target): result = S If s > target:r-= 1 while L < R a
                    nd nums[r] = = Nums[r+1]: R-= 1 Elif s < target:l + = 1 While L < r and Nums[l] = = Nums[l-1]: L + = 1 return result 
Letter Combinations of a Phonenumber "Medium"

Given a digit string, return all possible letter combinations this number could represent.

A mapping of Digit to letters (just as the telephone buttons) is given below.

Input:digit string "A"
Output: ["Ad", "AE", "AF", "BD", "Be", "BF", "CD", "CE", "CF"].

Solution:

Class Solution (object):
    def lettercombinations (self, digits): ""
        : Type digits:str
        : rtype:list[str ] "" "
        dic = {' 2 ': ' abc ', ' 3 ': ' Def ', ' 4 ': ' Ghi ', ' 5 ': ' JKL ', ' 6 ': ' MnO ', ' 7 ': ' Pqrs ', ' 8 ': ' TUV ', ' 9 ': ' WXYZ '}
        return [] If not digits else reduce (lambda x, y: [i + J for I in X for J in Dic[y]], digits, ['])
4Sum"Medium"

Given an array s of n integers, are there elements a, B, C, and D in s such that A + B + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note: The solution set must not contain duplicate quadruplets.

For example, given array S = [1, 0,-1, 0,-2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2,-1, 1, 2],
  [-2, 0, 0  , 2]
]

Solution:

Class Solution (object): Def foursum (self, Nums, target): "": Type Nums:list[int]: type Targe
        T:int:rtype:list[list[int]] "" "nums.sort () Lenn = Len (nums) result = [] For I in Range (lenn-3): If i > 0 and nums[i] = = Nums[i-1]: Continue for J in Range (i+1, Lenn): If J > i+1 and nums[j] = = Nums[j-1]: Continue L,
                    r = j+1, lenn-1 while L < r:s = Nums[i] + nums[j] + nums[l] +nums[r] If s < target:l + = 1 elif s > target:r 
                        -= 1 Else:result.append ([nums[i], nums[j], nums[l], Nums[r]) L, R = l+1, r-1 while L < r and Nums[l-1] = = Nums[l]: L +
                    = 1    While L < r and Nums[r+1] = = Nums[r]: R-= 1 return result
 

Discussion:

Reference: HTTPS://DISCUSS.LEETCODE.COM/TOPIC/22705/PYTHON-140MS-BEATS-100-AND-WORKS-FOR-N-SUM-N-2/2 19. Remove nth Nodefrom end of List "Medium"

Given A linked list, remove the nth node from the "End of" and return to its head.

For example,

Given linked list:1->2->3->4->5, and n = 2.

   After removing the "second node from the" end, the linked list becomes 1->2->3->5.

Note:
Given n'll always be valid.
Try to does this in one pass.

Solution:

# Definition for singly-linked list.
# class ListNode (object):
#     def __init__ (self, x):
#         self.val = x
#         Self.next = None

class Solution (object):
    def recursion (self, parent, node, N):
        if node = None: return
            1
        num = self.recursion (node, Node.next, N)
        if num = = N:
            If Parent = = Node:
                nextnone = node.next
                node.val = nextnone.val
                Node.next = Nextnone.next
                Nextnone = none
            else:
                parent.next = node.next
                Node.next = none return
        num + 1

    def Removenthfromend (self, head, N): ""
        : Type Head:listnode
        : Type n:int
        : Rtype:listnode
        "" C28/>if Head.next = = None: Return
            [] self.recursion-head, head
        , n
        
Valid parentheses"Easy"

Given A string containing just the characters ' (', ') ', ' {', '} ', ' [' and '] ', determine if the input string is valid.

The brackets must the correct order, "()" and "() []{} ' are all valid but ' (]" and "([)]" are not.

Solution:

Class Solution (object):
    def isValid (self, s): ""
        : Type s:str
        : Rtype:bool ""
        dic = {') ': ' , '] ': ' [', '} ': ' {'}
        l = [] for
        I in S:
            if not L or I not to dic or (I in DIC and L[-1]!= dic[i]):
                L.ap Pend (i)
            else:
                l.pop ( -1) return
        False if l else True

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.