{"id":9280,"date":"2021-12-30T02:37:20","date_gmt":"2021-12-30T10:37:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9280"},"modified":"2021-12-30T02:44:42","modified_gmt":"2021-12-30T10:44:42","slug":"leetcode-1925-count-square-sum-triples","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1925-count-square-sum-triples\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1925. Count Square Sum Triples"},"content":{"rendered":"\n<p>A&nbsp;<strong>square triple<\/strong>&nbsp;<code>(a,b,c)<\/code>&nbsp;is a triple where&nbsp;<code>a<\/code>,&nbsp;<code>b<\/code>, and&nbsp;<code>c<\/code>&nbsp;are&nbsp;<strong>integers<\/strong>&nbsp;and&nbsp;<code>a<sup>2<\/sup>&nbsp;+ b<sup>2<\/sup>&nbsp;= c<sup>2<\/sup><\/code>.<\/p>\n\n\n\n<p>Given an integer&nbsp;<code>n<\/code>, return&nbsp;<em>the number of&nbsp;<strong>square triples<\/strong>&nbsp;such that&nbsp;<\/em><code>1 &lt;= a, b, c &lt;= n<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5\n<strong>Output:<\/strong> 2\n<strong>Explanation<\/strong>: The square triples are (3,4,5) and (4,3,5).\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 10\n<strong>Output:<\/strong> 4\n<strong>Explanation<\/strong>: The square triples are (3,4,5), (4,3,5), (6,8,10), and (8,6,10).\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 250<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Enumerate a &amp; b<\/strong><\/h2>\n\n\n\n<p>Brute force enumerates a &amp; b &amp; c, which takes O(n<sup>3<\/sup>). Just need to enumerate a &amp; b and validate c.<\/p>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countTriples(int n) {\n    int ans = 0;\n    for (int a = 1; a <= n; ++a)\n      for (int b = 1; b <= n; ++b) {\n        int c = sqrt(a * a + b * b);\n        if (c <= n &#038;&#038; c * c == a * a + b * b) ++ans;        \n      }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;square triple&nbsp;(a,b,c)&nbsp;is a triple where&nbsp;a,&nbsp;b, and&nbsp;c&nbsp;are&nbsp;integers&nbsp;and&nbsp;a2&nbsp;+ b2&nbsp;= c2. Given an integer&nbsp;n, return&nbsp;the number of&nbsp;square triples&nbsp;such that&nbsp;1 &lt;= a, b, c &lt;= n. Example 1: Input:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31],"class_list":["post-9280","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9280"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9280\/revisions"}],"predecessor-version":[{"id":9282,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9280\/revisions\/9282"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}