当前位置:网站首页>[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
边栏推荐
- C语言基础知识 -- 指针
- leetcode-对称二叉树
- Amazon Cloud Technology joins hands with Thundersoft to build an AIoT platform for industry customers
- Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
- C语言日记 9 if的3种语句
- Pisanix v0.2.0 发布|新增动态读写分离支持
- Using OpenVINO to implement the flying paddle version of the PGNet inference program
- 基于OpenVINO工具套件简单实现YOLOv7预训练模型的部署
- .Net C# 控制台 使用 Win32 API 创建一个窗口
- 【Unity入门计划】2D游戏中遮挡问题的处理方法&伪透视
猜你喜欢
Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec
DAY22:sqli-labs 靶场通关wp(Less01~~Less20)
Xunrui cms website cannot be displayed normally after relocation and server change
.Net C# Console Create a window using Win32 API
01 【前言 基础使用 核心概念】
DAY23:命令执行&代码执行漏洞
ExcelPatternTool: Excel table-database mutual import tool
J9数字货币论:web3的创作者经济是什么?
Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
刷爆朋友圈,Alibaba出品亿级并发设计速成笔记太香了
随机推荐
How to deal with your own shame
【Word】Word公式导出PDF后出现井号括号#()错误
The 2022 EdgeX China Challenge will be grandly opened on August 3
C学生管理系统 头添加学生节点
多线程(2)
Live preview | 30 minutes started quickly!Look at credible distributed AI chain oar architectural design
".NET IoT from scratch" series
后期学习计划
亚马逊云科技 + 英特尔 + 中科创达为行业客户构建 AIoT 平台
CMS建站流程
Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
Leetcode brushing questions - 22. Bracket generation
直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
意识形态的机制
Utilities Log an error encountered when compiling google gn "I could not find a ".gn" file ..."
C语言实现简单猜数字游戏
Residential water problems
【LeetCode刷题】-数之和专题(待补充更多题目)
Exploding the circle of friends, Alibaba produced billion-level concurrent design quick notes are too fragrant