当前位置:网站首页>[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
边栏推荐
- PHP技能评测
- 直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
- 【日常训练】1403. 非递增顺序的最小子序列
- 特殊矩阵的压缩存储
- 2022 EdgeX中国挑战赛8月3日即将盛大开幕
- Flink 1.15.1 集群搭建(StandaloneSession)
- C语言基础知识 -- 指针
- How do programmers without objects spend the Chinese Valentine's Day
- Fragment visibility judgment
- Matlab map with color representation module value size arrow
猜你喜欢

高数_复习_第1章:函数、极限、连续

2022 EdgeX中国挑战赛8月3日即将盛大开幕

汇编语言之源程序

.Net C# Console Create a window using Win32 API

Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit

Advanced Numbers_Review_Chapter 1: Functions, Limits, Continuity

LeetCode使用最小花费爬楼梯----dp问题

ExcelPatternTool: Excel table-database mutual import tool

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

sql语句多字段多个值如何进行排序
随机推荐
Fragment visibility judgment
多线程(2)
<开发>实用工具
释放技术创新引擎,英特尔携手生态合作伙伴推动智慧零售蓬勃发展
02 【开发服务器 资源模块】
Transfer Learning - Distant Domain Transfer Learning
C语言基础知识 -- 指针
Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
用@Mapper查询oracle的分区情况报错
the mechanism of ideology
关于#sql shell#的问题,如何解决?
Pisanix v0.2.0 发布|新增动态读写分离支持
【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗
matlab绘制用颜色表示模值大小的箭头图
DAY23:命令执行&代码执行漏洞
蚁剑高级模块开发
海量服务实例动态化管理
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
2022 EdgeX中国挑战赛8月3日即将盛大开幕
J9数字货币论:web3的创作者经济是什么?