当前位置:网站首页>Leetcode daily question - 27 Remove element (simple)
Leetcode daily question - 27 Remove element (simple)
2022-06-25 20:13:00 【Cheng Xiaoqi】
The two articles in this article refer to the official java solution , There is no official python The answer .
The optimization is the first if sentence .
Solution 1 :
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
# First of all to see val Range ratio of nums[i] Twice as big , Narrow the scope first , This step is very important .
# If there is no such step , Once a number greater than 50, Still compare from beginning to end
if val >50 :
# Notice here that the return is a number , Not the array itself ( Will report a mistake )
return len(nums)
# Double finger needling
n = len(nums)
left = 0
right = 0
while right < n:
if nums[right] != val:
nums[left] = nums[right]
left += 1
right += 1
return left
Solution 2
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
# First of all to see val Range ratio of nums[i] Twice as big , Narrow the scope first
if val >50 :
# Notice here that the return is a number , Not the array itself ( Will report a mistake )
return len(nums)
# Double finger needling
n = len(nums)
left = 0
right = n
while left < right:
if nums[left] == val:
nums[left] = nums[right-1]
right -= 1
else:
left += 1
return left

边栏推荐
- 在打新債開戶證券安全嗎?低傭金靠譜嗎
- Now meditation: crash service and performance service help improve application quality
- Png to NII
- VMware failed to prompt to lock this profile exclusively
- JS mobile phone and computer open different websites
- Suddenly found that the screen adjustment button can not be used and the brightness can not be adjusted
- PAT B1071
- How to understand var = a = b = C = 9? How to pre parse?
- Yum command
- Database data type design (the most detailed in the whole network)
猜你喜欢

Wechat applet cloud function does not have dependency option installed

My official account writing experience sharing

Print 1 cute every 100 milliseconds ~ with a running lantern effect

JS asynchronism (I. asynchronous concept, basic use of web worker)

Log in to Huawei game with a minor account, and pop up anti addiction prompt after startup

Applet password input box

Number of wechat applet custom input boxes

CG kit explore high performance rendering on mobile terminal

PAT B1086

Swin UNET reading notes
随机推荐
4.ypthon function foundation
Impact of Huawei application transfer and application claim on user identification
Randomly generate 100 non repeating numbers between 1 and 150 and put them in the array
PAT B1091
Redis practice: smart use of data types to achieve 100 million level data statistics
Remember to deploy selenium crawler on the server
Does redis transaction support acid?
Is it safe to open a new bond account
Pcl+vs2019+opencv environment configuration
CG kit explore high performance rendering on mobile terminal
Please do not call Page constructor in files
Clickhouse disables automatic clearing of tables / columns, that is, disables TTL
Print 1 cute every 100 milliseconds ~ with a running lantern effect
Png to NII
Huawei fast application access advertising service development guide
Thymleaf template configuration analysis
2.14(Knight Moves)
2.2 step tariff
PAT B1086
PAT B1061