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
相关推荐
甄心爱学习21 分钟前
【最优化】1-6章习题
人工智能·算法
PD我是你的真爱粉21 分钟前
向量数据库原理与检索算法入门:ANN、HNSW、LSH、PQ 与相似度计算
数据库·人工智能·算法
汀、人工智能23 分钟前
[特殊字符] 第72课:杨辉三角
数据结构·算法·数据库架构·图论·bfs·杨辉三角
_深海凉_33 分钟前
LeetCode热题100- 字母异位词分组
leetcode
洛水水35 分钟前
【力扣100题】14.两数相加
c++·算法·leetcode
我不是小upper36 分钟前
相关≠因果!机器学习中皮尔逊相关检验的完整流程
人工智能·算法·机器学习
float_com38 分钟前
LeetCode80. 删除有序数组中的重复项 II
leetcode
pwn蒸鱼39 分钟前
leetcode:21. 合并两个有序链表
算法·leetcode·链表
洛水水40 分钟前
【力扣100题】15.删除链表的倒数第 N 个结点
算法·leetcode·链表
LTphy1 小时前
深度优先搜索的三种模板
算法·深度优先·图论