Leetcode 1925. Count Square Sum Triples

Problem

A square triple (a,b,c) is a triple where a, b, and c are integers and a2 + b2 = c2.

Given an integer n, return the number of square triples such that 1 <= a, b, c <= n.

Algorithm

Count the number of Pythagorean triples by enumerating them according to the definition.

Code

python3 复制代码
class Solution:
    def countTriples(self, n: int) -> int:
        cnts = 0
        for a in range(2, n):
            for b in range(2, n):
                c_2 = a * a + b *b
                c = int(sqrt(c_2))
                if c <= n and c * c == c_2:
                    cnts += 1
        return cnts
相关推荐
进击的荆棘38 分钟前
优选算法——双指针
数据结构·算法
魂梦翩跹如雨39 分钟前
死磕排序算法:手撕快速排序的四种姿势(Hoare、挖坑、前后指针 + 非递归)
java·数据结构·算法
夏鹏今天学习了吗8 小时前
【LeetCode热题100(87/100)】最小路径和
算法·leetcode·职场和发展
哈哈不让取名字8 小时前
基于C++的爬虫框架
开发语言·c++·算法
Lips61110 小时前
2026.1.20力扣刷题笔记
笔记·算法·leetcode
2501_9413297210 小时前
YOLOv8-LADH马匹检测识别算法详解与实现
算法·yolo·目标跟踪
洛生&10 小时前
Planets Queries II(倍增,基环内向森林)
算法
小郭团队11 小时前
1_6_五段式SVPWM (传统算法反正切+DPWM2)算法理论与 MATLAB 实现详解
嵌入式硬件·算法·matlab·dsp开发
小郭团队11 小时前
1_7_五段式SVPWM (传统算法反正切+DPWM3)算法理论与 MATLAB 实现详解
开发语言·嵌入式硬件·算法·matlab·dsp开发
鱼跃鹰飞11 小时前
Leetcode347:前K个高频元素
数据结构·算法·leetcode·面试