当前位置:网站首页>数组力扣(持续更新)
数组力扣(持续更新)
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
边栏推荐
- Collection of test case design methods
- (零八)Flask有手就行——数据库迁移Flask-Migrate
- 可以脱离设据生效这些都是控化部署能力,而后引如
- 短视频本地生活版块,有哪些新的机会存在?
- [translation] chaos mesh moved to CNCF incubator
- iPhone手机绑定163邮箱解决方案
- Where is the difficulty in attracting investment in the park? Inventory of difficulties and difficulties in attracting investment in industrial parks
- 《论文复现》BiDAF代码实现过程(3)模型建立
- Redis sentinel mode, master node check script
- C language classic exercises to write a program to find all the perfects within 1000.
猜你喜欢

【C语言】程序环境和预处理操作

Post it notes --46{hbuildx connect to night God simulator}

Robustness evaluation of commercial in vivo detection platform

Chery arizer 8 products are powerful, and "all excellent" is not for nothing

Leetcode (Sword finger offer) - 11. Minimum number of rotation array

嵌入式系统移植【6】——uboot源码结构

buu web

Codeforces Round #807 (Div. 2) A - D

How does the trend chart of spot silver change?

From bio to realizing the function of simple multi person chat room -- IO model
随机推荐
(零八)Flask有手就行——数据库迁移Flask-Migrate
Leetcode (Sword finger offer) - 11. Minimum number of rotation array
Leetcode 20 valid parentheses, 33 search rotation sort array, 88 merge two ordered arrays (nums1 length is m+n), 160 intersecting linked list, 54 spiral matrix, 415 character addition (cannot be direc
Robustness evaluation of commercial in vivo detection platform
The pit trodden by real people tells you to avoid the 10 mistakes often made in automated testing
Embedded system transplantation [6] - uboot source code structure
Text attack methods open source code summary
eCB接口,其实质也 MDSemodet
PMIX ERROR: ERROR in file gds_ ds12_ lock_ pthread.c
NFT insider 67: Barcelona Football Club launched its first NFT work, and Dubai launched the national metauniverse strategy
Once svchost Troubleshooting of exe process occupying a large amount of network bandwidth
[translation] chaos mesh moved to CNCF incubator
NFT除了买卖还能质押?
buu web
阿里淘系面试题:Redis 如何实现库存扣减操作和防止被超卖?
Privacy protection federal learning framework supporting most irregular users
Avoid mistakes, common appium related problems and Solutions
MySQL service 1 master 2 slave, master master, MHA configuration detailed steps
发送数据1010_1发人员通过 字节的
C语言基础学习笔记