当前位置:网站首页>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

边栏推荐
- Log in to Huawei game with a minor account, and pop up anti addiction prompt after startup
- 2020-11-14-Alexnet
- H5 application conversion fast application
- Automatic fitting when the applet reaches the top
- Yum command
- 2.2 step tariff
- The native JS mobile phone sends SMS cases. After clicking the button, the mobile phone number verification code is sent. The button needs to be disabled and re enabled after 60 seconds
- Expand and check the specified node when loading ztree
- Some pictures of real machine preview development and debugging are not shown
- Appearance of object attributes
猜你喜欢

<C>. array

<C>. Branch and loop statements

Hdoj topic 2005 day

Redis practice: smart use of data types to achieve 100 million level data statistics

Applet wx Request encapsulation

Panda weekly -2022/02/18

<C>. Rolling phase division

Arduino ide + esp8266+mqtt subscribe to publish temperature and humidity information

Single chip microcomputer line selection method to store impression (address range) method + Example

Nnformer reading notes
随机推荐
SQL statement select summary
Does redis transaction support acid?
[harmonyos] [arkui] how can Hongmeng ETS call pa
Hdoj topic 2005 day
Is it safe to open an online account for new bonds? What should be paid attention to
Go language installation and uninstallation
Some pictures of real machine preview development and debugging are not shown
Expand and check the specified node when loading ztree
Log in to Huawei game with a minor account, and pop up anti addiction prompt after startup
Clickhouse disables automatic clearing of tables / columns, that is, disables TTL
Measurement index SSMI
Redis high availability: do you call this the principle of master-slave architecture data synchronization?
NMS reduction box
PHP FPM, workman, spoole, golang simple performance test
Huawei in application review test requirements
<C>. Calculation date to day conversion
Jsonp non homologous interaction (click trigger)
Redis core article: the secret that can only be broken quickly
Determine whether it is a web page opened on wechat
2.15(Multiple of 3 Or 5)