File tree Expand file tree Collapse file tree 3 files changed +135
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 3 files changed +135
-0
lines changed Original file line number Diff line number Diff line change 1+ package leetcode_study
2+
3+ /*
4+ * 주어진 배열에서 자신 요소를 제외한 나머지 요소의 곱한 배열을 구하는 문제
5+ * 문제에세 O(n)의 시간 복잡도를 요구하였으나 방법이 떠오르지 않아 고민 후 정답 참조.
6+ * 기준 요소를 중심으로 왼쪽의 총 곱, 오른쪽의 총 곱을 진행하게 되었을 때, 문제를 O(n)의 시간 복잡도로 해결할 수 있음.
7+ * 시간 복잡도: O(n^2)
8+ * 공간 복잡도: O(n)
9+ * */
10+ fun productExceptSelf00 (nums : IntArray ): IntArray {
11+ val result = mutableListOf<Int >()
12+
13+ for (i in nums.indices) {
14+ var temp = 1
15+ for (j in nums.indices) {
16+ if (i == j) continue
17+ temp * = nums[j]
18+ }
19+ result.add(temp)
20+ }
21+ return result.toIntArray()
22+ }
23+
24+ /*
25+ * 시간 복잡도: O(n)
26+ * 공간 복잡도: O(n)
27+ * */
28+ fun productExceptSelf01 (nums : IntArray ): IntArray {
29+ val result = IntArray (nums.size)
30+
31+ var leftProduct = 1
32+ for (i in nums.indices) {
33+ result[i] = leftProduct
34+ leftProduct = leftProduct * nums[i]
35+ }
36+
37+ var rightProduct = 1
38+ for (i in nums.indices.reversed()) {
39+ result[i] = result[i] * rightProduct
40+ rightProduct = rightProduct * nums[i]
41+ }
42+ return result
43+ }
Original file line number Diff line number Diff line change 1+ package leetcode_study
2+
3+ /*
4+ * 주어진 32 bits unsigned integer를 뒤집는 문제
5+ * Bit 연산에 대한 개념이 전무해 String으로 치환 후 문제 해결
6+ * 작은 수는 표현할 수 있었지만 아래와 같이 문제를 해결할 경우 큰 수가 입력되었을 경우 부호 비트를 인식하여 음수로 표기합니다.
7+ * 또한 32 bit를 구성하기 위해 부족한 문자열을(자릿수) 추가하기 때문에 연산이 더해집니다.
8+ * */
9+ fun reverseBits1 (n : Int ):Int {
10+ val nStr = n.toString(2 )
11+ val totalLength = nStr.length
12+ var temp = " "
13+ if (totalLength != 32 ) {
14+ for (i in 0 until 32 - totalLength) {
15+ temp + = " 0"
16+ }
17+ }
18+ val fullBitString = temp + nStr
19+ var result = 0
20+
21+ for (i in (fullBitString.length - 1 ) downTo 0 ) {
22+ val eachBitValue = 2.0 .pow(i).toInt()
23+ if (fullBitString[i] == ' 0' ) {
24+ continue
25+ } else {
26+ result + = eachBitValue
27+ }
28+ }
29+ println (result.toString(2 ))
30+ return result
31+ }
32+
33+ /*
34+ * Bit 연산을 통한 Reverse Bit 구성
35+ * 시간 복잡도: O(32) (32비트의 숫자에 대해 반복)
36+ * 공간 복잡도: O(1) (상수 공간 사용)
37+ * */
38+ fun reverseBits (n : Int ): Int {
39+ var input = n
40+ var result = 0
41+
42+ for (i in 0 until 32 ) {
43+ // 결과에 현재 비트 추가
44+ result = (result shl 1 ) or (input and 1 )
45+ // 입력 비트를 오른쪽으로 이동
46+ input = input shr 1
47+ }
48+
49+ return result
50+ }
Original file line number Diff line number Diff line change 1+ package leetcode_study
2+
3+ /* *
4+ * 주어진 숫자 배열에서 두 개의 숫자를 더해 Target을 만들 수 있는 배열의 Index를 구하는 문제
5+ * 조합을 사용해 문제 해결
6+ * 시간 복잡도: O(n^2)
7+ * -> 두 번의 순회를 통해 모든 경우의 수를 계산하는 경우
8+ * 공간 복잡도: O(1)
9+ * -> 결과를 저장하는 result, 배열의 index를 가리키는 indices는 두 개의 값을 담기 때문에 O(1)
10+ */
11+ fun twoSum (nums : IntArray , target : Int ): IntArray {
12+ val result = IntArray (2 )
13+ val k = 2
14+ val maxSize = nums.size
15+ val indices = IntArray (k)
16+ for (i in 0 until k ) {
17+ indices[i] = i
18+ }
19+
20+ while (indices[k- 1 ] < maxSize) {
21+ if (nums[indices[0 ]] + nums[indices[1 ]] == target) {
22+ result[0 ] = indices[0 ]
23+ result[1 ] = indices[1 ]
24+ return result
25+ }
26+
27+ var i = k - 1
28+ while (i >= 0 && indices[i] == i + maxSize - k) {
29+ i--
30+ }
31+
32+ if (i >= 0 ) {
33+ indices[i]++
34+ for (j in i + 1 until k) {
35+ indices[j] = indices[j - 1 ] + 1
36+ }
37+ } else {
38+ break
39+ }
40+ }
41+ return result
42+ }
You can’t perform that action at this time.
0 commit comments