当前位置:网站首页>【LeetCode刷题】-数之和专题(待补充更多题目)
【LeetCode刷题】-数之和专题(待补充更多题目)
2022-08-05 02:07:00 【D2O】
LeetCode上关于“x数之和”的题目有如下几道
1 两数之和
1 两数之和
思路:hush map
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hush=[]
for i in range(len(nums)):
temp=target-nums[i]
if temp in hush:
return [hush.index(temp),i]
else:
hush.append(nums[i])
附一个画蛇添足的错误解法:
题目要求返回的是 index ,就不适用这种一上来就 sort 的解法,
由于nums中有重复元素的存在,这种方法不出意外地报错了。
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
temp1=nums.copy()
nums.sort()
l,r=0,len(nums)-1
while l<r:
temp=nums[l]+nums[r]
if temp==target:
return [temp1.index(nums[l]),temp1.index(nums[r])]
elif temp<target:
l+=1
else:
r-=1
15 三数之和
15 三数之和
思路:sort 之后用双指针
三数之和题解(其中一种超时了)
18 四数之和
[18 四数之和](https://leetcode.cn/problems/4sum/
思路:还是sort 之后双指针
# TODO:加上代码
633 平方数之和
633 平方数之和
这个题挺简单的,确定检索范围是 range(1, int(math.sqrt(c) + 1))
,
在这个范围内找两个数,使得它们的平方和是目标值,
接下来就是经典的两数和问题了。
Python 开方:math.sqrt(num)
解法1:双指针法
class Solution(object):
def judgeSquareSum(self, c):
if c == 0: return True
n=int(math.sqrt(c))
a,b=0,n
while a<=b:
temp=c-a**2-b**2
if temp ==0:
return True
elif temp>0:
a+=1
else:
b-=1
return False
解法2:暴力计算
注:因为只要b是自然数就符合题目要求,所以就不用辛辛苦苦地遍历list了,直接判断b是否是整数。
判断某数是否为整数:int(num)==num
class Solution(object):
def judgeSquareSum(self, c):
if c == 0: return True
for a in range(1, int(math.sqrt(c) + 1)):
b = c - a * a
if int(math.sqrt(b)) ** 2 == b:
return True
return False
关于双指针的一点思考
双指针的本质是什么?
移动的时候会不会剪掉正确答案?
边栏推荐
- 没有对象的程序员如何过七夕
- The difference between a process in user mode and kernel mode [exclusive analysis]
- 迅睿cms网站搬迁换了服务器后网站不能正常显示
- Exercise: Selecting a Structure (1)
- (17) 51 MCU - AD/DA conversion
- iNFTnews | What can NFTs bring to the sports industry and fans?
- .Net C# 控制台 使用 Win32 API 创建一个窗口
- STM32使用stm32cubemx LL库系列教程
- PHP Skills Assessment
- LPQ(局部相位量化)学习笔记
猜你喜欢
Leetcode刷题——22. 括号生成
Understand the recommendation system in one article: Recall 06: Two-tower model - model structure, training method, the recall model is a late fusion feature, and the sorting model is an early fusion
【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗
iNFTnews | What can NFTs bring to the sports industry and fans?
Why is this problem reported when installing oracle11
[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?
开篇-开启全新的.NET现代应用开发体验
金仓数据库 KingbaseES V8 GIS数据迁移方案(3. 基于ArcGIS平台的数据迁移到KES)
day14--postman interface test
iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?
随机推荐
Are testing jobs so hard to find?I am 32 this year and I have been unemployed for 2 months. What should an older test engineer do next to support his family?
SuperMap iDesktop.Net之布尔运算求交——修复含拓扑错误复杂模型
树表的查找
Oracle encapsulates restful interfaces into views
重新审视分布式系统:永远不会有完美的一致性方案……
蚁剑高级模块开发
Greenplum Database Fault Analysis - Can a Soft Connection Be Made to the Database Base Folder?
AI+小核酸药物|Eleven完成2200万美元种子轮融资
2022杭电多校第一场
.Net C# 控制台 使用 Win32 API 创建一个窗口
用@Mapper查询oracle的分区情况报错
Leetcode刷题——22. 括号生成
XMjs跨域问题解决
C语言基础知识 -- 指针
LPQ(局部相位量化)学习笔记
Using OpenVINO to implement the flying paddle version of the PGNet inference program
如何创建rpm包
超越YOLO5-Face | YOLO-FaceV2正式开源Trick+学术点拉满
开篇-开启全新的.NET现代应用开发体验
MySQL3