This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<List<Integer>> combine(int n, int k) { | |
List<List<Integer>> result = new ArrayList<>(); | |
Deque<Integer> path = new ArrayDeque<>(); | |
backtrace(result, 1, path, k, n); | |
return result; | |
} | |
private void backtrace(List<List<Integer>> result, int index, Deque<Integer> path, int k, int n) { | |
if(path.size() == k) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<List<Integer>> subsetsWithDup(int[] nums) { | |
List<List<Integer>> result = new ArrayList<>(); | |
Arrays.sort(nums); | |
Deque<Integer> path = new ArrayDeque<>(); | |
backtrace(result, nums, path, 0); | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kotlin | |
class Solution { | |
fun kthSmallest(root: TreeNode?, k: Int): Int { | |
if(root == null) return 0 | |
val leftSize = counter(root.left) | |
if(leftSize == (k-1)) return root.`val` | |
else if(leftSize < (k-1)) return kthSmallest(root.right, k-leftSize-1) | |
else return kthSmallest(root.left, k) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kotlin | |
class Solution { | |
fun myPow(x: Double, n: Int): Double { | |
if(x == 0.0 || x == 1.0) return x | |
return if(n >= 0) | |
quickN(x, n) | |
else | |
(1/quickN(x, -(n+1)))/x | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kotlin | |
class Solution { | |
fun search(nums: IntArray, target: Int): Int { | |
var start = 0 | |
var end = nums.size - 1 | |
while(start <= end) { | |
val middle = start + (end - start) / 2 | |
if(nums[middle] == target) return middle | |
else if(nums[middle] > target) { | |
end = middle - 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kotlin | |
class Solution { | |
fun searchInsert(nums: IntArray, target: Int): Int { | |
var start = 0 | |
var end = nums.size - 1 | |
while(start < end) { | |
val middle = start + (end - start) / 2 | |
if(nums[middle] == target) return middle | |
else if(nums[middle] > target) { | |
end = middle - 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kotlin | |
class Solution: VersionControl() { | |
override fun firstBadVersion(n: Int) : Int { | |
var start = 1 | |
var end = n | |
while(start < end) { | |
val middle = start + (end - start) / 2 | |
if(isBadVersion(middle)) { | |
end = middle | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kotlin | |
class Solution { | |
fun mySqrt(x: Int): Int { | |
var start = 1 | |
var end = x | |
while(start <= end) { | |
val middle = start + (end - start) / 2 | |
val temp = middle.toLong() * middle.toLong() | |
if(temp > x.toLong()) { | |
end = middle - 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kotlin | |
class Solution { | |
fun characterReplacement(s: String, k: Int): Int { | |
var start = 0 | |
var end = 0 | |
val counterArray = IntArray(26) { 0 } | |
var result = 0 | |
while(end < s.length) { | |
val endChar = s[end] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kotlin | |
class Solution { | |
fun lengthOfLongestSubstring(s: String): Int { | |
val set: MutableSet<Char> = mutableSetOf() | |
var result = if(s.length == 0) 0 else 1 | |
var end = -1 | |
for(start in 0 until s.length) { | |
if(start != 0) set.remove(s[start-1]) | |
while(end + 1 < s.length && !set.contains(s[end+1])) { | |
set.add(s[end+1]) |
NewerOlder