Skip to content

Commit

Permalink
CT_285
Browse files Browse the repository at this point in the history
  • Loading branch information
Jade-Good committed Oct 18, 2024
1 parent d91e333 commit be3fed8
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions PGM/Java/src/lv2/_2개이하로다른비트.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package lv2;

public class _2개이하로다른비트 {
public long[] solution(long[] numbers) {
long[] result = new long[numbers.length];

for (int i = 0; i < numbers.length; i++) {
long x = numbers[i];

// 짝수인 경우
if (x % 2 == 0) {
result[i] = x + 1;
} else {
// 홀수인 경우, 가장 낮은 0 비트를 찾고 그 오른쪽 1 비트를 바꾼다.
long lowestZeroBit = (~x) & (x + 1);
result[i] = x + lowestZeroBit / 2;
}
}

return result;
}
}

0 comments on commit be3fed8

Please sign in to comment.