Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- string
- Number Theory
- 자바
- Tree
- two pointers
- geometry
- Counting
- Method
- SQL
- Binary Tree
- simulation
- Math
- sorting
- Data Structure
- java
- database
- Binary Search
- 코딩테스트
- array
- greedy
- dynamic programming
- Stack
- hash table
- 파이썬
- Class
- bit manipulation
- 구현
- 코테
- Matrix
- implement
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 190. Reverse Bits 본문
1. Input
1) 32bit의 unsigned 정수 n
2) 자바를 포함한 몇몇 언어에서는 signed 정수만 취급하기 때문에 맨 앞 bit가 1일 경우 달리 계산될 수 있으니 주의
2. Output
1) n의 비트를 전부 뒤집은 결과를 int 변수에 담아 반환
3. Constraint
1) n은 2진 문자열로 길이는 무조건 32이다.
4. Example
Input: n = 11111111111111111111111111111101
Output: 3221225471
설명:
- 2진수 문자열 11111111111111111111111111111101은 unsigned 정수 4294967293을 나타낸다.
- n의 비트를 다 뒤집으면 10111111111111111111111111111111이 되는데, 이는 unsigned 정수 3221225471을 나타낸다.
위 문제를 signed로 풀면 완전 달라진다.
- 2진수 문자열 11111111111111111111111111111101은 signed 정수 -3을 나타낸다.
- n의 비트를 다 뒤집으면 10111111111111111111111111111111이 되는데, 이는 signed 정수 -1073741825
를
나타낸다.
5. Code
1) 첫 코드(2022/07/22)
String s = Integer.toBinaryString(n);
long result = 0;
for(int exp=31 ; exp>=0 ; exp--)
result += (s.charAt(exp)-'0') * Math.pow(2,exp);
return (int)result;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 217. Contains Duplicate (0) | 2022.10.12 |
---|---|
[LeetCode/Easy] 191. Number of 1 Bits (0) | 2022.10.12 |
[LeetCode/Easy] 171. Excel Sheet Column Number (0) | 2022.10.12 |
[LeetCode/Easy] 136. Single Number (0) | 2022.10.11 |
[LeetCode/Easy] 125. Valid Palindrome (0) | 2022.10.11 |