Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions reverse-bits/dahyeong-yun.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

reverse-bits/dahyeong-yun.java
/**
 * TC : O(1)
 *   - 32번의 루프를 2번 반복하므로 O(1)
 * SC : O(1)
 *   - 32칸 고정 길이의 stack이 필요하므로 O(1)
 */

class Solution {
    public int reverseBits(int n) {
        int answer = 0;
        Deque<Integer> stack = new ArrayDeque<>();

        for(int i = 0; i<32; i++) {
            stack.add(n % 2);
            n /= 2;
        }

        int j = 0;
        while(!stack.isEmpty()) {
            int bit = stack.getLast();
            stack.removeLast();
            answer += bit * Math.pow(2, j); 
            j += 1;
        }

        return answer;       
    }
}
  • 패턴: Stack / Queue, Bit Manipulation
  • 설명: 주어진 코드는 32비트 정수를 비트를 스택에 쌓고, 다시 꺼내며 자리수에 따라 반전된 비트를 구성한다. 비트 단위 조작과 스택 사용으로 비트 반전의 과정을 다룬다.

📊 시간/공간 복잡도 분석

복잡도
Time O(1)
Space O(1)

피드백: 고정된 32비트 길이의 루프와 고정 크기 스택으로 구성되어 있어 시간과 공간이 상수로 보장된다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* TC : O(1)
* - 32번의 루프를 2번 반복하므로 O(1)
* SC : O(1)
* - 32칸 고정 길이의 stack이 필요하므로 O(1)
*/

class Solution {
public int reverseBits(int n) {
int answer = 0;
Deque<Integer> stack = new ArrayDeque<>();

for(int i = 0; i<32; i++) {
stack.add(n % 2);
n /= 2;
}

int j = 0;
while(!stack.isEmpty()) {
int bit = stack.getLast();
stack.removeLast();
answer += bit * Math.pow(2, j);
j += 1;
}

return answer;
}
}
Loading