当前位置:网站首页>[LeetCode Brush Questions] - Sum of Numbers topic (more topics to be added)
[LeetCode Brush Questions] - Sum of Numbers topic (more topics to be added)
2022-08-05 02:18:00 【D2O】
LeetCode上关于“x数之和”The topics are as follows
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])
Attached is a superfluous wrong solution:
The question asked to return is index ,It doesn't apply as soon as it comes up sort 的解法,
由于numsThere are repeated elements,Unsurprisingly, this method fails.
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 use double pointer after
三数之和题解(One of them timed out)
18 四数之和
[18 四数之和](https://leetcode.cn/problems/4sum/
思路:还是sort 之后双指针
# TODO:加上代码
633 平方数之和
633 平方数之和
这个题挺简单的,Determine the search scope is range(1, int(math.sqrt(c) + 1)),
Find two numbers in this range,Make their sum of squares the target value,
Next comes the classic sum of two numbers problem.
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:暴力计算
注:因为只要bIt is a natural number that meets the requirements of the question,So you don't have to go through it hardlist了,直接判断b是否是整数.
Determine whether a number is an integer: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
A little thought about double pointers
What is the essence of double pointer?
Will the correct answer be clipped when moving?
珠玉在前,Let's put the analysis of double pointers written by God
边栏推荐
- 协作D2D局部模型聚合的半分散联合学习
- RAID磁盘阵列
- 02 【开发服务器 资源模块】
- STM32使用stm32cubemx LL库系列教程
- 释放技术创新引擎,英特尔携手生态合作伙伴推动智慧零售蓬勃发展
- Programmer's list of sheep counting when insomnia | Daily anecdote
- 【LeetCode刷题】-数之和专题(待补充更多题目)
- ExcelPatternTool: Excel table-database mutual import tool
- 【OpenCV 图像处理2】:OpenCV 基础知识
- .Net C# 控制台 使用 Win32 API 创建一个窗口
猜你喜欢

CPDA|运营人如何从负基础学会数据分析(SQL)

使用SuperMap iDesktopX数据迁移工具迁移地图文档和符号

Using OpenVINO to implement the flying paddle version of the PGNet inference program
![[Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective](/img/de/944b31c68cc5b9ffa6a585530e7be9.png)
[Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective

英特尔 XDC 2022 精彩回顾:共建开放生态,释放“基建”潜能

sql语句多字段多个值如何进行排序

常见的硬件延迟

select 标签自定义样式

记录谷歌gn编译时碰到的一个错误“I could not find a “.gn“ file ...”

第09章 性能分析工具的使用【2.索引及调优篇】【MySQL高级】
随机推荐
【LeetCode刷题】-数之和专题(待补充更多题目)
继承关系下构造方法的访问特点
Domain Driven Design - MDD
汇编语言之源程序
Utilities Oracle encapsulates restful interfaces into views
一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征
iNFTnews | What can NFTs bring to the sports industry and fans?
LPQ(局部相位量化)学习笔记
Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec
[parameters of PyQT5 binding functions]
第09章 性能分析工具的使用【2.索引及调优篇】【MySQL高级】
CPDA|运营人如何从负基础学会数据分析(SQL)
std::string::find 返回值的坑
意识形态的机制
How to deal with your own shame
iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?
CMS建站流程
用@Mapper查询oracle的分区情况报错
C学生管理系统 据学号查找学生节点