描述

A square triple (a,b,c) is a triple where a, b, and c are integers and a^2 + b^2 = c^2.

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

Example 1:

Input: n = 5
Output: 2
Explanation: The square triples are (3,4,5) and (4,3,5).

Example 2:

Input: n = 10
Output: 4
Explanation: The square triples are (3,4,5), (4,3,5), (6,8,10), and (8,6,10).

Note:

1 <= n <= 250

解析

根据题意,就是找出在 [1,n] 范围内的三个数 (a,b,c) ,满足 a^2 + b^2 = c^2 ,这里有两点需要注意:

  • 根据满足条件我们可以当作是在找直角三角形,也就是有一个隐形的条件就是 a+b>c
  • 在找出所有满足题意的 (a,b,c) 之后,因为 c 最大,所以它的位置是不能动的,但是可以转换 a 和 b 的位置,所以最后的三元组合数量是现有的三元组合的两倍

这种解法比较暴力,直接使用三次遍历,找出最后的所有三元组合。

解答

class Solution(object):def countTriples(self, n):""":type n: int:rtype: int"""result = 0for i in range(1,n+1):for j in range(i+1,n+1):for k in range(j+1,n+1):if i+j>k and i*i + j*j == k*k:result += 1return result*2

运行结果

Runtime: 5060 ms, faster than 66.67% of Python online submissions for Count Square Sum Triples.
Memory Usage: 13.4 MB, less than 100.00% of Python online submissions for Count Square Sum Triples.

解析

另外可以先将 1 到 n 的所有的平方结果存入集合 s 中,然后使用现有的组合排列的函数,将 s 中的值都进行排列组合成长度为 2 的组合 p ,然后遍历 p 中的每个组合的元素总和是否存在于 s 中,如果是则计数器 c 加一,遍历结束之后,将 c*2 返回即为结果。

解答

class Solution(object):def countTriples(self, n):""":type n: int:rtype: int"""s=set()for i in range(1,n+1):s.add(i*i)p=combinations(s,2)c=0for i in p:if(sum(list(i)) in s):c=c+1return c*2

运行结果

Runtime: 252 ms, faster than 100.00% of Python online submissions for Count Square Sum Triples.
Memory Usage: 13.4 MB, less than 100.00% of Python online submissions for Count Square Sum Triples.

解析

还有其他的解法,但是万变不离其宗,基本的原理都是一样的,不一样的只是代码形式而已,详细见官网解法。

原题链接:https://leetcode.com/problems/count-square-sum-triples/

您的支持是我最大的动力

leetcode 1925. Count Square Sum Triples(python)相关推荐

  1. Leetcode 1925. Count Square Sum Triples [Python]

    基本思路是暴力破解.但是注意sqrt给出的是float,转成int后检查下是否还是原来的数值,随后检查给出的sqrt是不是小于等于n的. class Solution:def countTriples ...

  2. 【Leetcode】1925. Count Square Sum Triples

    题目地址: https://leetcode.com/problems/count-square-sum-triples/ 给定一个正整数nnn,求三个数a,b,c≤na,b,c\le na,b,c≤ ...

  3. leetcode 1641. Count Sorted Vowel Strings(python)

    描述 Given an integer n, return the number of strings of length n that consist only of vowels (a, e, i ...

  4. LeetCode —— 257. 二叉树的所有路径(Python)

    给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: -------- 解题思路: (1)用变量string记录从根结点到当前结点经过的结点路径. (2) ...

  5. leetcode 1313. Decompress Run-Length Encoded List(python)

    描述 We are given a list nums of integers representing a list compressed with run-length encoding. Con ...

  6. LeetCode —— 897. 递增顺序查找树(Python)

    给你一个树,请你 按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有一个右子结点. 示例 : 来源:力扣(LeetCode) 链接:https://leetcode- ...

  7. Leetcode —— 208. 实现 Trie (前缀树)(Python)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie();trie.insert(" ...

  8. LeetCode笔记:Biweekly Contest 56(补发)

    LeetCode笔记:Biweekly Contest 56 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 ...

  9. 【Leetcode】 刷题之路1(python)

    leetcode 刷题之路1(python) 看到有大佬总结了一些相关题目,想着先刷一类. 1.两数之和 15.三数之和 16.最接近的三数之和 11.盛最多的水 18.四数之和 454.四数相加II ...

最新文章

  1. 实验三-密码破解技术
  2. CIW《操作系统安全》目录
  3. 系统重构的10点经验总结
  4. android画布demo,Android开发画板demo前奏
  5. 黑马程序员_java基础笔记(06)...集合
  6. Best Cow Line(POJ-3617)
  7. Hibernate之Inverse的用法
  8. Bossie Awards 开源大数据工具最佳列表
  9. 每天一点正则表达式积累(四)
  10. VirtualBox虚拟机移到另外一台机器,需要在设置中禁用usb
  11. DSP技术-2-DSP的C语言同主机C语言的主要区别在哪里?
  12. tcpdump进行IP抓包
  13. Python制作绘图板,基础功能实现
  14. 手动配置网络设置静态IP地址
  15. asp.net后台代码如何通过动态的id给aspx中的html控件赋值
  16. 软件相关的小问题记录
  17. 小白入门:a在微信给好友b点赞了。b看到了点赞。但是却没有消息提示b,说a点赞了。这个要怎么定位问题?...
  18. 博云信创云管平台入选工信部推荐解决方案名单,头部券商信创案例获应用示范单项
  19. Wise Installation制作的安装包添加卸载快捷方式
  20. 关于统计学的相关函数

热门文章

  1. python爬虫——selenium+bs4爬取选股宝‘利好‘or’利空'股票信息
  2. API to UPDATE Oracle FND User
  3. Hyper Flash 和 QSPI NOR Flash
  4. SXT:聚合与组合的理解
  5. 三种不同组网方式,组建WIFI全覆盖
  6. Enhance Security with Port Knocking
  7. 对你来说,哪一个深度学习网络是最佳选择?(2)
  8. Unity 解决安卓包激励视频广告退后台再进入APP广告消失
  9. 史上最全Java面试题全集(上)
  10. WinForm 窗体的边框