-
-
Notifications
You must be signed in to change notification settings - Fork 245
[gmlwls96] Week3 #764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[gmlwls96] Week3 #764
Changes from 3 commits
f3cdc7b
471cb9d
26b6991
6e058a5
e990fe7
d4b4783
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution { | ||
// you need treat n as an unsigned value | ||
fun reverseBits(n: Int): Int { | ||
var bitString = Integer.toBinaryString(n) | ||
bitString = CharArray(32 - bitString.length) { '0' }.concatToString() + bitString | ||
var result = 0 | ||
var scale = 1 | ||
bitString.forEach { | ||
result += it.digitToInt() * scale | ||
scale *= 2 | ||
} | ||
return result | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Solution { | ||
// 시간 : O(logN), 공간(2N) | ||
|
||
fun twoSum(nums: IntArray, target: Int): IntArray { | ||
val sortNums = List(nums.size) { listOf(nums[it], it) }.sortedBy { it[0] } | ||
// 1. list( list('값', 'index')) 형태의 list를 만들고 값을 기준으로 정렬한다. | ||
|
||
var i = 0 | ||
var j = sortNums.lastIndex | ||
// 2. 2포인터 방식으로 두 값을 합했을때 target이 되는 값을 찾는다. | ||
while (i < j) { | ||
val sum = sortNums[i][0] + sortNums[j][0] | ||
when { | ||
sum == target -> { // target과 sum이 일치할시 바로 return. | ||
return intArrayOf( | ||
min(sortNums[i][1], sortNums[j][1]), | ||
max(sortNums[i][1], sortNums[j][1]) | ||
) | ||
} | ||
sum < target -> { // sum이 target보다 값이 작은경우 i를 한칸씩 더한다. | ||
i++ | ||
} | ||
sum > target -> { // sum이 target보다 값이 큰경우 j를 한칸씩 내린다. | ||
j-- | ||
} | ||
} | ||
} | ||
return intArrayOf() | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.