当前位置:网站首页>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
边栏推荐
- 行测-图形推理-4-字母类
- Circumvention Technology: Registry
- QT graphicsview graphical view usage summary with flow chart development case prototype
- 行测-图形推理-5-一笔画类
- Debezium系列之:支持 mysql8 的 set role 语句
- Ligne - raisonnement graphique - 4 - classe de lettres
- I wish you all the best and the year of the tiger
- Unity与WebGL的相爱相杀
- Ni9185 and ni9234 hardware settings in Ni Max
- Unity dynamically merges mesh textures
猜你喜欢
Force deduction - question 561 - array splitting I - step by step parsing
数据库每日一题---第22天:最后一次登录
ASEMI整流桥KBPC1510的型号数字代表什么
Sword finger offer 27 Image of binary tree
线上面试,该如何更好的表现自己?这样做,提高50%通过率~
Knowledge drop - PCB manufacturing process flow
Database daily question --- day 22: last login
不夸张地说,这是我见过最通俗易懂的,pytest入门基础教程
Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
随机推荐
Debezium series: MySQL tombstone event
Debezium series: set role statement supporting mysql8
Unity dynamically merges mesh textures
Debezium系列之:支持 mysql8 的 set role 语句
Circumvention Technology: Registry
Anta DTC | Anta transformation, building a growth flywheel that is not only FILA
Txt file virus
Unity FAQ (I) lack of references
【测试面试题】页面很卡的原因分析及解决方案
Nx10.0 installation tutorial
Digital transformation: five steps to promote enterprise progress
Micro service remote debug, nocalhost + rainbow micro service development second bullet
行测-图形推理-4-字母类
LeetCode206. Reverse linked list [double pointer and recursion]
Unity technical notes (I) inspector extension
安踏DTC | 安踏转型,构建不只有FILA的增长飞轮
行测-图形推理-9-线条问题类
行测-图形推理-6-相似图形类
Transparent i/o model from beginning to end
Debezium系列之:引入对 LATERAL 运算符的支持