-
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.
- Loading branch information
Showing
1 changed file
with
22 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
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; | ||
} | ||
} |