-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a solution for the daily problem of S0633
- Loading branch information
1 parent
54ae935
commit c94c210
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 633. Sum of Square Numbers | ||
|
||
Topics: Math, Two Pointers, Binary Search | ||
|
||
Given a non-negative integer c, decide whether there are two integers a and b such that a2 + b2 = c. | ||
|
||
### Example 1: | ||
|
||
Input: c = 5 | ||
Output: true | ||
Explanation: 1 * 1 + 2 * 2 = 5 | ||
|
||
### Example 2: | ||
|
||
Input: c = 3 | ||
Output: false | ||
|
||
### Constraints: | ||
|
||
0 <= c <= 2^31 - 1 | ||
|
18 changes: 18 additions & 0 deletions
18
src/main/java/com/anthonyschuijlenburg/S0633/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.anthonyschuijlenburg.S0633; | ||
|
||
public class Solution { | ||
public boolean judgeSquareSum(int c) { | ||
double max = Math.sqrt(c); | ||
|
||
for (int i = 0; i <= max; i++) { | ||
double result = Math.sqrt(c - (i * i)); | ||
|
||
// Check if result is a whole number | ||
if (result == (int) result) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/test/java/com/anthonyschuijlenburg/S0633/SolutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.anthonyschuijlenburg.S0633; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class SolutionTest { | ||
@Test | ||
void judgeSquareSum_testCase0() { | ||
Solution solutionGenerator = new Solution(); | ||
|
||
boolean result = solutionGenerator.judgeSquareSum(0); | ||
assertTrue(result); | ||
} | ||
|
||
@Test | ||
void judgeSquareSum_testCase1() { | ||
Solution solutionGenerator = new Solution(); | ||
|
||
boolean result = solutionGenerator.judgeSquareSum(3); | ||
assertFalse(result); | ||
} | ||
|
||
@Test | ||
void judgeSquareSum_testCase2() { | ||
Solution solutionGenerator = new Solution(); | ||
|
||
boolean result = solutionGenerator.judgeSquareSum(5); | ||
assertTrue(result); | ||
} | ||
|
||
@Test | ||
void judgeSquareSum_testCase3() { | ||
Solution solutionGenerator = new Solution(); | ||
|
||
boolean result = solutionGenerator.judgeSquareSum(4); | ||
assertTrue(result); | ||
} | ||
|
||
@Test | ||
void judgeSquareSum_testCase5() { | ||
Solution solutionGenerator = new Solution(); | ||
|
||
boolean result = solutionGenerator.judgeSquareSum(1); | ||
assertTrue(result); | ||
} | ||
|
||
@Test | ||
void judgeSquareSum_testCase6() { | ||
Solution solutionGenerator = new Solution(); | ||
|
||
// Largest available number (constraints) | ||
boolean result = solutionGenerator.judgeSquareSum(Integer.MAX_VALUE - 1); | ||
assertFalse(result); | ||
} | ||
} |