File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed
Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ // 풀이
2+ // result 가장 오른쪽 값에 num의 값을 추가하면서 왼쪽으로 밀어 reverse 처리.
3+
4+ // TC
5+ // for문은 무조건 32회만 돌기때문에 O(1)
6+
7+ // SC
8+ // 추가적인 공간 사용량은 일정하므로 0(1)
9+
10+ func reverseBits (num uint32 ) uint32 {
11+ result := uint32 (0 )
12+ for i := 0 ; i < 32 ; i ++ {
13+ result <<= 1
14+ if num % 2 == 1 {
15+ result ++
16+ }
17+ num >>= 1
18+ }
19+ return result
20+ }
Original file line number Diff line number Diff line change 1+ // 풀이
2+ // map의 key에 값, value에 index를 넣어, target-num이 map에 존재할 때를 찾기.
3+ // 오직 하나의 답만 무조건 존재한다고 했기 때문에 찾자마자 return.
4+
5+ // TC
6+ // 가장 마지막 인덱스까지 가서야 값을 구할 수 있었다고 하면, nums의 길이만큼 for문을 한바퀴 돌기때문에 O(n).
7+
8+ // SC
9+ // 중복없이 마지막 인덱스까지 가서 값을 구한다면 들어오는 nums의 길이만큼 map도 공간을 차지하게 되므로 O(n).
10+
11+ func twoSum (nums []int , target int ) []int {
12+ m := make (map [int ]int )
13+ for i , num := range nums {
14+ if index , ok := m [target - num ]; ok {
15+ return []int {index , i }
16+ }
17+ m [num ] = i
18+ }
19+ return []int {0 , 0 }
20+ }
You can’t perform that action at this time.
0 commit comments