当前位置:网站首页>数组力扣(持续更新)
数组力扣(持续更新)
2022-07-24 04:25:00 【白炎灵】
两数之和(7.21)

执行用时:3148ms
自己写的代码:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
for i in range(n):
for j in range(i+1,n):
if nums[i] + nums[j] == target:
return [i,j]
官方代码:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashtable = dict()
for i, num in enumerate(nums):
if target - num in hashtable:
return [hashtable[target - num], i]
hashtable[nums[i]] = i
return []
盛最多水的容器(7.22 难度中等)

面积取决于短板。①因此即使长板往内移动时遇到更长的板,矩形的面积也不会改变;遇到更短的板时,面积会变小。②因此想要面积变大,只能让短板往内移动(因为移动方向固定了),当然也有可能让面积变得更小,但只有这样才存在让面积变大的可能性
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r = 0, len(height) - 1
ans = 0
while l < r:
area = min(height[l], height[r]) * (r - l)
ans = max(ans, area)
if height[l] <= height[r]:
l += 1
else:
r -= 1
return ans
删除有序数组中的重复项 (7.22 简单)


思考:
还是利用双指针
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
n = len(nums)
fast = slow = 1
while fast < n:
if nums[fast] != nums[fast - 1]:
nums[slow] = nums[fast]
slow += 1
fast += 1
return slow
边栏推荐
- 致-.-- -..- -
- Game improvement of smart people: Lesson 2 of Chapter 3: number of factors
- MySQL service 1 master 2 slave, master master, MHA configuration detailed steps
- Will your NFT disappear? Dfinity provides the best solution for NFT storage
- How to perform chowtest with Stata
- [JS] save the string as a file to the local (.Txt,.Json,.Md...)
- The second anniversary of opengauss' open source, cracking the pain point of database ecology
- .gz的业务交互和对外服篇中我们通合多个模型
- Insider of LAN SDN hard core technology 22 Kang long regrets -- Specifications and restrictions (Part 2)
- Live video | 37 how to use starrocks to realize user portrait analysis in mobile games
猜你喜欢

PMIX ERROR: ERROR in file gds_ds12_lock_pthread.c

Vscode configuration user code snippet

Hardware knowledge 3 -- IIC protocol

归并排序(Merge sort)

PostgreSQL guide -- inside exploration Chapter 1 database clusters, databases and data tables

Four characteristics of nb-iot

An accident caused by MySQL misoperation, and "high availability" can't withstand it

Go language series - synergy GMP introduction - with ByteDance interpolation

What is the product and expressiveness of 113700 Xingrui? Come and have a look

How safe is Volvo XC90? Come and have a look
随机推荐
CONDA common commands
Codeforces Round #809 (Div. 2) A - D1
The C host is always set separately for IIC. If enough, the next few bits can be set
iPhone手机绑定163邮箱解决方案
LAN SDN hard core technology insider 21 Kang long regrets -- Specifications and restrictions (middle)
如何用STATA进行chowtest
短视频本地生活版块,有哪些新的机会存在?
[novice] develop a tab component that is easy to expand by hand
C主机对IIC从来分别设置每足够的话,可下几位
Particle Designer:粒子效果制作器,生成plist文件并在工程中正常使用
LAN SDN technology hard core insider 10 cloud converged matchmaker evpn
buu web
Shell语法(二)
【C语言】程序环境和预处理操作
Baidu search cracking down on pirated websites: why Internet content infringement continues despite repeated prohibitions
Smart contract: release an erc20 token
Insider of LAN SDN hard core technology 22 Kang long regrets -- Specifications and restrictions (Part 2)
Up sampling method (deconvolution, interpolation, anti pooling)
Go language series - synergy GMP introduction - with ByteDance interpolation
(零八)Flask有手就行——数据库迁移Flask-Migrate