This commit is contained in:
Kagura 2024-08-05 10:14:20 +08:00
parent 213f2d5969
commit 47774ce9f6
9 changed files with 231 additions and 9 deletions

View file

@ -1,4 +1,4 @@
fun main() {
val t = Test35()
val t = Test20()
t.test()
}

32
src/Test11.kt Normal file
View file

@ -0,0 +1,32 @@
import kotlin.math.max
import kotlin.math.min
class Test11 {
class Solution {
fun maxArea(height: IntArray): Int {
fun getVolume(left:Int,right: Int): Int{
return min(height[left],height[right]) * (right - left)
}
var left = 0
var right = height.size - 1
var maxVol = getVolume(left,right)
while (right > left){
if (height[left] <= height[right]){
left ++
maxVol = max(maxVol,getVolume(left,right))
}else{
right --
maxVol = max(maxVol,getVolume(left,right))
}
}
return maxVol
}
}
fun test(){
println(Solution().maxArea(intArrayOf(1,8,6,2,5,4,8,3,7)))
println(Solution().maxArea(intArrayOf(1,1)))
println(Solution().maxArea(intArrayOf(1,3,2,5,25,24,5)))
}
}

25
src/Test128.kt Normal file
View file

@ -0,0 +1,25 @@
class Test128 {
class Solution {
fun longestConsecutive(nums: IntArray): Int {
if (nums.isEmpty()) return 0
if (nums.size == 1) return 1
val sorted = nums.sorted()
var longest = 1
var long = 1
for (i in 1..<sorted.size) {
if (sorted[i] == sorted[i - 1] +1) {
long ++
} else if (sorted[i] == sorted[i - 1]){
} else{
longest = maxOf(longest, long)
long = 1
}
}
return maxOf(longest, long)
}
}
fun test(){
println(Solution().longestConsecutive(intArrayOf(0,3,7,2,5,8,4,6,0,1)))
}
}

37
src/Test15.kt Normal file

File diff suppressed because one or more lines are too long

44
src/Test20.kt Normal file
View file

@ -0,0 +1,44 @@
class Test20 {
class Solution {
fun isValid(s: String): Boolean {
val stack = ArrayDeque(listOf<Char>())
for (c in s) {
when(c){
')' ->{
if (stack.isEmpty() || stack.last() != '('){
return false
}else{
stack.removeLast()
}
}
'}' ->{
if (stack.isEmpty() || stack.last() != '{'){
return false
}else{
stack.removeLast()
}
}
']' ->{
if (stack.isEmpty() || stack.last() != '['){
return false
}else{
stack.removeLast()
}
}
else -> {
stack.addLast(c)
}
}
}
return stack.isEmpty()
}
}
fun test(): Unit {
println(Solution().isValid("()"))
println(Solution().isValid("()[]{}"))
println(Solution().isValid("(]"))
println(Solution().isValid("]"))
}
}

View file

@ -1,7 +1,31 @@
import kotlin.math.max
class Test3 {
class Solution {
fun lengthOfLongestSubstring(s: String): Int {
if (s.length < 2){
return s.length
}
var l = 0
var r = 0
var maxLen = 1
while (r < s.length - 1) {
val next = s[r + 1]
for (i in l..r) {
if (s[i] == next) {
l = i + 1
break
}
}
maxLen = max(maxLen, r - l + 2)
r++
}
return maxLen
}
}
fun test(){
println(Solution().lengthOfLongestSubstring("abcabcbb"))
println(Solution().lengthOfLongestSubstring("bbbbb"))
println(Solution().lengthOfLongestSubstring("pwwkew"))
}
}

30
src/Test42.kt Normal file
View file

@ -0,0 +1,30 @@
class Test42 {
class Solution {
fun trap(height: IntArray): Int {
var l = 0
var r = height.size - 1
var size = 0
for (level in 1..height.max()){
while (height[l] < level){
l++
}
while (height[r] < level){
r--
}
if (l > r){
break
}
for (mid in l+1..<r){
if (height[mid] < level){
size++
}
}
}
return size
}
}
fun test(){
println(Solution().trap(intArrayOf(0,1,0,2,1,0,1,3,2,1,2,1)))
println(Solution().trap(intArrayOf(4,2,0,3,2,5)))
}
}

34
src/Test438.kt Normal file
View file

@ -0,0 +1,34 @@
class Test438 {
class Solution {
fun findAnagrams(s: String, p: String): List<Int> {
val appeared = ShortArray('z'-'a'+1) {0}
for (c in p) {
appeared[c.code-'a'.code] ++
}
var result = intArrayOf()
var curAppeared = ShortArray('z'-'a'+1) {0}
for (i in 0..s.length-p.length){
for (j in p.indices){
if (appeared[s[i+j].code-'a'.code] == 0.toShort()){
curAppeared = ShortArray('z'-'a'+1) {0}
break
}else{
curAppeared[s[i+j].code-'a'.code] ++
}
}
if (curAppeared.contentEquals(appeared)){
result += i
}
curAppeared = ShortArray('z'-'a'+1) {0}
}
return result.toList()
}
}
fun test(){
println(Solution().findAnagrams("cbaebabacd","abc"))
println(Solution().findAnagrams("abab","ab"))
}
}

View file

@ -1,10 +1,6 @@
class Test49 {
fun groupAnagrams(strs: Array<String>): List<List<String>> {
var map = HashMap<Int,Int>() // Index Identifier
return listOf(listOf(""))
return strs.groupBy { it.toCharArray().sorted() }.values.toList()
}