当前位置:网站首页>[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
边栏推荐
- leetcode-对称二叉树
- 基于OpenVINO工具套件简单实现YOLOv7预训练模型的部署
- source program in assembly language
- Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
- .Net C# 控制台 使用 Win32 API 创建一个窗口
- 【Word】Word公式导出PDF后出现井号括号#()错误
- 多线程(2)
- <开发>实用工具
- The difference between a process in user mode and kernel mode [exclusive analysis]
- 《.NET物联网从零开始》系列
猜你喜欢

Leetcode刷题——22. 括号生成

如何逐步执行数据风险评估

【Endnote】Word插入自定义形式的Endnote文献格式

C语言日记 9 if的3种语句

SuperMap iDesktop.Net之布尔运算求交——修复含拓扑错误复杂模型

Using OpenVINO to implement the flying paddle version of the PGNet inference program

MySQL3

【Word】Word公式导出PDF后出现井号括号#()错误

“嘀哩哩,等灯等灯”,工厂安全生产的提示音

【MySQL series】- Does LIKE query start with % will make the index invalid?
随机推荐
01 【前言 基础使用 核心概念】
线性表的查找
短域名绕过及xss相关知识
浅谈数据安全治理与隐私计算
the mechanism of ideology
fragment可见性判断
进程在用户态和内核态的区别[独家解析]
行业案例|世界 500 强险企如何建设指标驱动的经营分析系统
PHP Skills Assessment
Leetcode刷题——22. 括号生成
The difference between a process in user mode and kernel mode [exclusive analysis]
居民用水问题
.Net C# 控制台 使用 Win32 API 创建一个窗口
select 标签自定义样式
使用OpenVINO实现飞桨版PGNet推理程序
英特尔 XDC 2022 精彩回顾:共建开放生态,释放“基建”潜能
leetcode 15
Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
LeetCode使用最小花费爬楼梯----dp问题
刷爆朋友圈,Alibaba出品亿级并发设计速成笔记太香了