Skip to content

Commit 7b794aa

Browse files
authored
Added tasks 476, 477, 478, 479.
1 parent 9b0270f commit 7b794aa

File tree

13 files changed

+273
-0
lines changed

13 files changed

+273
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package g0401_0500.s0476_number_complement;
2+
3+
// #Easy #Bit_Manipulation
4+
5+
public class Solution {
6+
public int findComplement(int num) {
7+
return ~num & ((Integer.highestOneBit(num) << 1) - 1);
8+
}
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
476\. Number Complement
2+
3+
Easy
4+
5+
The **complement** of an integer is the integer you get when you flip all the `0`'s to `1`'s and all the `1`'s to `0`'s in its binary representation.
6+
7+
* For example, The integer `5` is `"101"` in binary and its **complement** is `"010"` which is the integer `2`.
8+
9+
Given an integer `num`, return _its complement_.
10+
11+
**Example 1:**
12+
13+
**Input:** num = 5
14+
15+
**Output:** 2
16+
17+
**Explanation:** The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
18+
19+
**Example 2:**
20+
21+
**Input:** num = 1
22+
23+
**Output:** 0
24+
25+
**Explanation:** The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= num < 2<sup>31</sup></code>
30+
31+
**Note:** This question is the same as 1009: [https://leetcode.com/problems/complement-of-base-10-integer/](https://leetcode.com/problems/complement-of-base-10-integer/)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0401_0500.s0477_total_hamming_distance;
2+
3+
// #Medium #Array #Math #Bit_Manipulation
4+
5+
public class Solution {
6+
public int totalHammingDistance(int[] nums) {
7+
int ans = 0;
8+
int n = nums.length;
9+
for (int i = 0; i < 32; i++) {
10+
int ones = 0;
11+
for (int k : nums) {
12+
ones += ((k >> i) & 1);
13+
}
14+
ans = ans + (ones * (n - ones));
15+
}
16+
return ans;
17+
}
18+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
477\. Total Hamming Distance
2+
3+
Medium
4+
5+
The [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) between two integers is the number of positions at which the corresponding bits are different.
6+
7+
Given an integer array `nums`, return _the sum of **Hamming distances** between all the pairs of the integers in_ `nums`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [4,14,2]
12+
13+
**Output:** 6
14+
15+
**Explanation:** In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just showing the four bits relevant in this case). The answer will be: HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [4,14,4]
20+
21+
**Output:** 4
22+
23+
**Constraints:**
24+
25+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
26+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
27+
* The answer for the given input will fit in a **32-bit** integer.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0401_0500.s0478_generate_random_point_in_a_circle;
2+
3+
// #Medium #Math #Geometry #Randomized #Rejection_Sampling
4+
5+
import java.security.SecureRandom;
6+
7+
public class Solution {
8+
private final double radius;
9+
private final double xCenter;
10+
private final double yCenter;
11+
private final SecureRandom random = new SecureRandom();
12+
13+
public Solution(double radius, double xCenter, double yCenter) {
14+
15+
this.radius = radius;
16+
this.xCenter = xCenter;
17+
this.yCenter = yCenter;
18+
}
19+
20+
public double[] randPoint() {
21+
double x = getCoordinate(xCenter);
22+
double y = getCoordinate(yCenter);
23+
while (getDistance(x, y) >= radius * radius) {
24+
x = getCoordinate(xCenter);
25+
y = getCoordinate(yCenter);
26+
}
27+
return new double[] {x, y};
28+
}
29+
30+
private double getDistance(double x, double y) {
31+
return (xCenter - x) * (xCenter - x) + (yCenter - y) * (yCenter - y);
32+
}
33+
34+
private double getCoordinate(double center) {
35+
return center - radius + random.nextDouble() * 2 * radius;
36+
}
37+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
478\. Generate Random Point in a Circle
2+
3+
Medium
4+
5+
Given the radius and the position of the center of a circle, implement the function `randPoint` which generates a uniform random point inside the circle.
6+
7+
Implement the `Solution` class:
8+
9+
* `Solution(double radius, double x_center, double y_center)` initializes the object with the radius of the circle `radius` and the position of the center `(x_center, y_center)`.
10+
* `randPoint()` returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array `[x, y]`.
11+
12+
**Example 1:**
13+
14+
**Input** ["Solution", "randPoint", "randPoint", "randPoint"] [[1.0, 0.0, 0.0], [], [], []]
15+
16+
**Output:** [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]]
17+
18+
**Explanation:** Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248]
19+
20+
**Constraints:**
21+
22+
* <code>0 < radius <= 10<sup>8</sup></code>
23+
* <code>-10<sup>7</sup> <= x_center, y_center <= 10<sup>7</sup></code>
24+
* At most <code>3 * 10<sup>4</sup></code> calls will be made to `randPoint`.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0401_0500.s0479_largest_palindrome_product;
2+
3+
// #Hard #Math
4+
5+
public class Solution {
6+
public int largestPalindrome(int n) {
7+
long pow10 = (long) Math.pow(10, n);
8+
long max = (pow10 - 1) * (pow10 - (long) Math.sqrt(pow10) + 1);
9+
long left = (max / pow10);
10+
long t = pow10 / 11;
11+
t -= ~t & 1;
12+
for (long i = left; i > 0; i--) {
13+
for (long j = t, num = gen(i); j >= i / 11; j -= 2) {
14+
if (num % j == 0) {
15+
return (int) (num % 1337);
16+
}
17+
}
18+
}
19+
return 9;
20+
}
21+
22+
private long gen(long x) {
23+
long r = x;
24+
for (; x > 0; r = r * 10 + x % 10, x /= 10) {}
25+
return r;
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
479\. Largest Palindrome Product
2+
3+
Hard
4+
5+
Given an integer n, return _the **largest palindromic integer** that can be represented as the product of two `n`\-digits integers_. Since the answer can be very large, return it **modulo** `1337`.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 2
10+
11+
**Output:** 987 Explanation: 99 x 91 = 9009, 9009 % 1337 = 987
12+
13+
**Example 2:**
14+
15+
**Input:** n = 1
16+
17+
**Output:** 9
18+
19+
**Constraints:**
20+
21+
* `1 <= n <= 8`

src/test/java/com_github_leetcode/CommonUtils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ public static void printArray(int[] nums) {
1313
System.out.println();
1414
}
1515

16+
public static void printArray(double[] nums) {
17+
for (double i : nums) {
18+
System.out.print(i + ", ");
19+
}
20+
System.out.println();
21+
}
22+
1623
public static char[][] convertLeetCodeRegular2DCharArrayInputIntoJavaArray(String input) {
1724
/*
1825
* LeetCode 2-d char array usually comes in like this:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0401_0500.s0476_number_complement;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void findComplement() {
11+
assertThat(new Solution().findComplement(5), equalTo(2));
12+
}
13+
14+
@Test
15+
void findComplement2() {
16+
assertThat(new Solution().findComplement(1), equalTo(0));
17+
}
18+
}

0 commit comments

Comments
 (0)