当前位置:网站首页>[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
边栏推荐
- Greenplum Database Fault Analysis - Can a Soft Connection Be Made to the Database Base Folder?
- 如何基于OpenVINO POT工具简单实现对模型的量化压缩
- 协作D2D局部模型聚合的半分散联合学习
- 直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计
- sql语句多字段多个值如何进行排序
- 海量服务实例动态化管理
- Log an error encountered when compiling google gn "I could not find a ".gn" file ..."
- 优化Feed流遭遇拦路虎,是谁帮百度打破了“内存墙”?
- 树形查找(二叉查找树)
- C语言实现简单猜数字游戏
猜你喜欢
直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计
J9数字货币论:web3的创作者经济是什么?
优化Feed流遭遇拦路虎,是谁帮百度打破了“内存墙”?
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
HOG feature study notes
1349. Maximum number of students taking the exam Status Compression
LeetCode使用最小花费爬楼梯----dp问题
程序员失眠时的数羊列表 | 每日趣闻
KingbaseES V8 GIS data migration solution (2. Introduction to the capabilities of Kingbase GIS)
线性表的查找
随机推荐
Programmer's list of sheep counting when insomnia | Daily anecdote
hypervisor相关的知识点
"Dilili, wait for the lights, wait for the lights", the prompt sound for safe production in the factory
浅谈数据安全治理与隐私计算
EBS uses virtual columns and hint hints to optimize sql case
C语言日记 9 if的3种语句
ExcelPatternTool: Excel表格-数据库互导工具
How do programmers without objects spend the Chinese Valentine's Day
fragment可见性判断
Greenplum数据库故障分析——能对数据库base文件夹进行软连接嘛?
迁移学习——Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
【Unity入门计划】2D游戏中遮挡问题的处理方法&伪透视
[Endnote] Word inserts a custom form of Endnote document format
英特尔 XDC 2022 精彩回顾:共建开放生态,释放“基建”潜能
学习笔记-----左偏树
【Word】Word公式导出PDF后出现井号括号#()错误
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
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
C学生管理系统 头添加学生节点