当前位置:网站首页>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
边栏推荐
- The wonderful relationship between message queue and express cabinet
- Cases of agile innovation and transformation of consumer goods enterprises
- CTF exercise
- Leetcode1984. Minimum difference in student scores
- Basic knowledge of binary tree
- 知识点滴 - PCB制造工艺流程
- Digital transformation: five steps to promote enterprise progress
- Ligne - raisonnement graphique - 4 - classe de lettres
- 消息队列与快递柜之间妙不可言的关系
- Knowledge drop - PCB manufacturing process flow
猜你喜欢
数据库每日一题---第22天:最后一次登录
Nx10.0 installation tutorial
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
Comparison of various development methods of applets - cross end? Low code? Native? Or cloud development?
0-5VAC转4-20mA交流电流隔离变送器/转换模块
Redis official ORM framework is more elegant than redistemplate
The PHP source code of the new website + remove authorization / support burning goose instead of pumping
Why is network i/o blocked?
0-5vac to 4-20mA AC current isolated transmitter / conversion module
随机推荐
6-3 find the table length of the linked table
Debezium series: set role statement supporting mysql8
JS triangle
微生物健康網,如何恢複微生物群落
全面掌控!打造智慧城市建设的“领导驾驶舱”
Debezium系列之:支持 mysql8 的 set role 語句
Unity local coordinates and world coordinates
今日创见|企业促进创新的5大关键要素
「开源摘星计划」Loki实现Harbor日志的高效管理
Unity technical notes (II) basic functions of scriptableobject
行测-图形推理-2-黑白格类
PCL . VTK files and Mutual conversion of PCD
Debezium系列之:mysql墓碑事件
Unity technical notes (I) inspector extension
【刷题记录】3. 无重复字符的最长子串
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution
Visual design form QT designer design gui single form program
Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to JSP-1