Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitae68f30

Browse files
add 478
1 parentcfd7b05 commitae68f30

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ _If you like this project, please leave me a star._ ★
681681
|480|[Sliding Window Median](https://leetcode.com/problems/sliding-window-median/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_480.java) | |Hard| Heap
682682
|479|[Largest Palindrome Product](https://leetcode.com/problems/largest-palindrome-product/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_479.java)||Easy|
683683
|477|[Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_477.java) | |Medium| Bit Manipulation
684+
|478|[Generate Random Point in a Circle](https://leetcode.com/problems/generate-random-point-in-a-circle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_478.java) | | Medium| Math, Random, Rejection Sampling
684685
|476|[Number Complement](https://leetcode.com/problems/number-complement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_476.java) | | Easy| Bit Manipulation
685686
|475|[Heaters](https://leetcode.com/problems/heaters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_475.java) | |Easy | Array Binary Search
686687
|474|[Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_474.java) | |Medium| DP
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
packagecom.fishercoder.solutions;
2+
3+
publicclass_478 {
4+
publicstaticclassSolution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/generate-random-point-in-a-circle/discuss/154037/Polar-Coordinates-10-lines
7+
* and
8+
* https://leetcode.com/problems/generate-random-point-in-a-circle/discuss/155650/Explanation-with-Graphs-why-using-Math.sqrt()
9+
*/
10+
doubleradius,xCenter,yCenter;
11+
12+
publicSolution1(doubleradius,doublexCenter,doubleyCenter) {
13+
this.radius =radius;
14+
this.xCenter =xCenter;
15+
this.yCenter =yCenter;
16+
}
17+
18+
publicdouble[]randPoint() {
19+
doublelen =Math.sqrt(Math.random()) *radius;
20+
doubledegree =Math.random() *2 *Math.PI;
21+
doublex =xCenter +len *Math.cos(degree);
22+
doubley =yCenter +len *Math.sin(degree);
23+
returnnewdouble[]{x,y};
24+
}
25+
}
26+
27+
publicstaticclassSolution2 {
28+
/**
29+
* How to know one point is within a circle:
30+
* https://www.geeksforgeeks.org/find-if-a-point-lies-inside-or-on-circle/
31+
*/
32+
doubletop;
33+
doublebottom;
34+
doubleleft;
35+
doubleright;
36+
doublerad;
37+
doublexCenter;
38+
doubleyCenter;
39+
40+
publicSolution2(doubleradius,doublexCenter,doubleyCenter) {
41+
this.rad =radius;
42+
this.top =yCenter +radius;
43+
this.bottom =yCenter -radius;
44+
this.left =xCenter -radius;
45+
this.right =xCenter +radius;
46+
this.xCenter =xCenter;
47+
this.yCenter =yCenter;
48+
}
49+
50+
publicdouble[]randPoint() {
51+
double[]result =newdouble[2];
52+
result[0] = (Math.random() * (right -left)) +left;
53+
result[1] = (Math.random() * (top -bottom)) +bottom;
54+
while (Math.pow(result[0] -xCenter,2.0) +Math.pow(result[1] -yCenter,2.0) >Math.pow(rad,2.0)) {
55+
result[0] = (Math.random() * (right -left)) +left;
56+
result[1] = (Math.random() * (top -bottom)) +bottom;
57+
}
58+
returnresult;
59+
}
60+
}
61+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._478;
5+
importorg.junit.Test;
6+
7+
publicclass_478Test {
8+
privatestatic_478.Solution1solution1;
9+
10+
@Test
11+
publicvoidtest1() {
12+
solution1 =new_478.Solution1(10.0,5.0, -7.5);
13+
CommonUtils.printArray(solution1.randPoint());
14+
}
15+
16+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp