Finish 10 this week
This commit is contained in:
parent
c765d131ec
commit
213f2d5969
3 changed files with 64 additions and 3 deletions
|
@ -1,4 +1,4 @@
|
|||
fun main() {
|
||||
val t = Test226()
|
||||
val t = Test35()
|
||||
t.test()
|
||||
}
|
|
@ -9,11 +9,23 @@ class Test101 {
|
|||
return true
|
||||
}
|
||||
|
||||
var pl = root.left
|
||||
var pr = root.right
|
||||
return isSame(root.left,root.right)
|
||||
}
|
||||
|
||||
|
||||
fun isSame(l: TreeNode?,r:TreeNode?):Boolean{
|
||||
if ((l==null && r!=null) || (r==null && l!=null)){
|
||||
return false
|
||||
}
|
||||
if (l==null && r==null){
|
||||
return true
|
||||
}
|
||||
|
||||
if (l!!.`val`!=r!!.`val`){
|
||||
return false
|
||||
}
|
||||
|
||||
return isSame(l.left,r.right) && isSame(l.right,r.left)
|
||||
|
||||
}
|
||||
}
|
||||
|
|
49
src/Test35.kt
Normal file
49
src/Test35.kt
Normal file
|
@ -0,0 +1,49 @@
|
|||
class Test35 {
|
||||
class Solution {
|
||||
fun searchInsert(nums: IntArray, target: Int): Int {
|
||||
if (nums.isEmpty()){
|
||||
return 0
|
||||
}
|
||||
return binSearch(nums,0,nums.size,target)
|
||||
}
|
||||
|
||||
fun binSearch(nums: IntArray,low:Int,high:Int,target: Int):Int{
|
||||
if (low+1==high){
|
||||
if (target<=nums[low]){
|
||||
return low
|
||||
}
|
||||
return high
|
||||
}
|
||||
|
||||
if (target < nums[(low+high)/2]){
|
||||
return binSearch(nums,low,(low+high)/2,target)
|
||||
}else if (target > nums[(low+high)/2]){
|
||||
return binSearch(nums,(low+high)/2,high,target)
|
||||
}
|
||||
return (low+high)/2
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun test() {
|
||||
val solution = Solution()
|
||||
|
||||
val testCases = arrayOf(
|
||||
Pair(intArrayOf(1, 3, 5, 6), 5) to 2,
|
||||
Pair(intArrayOf(1, 3, 5, 6), 2) to 1,
|
||||
Pair(intArrayOf(1, 3, 5, 6), 7) to 4,
|
||||
Pair(intArrayOf(1, 3, 5, 6), 0) to 0,
|
||||
Pair(intArrayOf(1), 1) to 0,
|
||||
Pair(intArrayOf(), 1) to 0
|
||||
)
|
||||
|
||||
for ((input, expected) in testCases) {
|
||||
val (nums, target) = input
|
||||
val result = solution.searchInsert(nums, target)
|
||||
println("Input: nums=${nums.contentToString()}, target=$target, Output: $result, Expected: $expected")
|
||||
assert(result == expected) { "Test failed for input nums=${nums.contentToString()}, target=$target" }
|
||||
}
|
||||
|
||||
println("All tests passed!")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue