diff --git a/app/src/main/java/com/leetcode_kotlin/Executing.kt b/app/src/main/java/com/leetcode_kotlin/Executing.kt index e1f02088..9ca2eb96 100644 --- a/app/src/main/java/com/leetcode_kotlin/Executing.kt +++ b/app/src/main/java/com/leetcode_kotlin/Executing.kt @@ -1,7 +1,5 @@ package com.leetcode_kotlin -import java.util.concurrent.ConcurrentHashMap - /** * Executing @@ -10,10 +8,6 @@ import java.util.concurrent.ConcurrentHashMap fun main() { - val concurrentHashMap = ConcurrentHashMap() - - concurrentHashMap.put(1, 5) - } diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index 39684a79..5c9e14a8 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -1,6 +1,7 @@ package com.github.contest +import com.github.contest.bitManipulation.hammingWeight import com.github.contest.math.numberOfPowerfulInt import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern import com.github.contest.slidingWindow.customStructure.slidingWindowClassic @@ -15,9 +16,7 @@ import java.util.TreeMap fun main() { - val str = "abcdefrt" - - str.chunked(3).also { println(it) } + val str: String? = "ghdirfghdi" } diff --git a/contest/src/main/java/com/github/contest/bitManipulation/BitManipulationLeetcode.kt b/contest/src/main/java/com/github/contest/bitManipulation/BitManipulationLeetcode.kt index e3a63857..2b75f210 100644 --- a/contest/src/main/java/com/github/contest/bitManipulation/BitManipulationLeetcode.kt +++ b/contest/src/main/java/com/github/contest/bitManipulation/BitManipulationLeetcode.kt @@ -22,4 +22,36 @@ fun subsetXORSum(nums: IntArray): Int { calculateSubsetXOR(0, 0) return totalXORSum +} + +/** + * 191. Number of 1 Bits + */ + +fun hammingWeight(n: Int): Int { + var num = n + var count = 0 + + while (num != 0) { + count += num and 1 + num = num ushr 1 + } + + return count +} + +/** + * 461. Hamming Distance + */ + +fun hammingDistance(x: Int, y: Int): Int { + var res = x xor y + var count = 0 + + while (res != 0) { + count += res and 1 + res = res ushr 1 + } + + return count } \ No newline at end of file diff --git a/contest/src/main/java/com/github/contest/twoPointer/TwoPointerLeetCode.kt b/contest/src/main/java/com/github/contest/twoPointer/TwoPointerLeetCode.kt index 6ea85f7d..11f9630a 100644 --- a/contest/src/main/java/com/github/contest/twoPointer/TwoPointerLeetCode.kt +++ b/contest/src/main/java/com/github/contest/twoPointer/TwoPointerLeetCode.kt @@ -48,4 +48,22 @@ fun trap(height: IntArray): Int { } return water +} + +/** + * 2200. Find All K-Distant Indices in an Array + */ + +fun findKDistantIndices(nums: IntArray, key: Int, k: Int): List { + val result = mutableSetOf() + for (j in nums.indices) { + if (nums[j] == key) { + val start = maxOf(0, j - k) + val end = minOf(nums.size - 1, j + k) + for (i in start..end) { + result.add(i) + } + } + } + return result.sorted() } \ No newline at end of file