[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 that a2 + b2 = c.

Example 1:

Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5

Example 2:

Input: 3
Output: False

思路:

类似于二分查找。

从0 和sqrt(n)两端分别查找。

bool judgeSquareSum(int c)
{
  if(c==0)return true;

  int qq = sqrt( (double)c );
    for( int ii= 0, pp = qq; ii<= qq && pp >= ii; )
    {
        int sum = ii * ii + pp * pp;
        if( sum < c )
            ii++;
        else if( sum > c )
            pp--;
        else
            return true;
      }

  return false;
}
时间: 2024-11-06 19:54:51

[leetcode-633-Sum of Square Numbers]的相关文章

LeetCode 633. Sum of Square Numbers平方数之和 (C++)

题目: Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. Example 1: Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5 Example 2: Input: 3 Output: False 分析: 给定一个非负整数c ,你要判断是否存在两个整数a和b,使

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 that a2 + b2 = c. Example 1: Input: 5Output: TrueExplanation: 1 * 1 + 2 * 2 = 5 Example 2: Input: 3Output: False 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

LeetCode Sum of Square Numbers

原题链接在这里:https://leetcode.com/problems/sum-of-square-numbers/description/ 题目: Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. Example 1: Input: 5 Output: True Explanation: 1 * 1 + 2 *

[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 that a2 + b2 = c. Example 1: Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5 Example 2: Input: 3 Output: False s

LeetCode 985 Sum of Even Numbers After Queries 解题报告

题目要求 We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index].  Then, the answer to the i-th query is the sum of the even values of A. (Here, the given inde

Leetcode 985. Sum of Even Numbers After Queries

简单题,按照它给的说法计算就行. class Solution: def sumEvenAfterQueries(self, A: List[int], queries: List[List[int]]) -> List[int]: s = sum(x for x in A if x % 2 == 0) ans = [] for v, i in queries: new = A[i] + v s += new if new % 2 == 0 else 0 s -= A[i] if A[i] %

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited

[leetcode]_Sum Root to Leaf Numbers

题目:计算一棵二叉树所有路径组成的数的总和. 思考:也是DFS的基础应用.虽然还是套着别人的DFS框架写的,但是学习通常会经历先模拟,再创新的过程. 代码: 1 private int sum = 0; 2 public int sumNumbers(TreeNode root) { 3 dfs(root , 0); 4 return sum; 5 } 6 public void dfs(TreeNode node , int tempSum){ 7 if(node == null) retur

LeetCode: Combination Sum [038]

[题目] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target)