当前位置:网站首页>[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
边栏推荐
- 汇编语言之源程序
- 2022 EdgeX中国挑战赛8月3日即将盛大开幕
- 协作D2D局部模型聚合的半分散联合学习
- 如何看待自己的羞愧感
- EBS uses virtual columns and hint hints to optimize sql case
- Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
- Pisanix v0.2.0 发布|新增动态读写分离支持
- C学生管理系统 头添加学生节点
- How do programmers without objects spend the Chinese Valentine's Day
- The difference between a process in user mode and kernel mode [exclusive analysis]
猜你喜欢
iNFTnews | What can NFTs bring to the sports industry and fans?
js中try...catch和finally的用法
程序员失眠时的数羊列表 | 每日趣闻
【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
nodeJs--封装路由
[Word] #() error occurs after Word formula is exported to PDF
2022 EdgeX中国挑战赛8月3日即将盛大开幕
ExcelPatternTool: Excel table-database mutual import tool
DAY22:sqli-labs 靶场通关wp(Less01~~Less20)
Advanced Numbers_Review_Chapter 1: Functions, Limits, Continuity
随机推荐
DAY22:sqli-labs 靶场通关wp(Less01~~Less20)
Leetcode brushing questions - 22. Bracket generation
网络安全与元宇宙:找出薄弱环节
在这个超连接的世界里,你的数据安全吗
继承关系下构造方法的访问特点
C语言日记 9 if的3种语句
Greenplum Database Fault Analysis - Can a Soft Connection Be Made to the Database Base Folder?
HOG feature study notes
【genius_platform软件平台开发】第七十六讲:vs预处理器定义的牛逼写法!!!!(其他组牛逼conding人员告知这么配置来取消宏定义)
PHP技能评测
Greenplum数据库故障分析——版本升级后gpstart -a为何返回失败
Fragment visibility judgment
SDC简介
"Dilili, wait for the lights, wait for the lights", the prompt sound for safe production in the factory
C语言基础知识 -- 指针
SuperMap iDesktop.Net之布尔运算求交——修复含拓扑错误复杂模型
使用OpenVINO实现飞桨版PGNet推理程序
[Endnote] Word inserts a custom form of Endnote document format
一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征
How to simply implement the quantization and compression of the model based on the OpenVINO POT tool