[633] 平方数之和

/*
 * @lc app=leetcode.cn id=633 lang=java
 *
 * [633] 平方数之和
 *
 * https://leetcode-cn.com/problems/sum-of-square-numbers/description/
 *
 * algorithms
 * Easy (29.35%)
 * Total Accepted:    4K
 * Total Submissions: 13.6K
 * Testcase Example:  '5'
 *
 * 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c。
 * 
 * 示例1:
 * 
 * 
 * 输入: 5
 * 输出: True
 * 解释: 1 * 1 + 2 * 2 = 5
 * 
 * 
 * 
 * 
 * 示例2:
 * 
 * 
 * 输入: 3
 * 输出: False
 * 
 * 
 */
class Solution {
    public boolean judgeSquareSum(int c) {
        boolean result = false;
        int half = c/2;
        for (int i =0;i*i<=half;i++) {
            int second = c-i*i;
            int temp = (int)Math.sqrt(second);
            if (temp*temp == second) {
                 result = true;
            }       
        }
        return result;
    }




}

  

猜你喜欢

转载自www.cnblogs.com/airycode/p/10457077.html