前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#版 - Leetcode 633. 平方数之和 - 题解

C#版 - Leetcode 633. 平方数之和 - 题解

作者头像
Enjoy233
发布2019-03-05 15:55:27
5880
发布2019-03-05 15:55:27
举报
文章被收录于专栏:大白技术控的技术自留地

C#版 - Leetcode 633. 平方数之和 - 题解

Leetcode 633 - Sum of square number

在线提交: https://leetcode.com/problems/sum-of-square-numbers/

题目描述

给定一个非负整数 c ,你要判断是否存在两个整数 a和 b,使得 a2+b2=ca2+b2=ca^2 + b^2 = c。

示例1:

代码语言:javascript
复制
输入: 5
输出: True
解释: 1 * 1 + 2 * 2 = 5

示例2:

代码语言:javascript
复制
输入: 3
输出: False

Input: 5 2 100

Expected answer: true true true



思路:

做一次循环,用目标和减去循环变量的平方,如果剩下的部分依然是完全平方的情形存在,就返回true,否则返回false。循环变量i满足 i2⋅2<c2i2⋅2<c2i^2 \cdot 2 < c^2 .

已AC代码: 最初版本:

代码语言:javascript
复制
public class Solution
{
    public bool JudgeSquareSum(int c)
    {           
        for (int i = 0; c - 2 * i * i >= 0; i++)
        {
            double diff = c - i*i;
            if ((int)(Math.Ceiling(Math.Sqrt(diff))) == (int)(Math.Floor(Math.Sqrt(diff))))
                return true;
        }

        return false;
    }
}

Rank:

You are here! Your runtime beats 56.14% of csharp submissions. 优化1:

代码语言:javascript
复制
public class Solution
{
    public bool JudgeSquareSum(int c)
    {           
        for (int i = 0; c - 2 * i * i >= 0; i++)
        {
            int diff = c - i*i;
            if (IsPerfectSquare(diff))
                return true;
        }

        return false;
    }
    private bool IsPerfectSquare(int num)
    {
        double sq1 = Math.Sqrt(num);
        int sq2 = (int)Math.Sqrt(num);
        if (Math.Abs(sq1 - (double)sq2) < 10e-10)
            return true;
        return false;
    }
}

Rank: You are here! Your runtime beats 85.96% of csharp submissions.

优化2:

代码语言:javascript
复制
public class Solution
{
    public bool JudgeSquareSum(int c)
    {           
        for (int i = 0; i <= c && c - i * i >= 0; i++)
        {
            int diff = c - i*i;
            if (IsPerfectSquare(diff))
                return true;
        }

        return false;
    }
    public bool IsPerfectSquare(int num)
    {
        if ((0x0213 & (1 << (num & 15))) != 0)
        {
            int t = (int)Math.Floor(Math.Sqrt((double)num) + 0.5);
            return t * t == num;
        }
        return false;
    }
}

Rank: You are here! Your runtime beats 85.96% of csharp submissions.

优化3:

代码语言:javascript
复制
public class Solution
{
    public bool JudgeSquareSum(int c)
    {           
        for (int i = 0; c - i * i >= 0; i++)
        {
            long diff = c - i*i;
            if (IsSquareFast(diff))
                return true;
        }

        return false;
    }

    bool IsSquareFast(long n)
    {
        if ((0x2030213 & (1 << (int)(n & 31))) > 0)
        {
            long t = (long)Math.Round(Math.Sqrt((double)n));
            bool result = t * t == n;
            return result;
        }
        return false;
    }
}

Rank: You are here! Your runtime beats 85.96% of csharp submissions.

另外,stackoverflow上还推荐了一种写法:

代码语言:javascript
复制
public class Solution
{
    public bool JudgeSquareSum(int c)
    {           
        for (int i = 0; c - 2 * i * i >= 0; i++)
        {
            double diff = c - i*i;
            if (Math.Abs(Math.Sqrt(diff) % 1) < 0.000001)
                return true;
        }

        return false;
    }
}

事实上,速度并不快: Rank: You are here! Your runtime beats 29.82% of csharp submissions.

Reference:

Fast way to test whether a number is a square https://www.johndcook.com/blog/2008/11/17/fast-way-to-test-whether-a-number-is-a-square/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年07月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C#版 - Leetcode 633. 平方数之和 - 题解
  • 题目描述
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档