当前位置:网站首页>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
边栏推荐
- CTF练习
- Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
- The author of LinkedList said he didn't use LinkedList himself
- 双非大厂测试员亲述:对测试员来说,学历重要吗?
- Debezium系列之:源码阅读之SnapshotReader
- Knowledge drop - PCB manufacturing process flow
- 行测-图形推理-1-汉字类
- Loki, the "open source star picking program", realizes the efficient management of harbor logs
- 数字化转型:五个步骤推动企业进步
- 数据库每日一题---第22天:最后一次登录
猜你喜欢
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
【测试面试题】页面很卡的原因分析及解决方案
不夸张地说,这是我见过最通俗易懂的,pytest入门基础教程
线上面试,该如何更好的表现自己?这样做,提高50%通过率~
Force deduction - question 561 - array splitting I - step by step parsing
Unity and webgl love each other
There is another problem just online... Warm
C # realizes the communication between Modbus protocol and PLC
Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
LeetCode203. Remove linked list elements
随机推荐
Build an "immune" barrier in the cloud to prepare your data
PCL . VTK files and Mutual conversion of PCD
Unity technical notes (II) basic functions of scriptableobject
6-3 find the table length of the linked table
Debezium series: source code reading snapshot reader
消费品企业敏捷创新转型案例
微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
LeetCode144. Preorder traversal of binary tree
LeetCode206. Reverse linked list [double pointer and recursion]
Comparison of various development methods of applets - cross end? Low code? Native? Or cloud development?
Are the microorganisms in the intestines the same as those on the skin?
Line test - graphic reasoning - 6 - similar graphic classes
C # realizes the communication between Modbus protocol and PLC
Ligne - raisonnement graphique - 4 - classe de lettres
Transparent i/o model from beginning to end
行测-图形推理-7-相异图形类
行测-图形推理-4-字母类
[language programming] exe virus code example
Select sort (illustration +c code)
Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb