当前位置:网站首页>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
边栏推荐
- LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
- Why is network i/o blocked?
- Debezium series: MySQL tombstone event
- [untitled] reprint melting ice - track icedid server with a few simple steps
- Interview questions: how to test app performance?
- CTF练习
- Build an "immune" barrier in the cloud to prepare your data
- What is ADC sampling rate (Hz) and how to calculate it
- Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!
- 微生物健康网,如何恢复微生物群落
猜你喜欢
Micro service remote debug, nocalhost + rainbow micro service development second bullet
I wish you all the best and the year of the tiger
数字藏品加速出圈,MarsNFT助力多元化文旅经济!
Talk about DART's null safety feature
Leetcode1984. Minimum difference in student scores
Leetcode interview question 02.07 Linked list intersection [double pointer]
线上面试,该如何更好的表现自己?这样做,提高50%通过率~
[record of question brushing] 3 Longest substring without duplicate characters
PCL . VTK files and Mutual conversion of PCD
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
随机推荐
Digital transformation: five steps to promote enterprise progress
Personal statement of testers from Shuangfei large factory: is education important for testers?
[language programming] exe virus code example
Circumvention Technology: Registry
肠道里的微生物和皮肤上的一样吗?
Knowledge drop - PCB manufacturing process flow
[record of question brushing] 3 Longest substring without duplicate characters
Why is network i/o blocked?
Line test graph reasoning graph group class
Anta DTC | Anta transformation, building a growth flywheel that is not only FILA
6-3 find the table length of the linked table
【刷题记录】3. 无重复字符的最长子串
Line test - graphic reasoning - 2 - black and white lattice class
消费品企业敏捷创新转型案例
LeetCode144. Preorder traversal of binary tree
Online interview, how to better express yourself? In this way, the passing rate will be increased by 50%~
Force deduction - question 561 - array splitting I - step by step parsing
线上面试,该如何更好的表现自己?这样做,提高50%通过率~
Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
Debezium series: source code reading snapshot reader