版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址

http://blog.csdn.net/lzuacm

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

Leetcode 633 - Sum of square number

在线提交:

https://leetcode.com/problems/sum-of-square-numbers/

题目描述

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

示例1:

输入: 5
输出: True
解释: 1 * 1 + 2 * 2 = 5

示例2:

输入: 3
输出: False

Input:

5

2

100

Expected answer:

true

true

true


  ●  题目难度: 简单

思路:

做一次循环,用目标和减去循环变量的平方,如果剩下的部分依然是完全平方的情形存在,就返回true,否则返回false。循环变量i满足 i2⋅2&lt;c2" role="presentation">i2⋅2<c2i2⋅2<c2.

已AC代码:

最初版本:

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:

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:

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:

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上还推荐了一种写法:

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/

C#版 - Leetcode 633. 平方数之和 - 题解的更多相关文章

  1. Java实现 LeetCode 633 平方数之和(暴力大法)

    633. 平方数之和 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 ...

  2. LeetCode 633. 平方数之和

    题目: 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c.     示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2 ...

  3. C#刷遍Leetcode面试题系列连载(4) No.633 - 平方数之和

    上篇文章中一道数学问题 - 自除数,今天我们接着分析 LeetCode 中的另一道数学题吧~ 今天要给大家分析的面试题是 LeetCode 上第 633 号问题, Leetcode 633 - 平方数 ...

  4. leetcode.双指针.633平方数之和-Java

    1. 具体题目 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a^2 + b^2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 注 ...

  5. 力扣(LeetCode)平方数之和 个人题解

    给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2: 输入: 3 ...

  6. [LeetCode] 633. Sum of Square Numbers 平方数之和

    Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...

  7. [LeetCode] Sum of Square Numbers 平方数之和

    Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...

  8. C#版 - Leetcode 306. 累加数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  9. 【JavaScript】Leetcode每日一题-平方数之和

    [JavaScript]Leetcode每日一题-平方数之和 [题目描述] 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 示例1: 输入:c = 5 ...

随机推荐

  1. 网络对抗技术 20165220 Exp6 信息搜集与漏洞扫描

    网络对抗技术 20165220 Exp6 信息搜集与漏洞扫描 实验任务 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服 ...

  2. 昂贵的聘礼 POJ - 1062(最短路)

    年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:" ...

  3. Pycharm相对路径

    问题: 今天有个程序,明显路径是存在的,但是os.path.exists的返回结果是False. 仔细想了想, 是相对路径的问题. 情况描述: 我的路径是: dir_path = 'data/mark ...

  4. js-面试题-驼峰命名

    //将字符串转为驼峰命名‘foo-bar-car-day-efg’ function toUpperCase(str){ var string=str.split('-'); var arr=[]; ...

  5. sublime2 nodejs 执行编译无反应

    这个问题困扰了我得一周了,好不容易解决了, 一.问题描述: 安装网上的一些教程在sublime text 2 里面安装Nodejs 的编译环境,但是安装完之后执行编译没有任何输出信息,编译没有反应,只 ...

  6. cn microsoft hyper-v server 2016 安装笔记

    英文版,先用,随后 中文版再出笔记 不知道是不是 WINDOWS 7 USB DVD TOOLS 没有更新,怎么做不了优盘启动,只能找个 替代工具RUFUS.

  7. Exp4 恶意代码分析 20164302 王一帆

    1.实践目标 1.1监控自己系统的运行状态,看有没有可疑的程序在运行. 1.2分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,systra ...

  8. VB中StdPicture尺寸(Width,Height)转像素单位

    首先获得一个图片对象 Dim spic As StdPicture Set spic = LoadPicture("d:\0.bmp") '从文件获得 Set spic = Cli ...

  9. Unity-批量修改Prefab上的属性

    问题描述:今天发现工程中有些prefab上的脚本丢失了一些引用,本以为手动拖拽上去搞定,后来查看其它prefab,也有类似的问题,于是写了一个小工具,批量修改下. 上代码: [ExecuteInEdi ...

  10. js实现八皇后,回溯法

    八皇后问题:将八个皇后摆在一张8*8的国际象棋棋盘上,使每个皇后都无法吃掉别的皇后,一共有多少种摆法? 两个皇后不能同时在同一行,同一列,和斜对角线的位置上,使用回溯法解决. 从第一行选个位置开始放棋 ...