0806
This commit is contained in:
parent
47774ce9f6
commit
3939fe4557
7 changed files with 124 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
|||
fun main() {
|
||||
val t = Test20()
|
||||
val t = Test75()
|
||||
t.test()
|
||||
}
|
20
src/Test118.kt
Normal file
20
src/Test118.kt
Normal file
|
@ -0,0 +1,20 @@
|
|||
class Test118 {
|
||||
class Solution {
|
||||
fun generate(numRows: Int): List<List<Int>> {
|
||||
var result = arrayOf(listOf(1))
|
||||
for (i in 1..<numRows){
|
||||
val newLine = MutableList(i+1){1}
|
||||
for (j in 1..<i){
|
||||
newLine[j] = result[i-1][j-1]+result[i-1][j]
|
||||
}
|
||||
result += newLine.toList()
|
||||
}
|
||||
return result.toList()
|
||||
}
|
||||
}
|
||||
|
||||
fun test(): Unit {
|
||||
println(Solution().generate(5))
|
||||
println(Solution().generate(1))
|
||||
}
|
||||
}
|
11
src/Test136.kt
Normal file
11
src/Test136.kt
Normal file
|
@ -0,0 +1,11 @@
|
|||
class Test136 {
|
||||
class Solution {
|
||||
fun singleNumber(nums: IntArray): Int {
|
||||
var i = 0
|
||||
nums.forEach{
|
||||
i = i.xor(it)
|
||||
}
|
||||
return i
|
||||
}
|
||||
}
|
||||
}
|
7
src/Test169.kt
Normal file
7
src/Test169.kt
Normal file
|
@ -0,0 +1,7 @@
|
|||
class Test169 {
|
||||
class Solution {
|
||||
fun majorityElement(nums: IntArray): Int {
|
||||
return nums.sorted()[nums.size/2]
|
||||
}
|
||||
}
|
||||
}
|
31
src/Test198.kt
Normal file
31
src/Test198.kt
Normal file
|
@ -0,0 +1,31 @@
|
|||
import kotlin.math.max
|
||||
|
||||
class Test198 {
|
||||
class Solution {
|
||||
fun rob(nums: IntArray): Int {
|
||||
val cache = Array<Int>(nums.size){-1}
|
||||
fun gain(k: Int): Int {
|
||||
if (cache[k]!=-1){
|
||||
return cache[k]
|
||||
}
|
||||
cache[k]= when (k) {
|
||||
0 -> nums[0]
|
||||
1 -> max(nums[0],nums[1])
|
||||
else -> {
|
||||
max(gain(k-2)+nums[k],gain(k-1))
|
||||
}
|
||||
}
|
||||
return cache[k]
|
||||
}
|
||||
return gain(nums.size-1)
|
||||
}
|
||||
}
|
||||
|
||||
fun test(): Unit {
|
||||
println(Solution().rob(intArrayOf(1,2,3,1)))
|
||||
println(Solution().rob(intArrayOf(2,7,9,3,1)))
|
||||
println(Solution().rob(intArrayOf(2,1,1,2)))
|
||||
println(Solution().rob(intArrayOf(2,1)))
|
||||
println(Solution().rob(intArrayOf(114,117,207,117,235,82,90,67,143,146,53,108,200,91,80,223,58,170,110,236,81,90,222,160,165,195,187,199,114,235,197,187,69,129,64,214,228,78,188,67,205,94,205,169,241,202,144,240)))
|
||||
}
|
||||
}
|
21
src/Test70.kt
Normal file
21
src/Test70.kt
Normal file
|
@ -0,0 +1,21 @@
|
|||
class Test70 {
|
||||
class Solution {
|
||||
fun climbStairs(n: Int): Int {
|
||||
var last= 1 // 0 n-2
|
||||
var cur = 1 // 1 n-1
|
||||
var curCopy: Int
|
||||
for (i in 2..n){
|
||||
curCopy = cur
|
||||
cur += last
|
||||
last = curCopy
|
||||
}
|
||||
return cur
|
||||
}
|
||||
}
|
||||
|
||||
fun test(): Unit {
|
||||
println(Solution().climbStairs(2))
|
||||
println(Solution().climbStairs(3))
|
||||
println(Solution().climbStairs(4))
|
||||
}
|
||||
}
|
33
src/Test75.kt
Normal file
33
src/Test75.kt
Normal file
|
@ -0,0 +1,33 @@
|
|||
class Test75 {
|
||||
class Solution {
|
||||
fun sortColors(nums: IntArray): Unit {
|
||||
var l = 0
|
||||
var r = nums.size - 1
|
||||
var ptr = 0
|
||||
while (ptr <= r){
|
||||
when (nums[ptr]){
|
||||
0 -> {
|
||||
nums[ptr] = nums[l]
|
||||
nums[l] = 0
|
||||
l++
|
||||
}
|
||||
2 -> {
|
||||
nums[ptr] = nums[r]
|
||||
nums[r] = 2
|
||||
r--
|
||||
ptr --
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
ptr ++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun test(): Unit {
|
||||
Solution().sortColors(intArrayOf(2,0,2,1,1,0))
|
||||
Solution().sortColors(intArrayOf(2,0,1))
|
||||
Solution().sortColors(intArrayOf(1,2))
|
||||
Solution().sortColors(intArrayOf(1,2,0))
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue