当前位置:网站首页>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
边栏推荐
- What is fake sharing after filling the previous hole?
- 聊聊 Dart 的空安全 (null safety) 特性
- LeetCode203. Remove linked list elements
- Details of the open source framework of microservice architecture
- Database daily question --- day 22: last login
- Ligne - raisonnement graphique - 4 - classe de lettres
- Debezium系列之:源码阅读之BinlogReader
- What is ADC sampling rate (Hz) and how to calculate it
- Are the microorganisms in the intestines the same as those on the skin?
- PHP method of obtaining image information
猜你喜欢
![[record of question brushing] 3 Longest substring without duplicate characters](/img/44/1cd8128d93c9c273e0f4718d84936e.png)
[record of question brushing] 3 Longest substring without duplicate characters

I wish you all the best and the year of the tiger

ASEMI整流桥KBPC1510的型号数字代表什么

小程序多种开发方式对比-跨端?低代码?原生?还是云开发?
Apple further entered the financial sector through the 'virtual card' security function in IOS 16

Sword finger offer 27 Image of binary tree

Software test classification

详解全志V853上的ARM A7和RISC-V E907之间的通信方式

It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen

Microbial Health Network, How to restore Microbial Communities
随机推荐
CTF exercise
Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
Comparison of various development methods of applets - cross end? Low code? Native? Or cloud development?
PCL .vtk文件与.pcd的相互转换
[network] Introduction to C language
PHP method of obtaining image information
Interview questions: how to test app performance?
Debezium系列之:引入对 LATERAL 运算符的支持
关于海康ipc的几个参数
行测-图形推理-6-相似图形类
XMIND mind mapping software sharing
Leetcode1984. Minimum difference in student scores
聊聊 Dart 的空安全 (null safety) 特性
Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
[untitled] reprint melting ice - track icedid server with a few simple steps
The author of LinkedList said he didn't use LinkedList himself
30讲 线性代数 第五讲 特征值与特征向量
Leetcode206. Reverse linked list