当前位置:网站首页>Brush question 4
Brush question 4
2022-07-07 23:05:00 【Anny Linlin】
10、 Given an include n Array of integers nums and A target value target. find nums Three integers in , Make their sum and target Nearest . Returns the sum of the three numbers . Assume that there is only one answer for each group of input .
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
nums.sort()
min = sum(nums[0:3])
for i in range(len(nums) - 2):
left, right = i + 1, len(nums) - 1
while left < right:
sum = nums[i] + nums[left] + nums[right]
min = min if abs(min - target) < abs(sum - target) else sum
if sum == target:
return sum
elif sum > target:
right -= 1
else:
left += 1
return min
11、 Valid parenthesis
Given one only includes '(',')','{','}','[',']' String , Determines whether the string is valid .
Valid string needs to meet :
Opening parentheses must be closed with closing parentheses of the same type .
The left parenthesis must be closed in the correct order .
Note that an empty string can be considered a valid string .
def isValid(s):
if s and len(s) % 2 is 0:
a = {')': '(', ']': '[', '}': '{'}
l = [None]
for i in s:
if i in a and a[i] == l[-1]:
l.pop()
else:
l.append(i)
return len(l) == 1
elif len(s) % 2 is 1:
return False
else:
return True
12、 Merge two sequential tables
Merge two ordered lists into a new ordered list and return . The new linked list is made up of all the nodes of the given two linked lists .
Example :
Input :1->2->4, 1->3->4
Output :1->1->2->3->4->4
class Solution:
def mergeTwoLists(self, l1: 'ListNode', l2: 'ListNode') -> 'ListNode':
head = ListNode(0)
node = head
while l1 and l2:
if l1.val <= l2.val:
node.next = l1
l1 = l1.next
else:
node.next = l2
l2 = l2.next
node = node.next
if l1:
node.next = l1
if l2:
node.next = l2
return head.next
边栏推荐
- 安踏DTC | 安踏转型,构建不只有FILA的增长飞轮
- 行测-图形推理-3-对称图形类
- Line test graph reasoning graph group class
- Visual design form QT designer design gui single form program
- 一次搞明白 Session、Cookie、Token,面试问题全稿定
- Take full control! Create a "leading cockpit" for smart city construction
- The PHP source code of the new website + remove authorization / support burning goose instead of pumping
- I wish you all the best and the year of the tiger
- Innovation today | five key elements for enterprises to promote innovation
- Leetcode19. Delete the penultimate node of the linked list [double pointer]
猜你喜欢
Line measurement - graphic reasoning -9- line problem class
Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
Unity and webgl love each other
行测-图形推理-1-汉字类
DTC社群运营怎么做?
Online interview, how to better express yourself? In this way, the passing rate will be increased by 50%~
Basic knowledge of linked list
Redis cluster installation
Ligne - raisonnement graphique - 4 - classe de lettres
Amesim2016 and matlab2017b joint simulation environment construction
随机推荐
Redis cluster installation
Line test - graphic reasoning - 2 - black and white lattice class
LeetCode144. Preorder traversal of binary tree
Unity technical notes (II) basic functions of scriptableobject
Line test - graphic reasoning -7- different graphic classes
Why is network i/o blocked?
Line measurement - graphic reasoning -9- line problem class
Cases of agile innovation and transformation of consumer goods enterprises
Line test - graphic reasoning - 3 - symmetric graphic class
Debezium系列之:支持 mysql8 的 set role 语句
Quick sort (diagram +c code)
Transparent i/o model from beginning to end
7-51 combination of two ordered linked list sequences
Talk about DART's null safety feature
GBU1510-ASEMI电源专用15A整流桥GBU1510
Sword finger offer 28 Symmetric binary tree
微生物健康网,如何恢复微生物群落
[language programming] exe virus code example
Leetcode206. Reverse linked list
Digital transformation: five steps to promote enterprise progress