Kotlin - 两个指针 - 嵌套 While 循环导致:线程“main”中出现异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1

问题描述 投票:0回答:1

在下面的代码示例中,我试图解决 Leetcode 上的 Longest Word in Dictionary Through Deleting 问题。我认为这个解决方案应该可行,但由于某种原因,下面的嵌套 while 循环似乎破坏了该功能,我不明白为什么。

我收到的错误是“第 20 行:线程“main”java.lang.StringIndexOutOfBoundsException 中出现异常:字符串索引超出范围:1。”话虽如此,当我进行多次检查以确保不会发生这种情况时,我根本不知道这个错误是如何发生的。此外,包装器 for 循环的调用次数似乎比字典中的索引数量还要多。

我发现最接近找出可能出现问题的方法是在包装器 For 循环的顶部放置一个 println(i) ,并注释掉包装器 Do - While 循环内指示的 While 循环。

当这些内部循环被取消注释时,该函数似乎正在获取答案,但 for 循环打印 0,1,2,3,0 并且该函数返回错误“第 20 行:线程“main”中的异常” java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1。”当我注释掉这些循环时,该函数会正确打印 0,1,2,3 并完成返回错误答案。

我不知道为什么这些循环会导致包装器 For 循环从 0 开始运行额外的次数并返回 1 的错误。这还是问题吗?

class Solution {
    fun findLongestWord(s: String, dictionary: List<String>): String {
        var answer = ""

        for (i in dictionary.indices) {
            
            // Printing i to test the iterations of function
            // Prints 0, 1, 2, 3 when below while loops are commented out
            // Prints more numbers past 3 when the below two while loops aren't commented out.
            println(i)
            if (answer.length > dictionary[i].length) {
                
                continue
            }
            var tempString = s.toCharArray().toMutableList()
            
            var low = 0
            var dictLow = 0
            var high = tempString.lastIndex
            var dictHigh = dictionary[i].lastIndex
          
            do {
                // Commenting out the below two while loops causes function to work
                while (tempString.size >= answer.length && tempString.isNotEmpty() && tempString[low] != dictionary[i][dictLow] ) {
                    tempString.removeAt(low)
                    high -= 1

                }

                while (tempString.size >= answer.length && tempString.isNotEmpty() && tempString[high] != dictionary[i][dictHigh]) {
                    tempString.removeAt(high)
                    high -= 1
                }

                if (tempString.joinToString("") == dictionary[i]) {
                    var potentialAnswer = tempString.joinToString("")
                    if (potentialAnswer.length > answer.length) {
                        answer = potentialAnswer
                    } else if (potentialAnswer.length == answer.length) {
                        var list = mutableListOf(potentialAnswer, answer).sorted()
                        answer = list[0]
                    }
                    break
                }
                else {
                    low += 1
                    high -= 1
                    dictLow += 1
                    dictHigh -= 1
                }
                

            } while (tempString.size > answer.length && low < high) 
        }

        return answer
    }
}
kotlin for-loop pointers while-loop indexoutofboundsexception
1个回答
0
投票

我不明白你想要实现什么目标,但这里有一些需要检查的内容:

while (tempString.size >= answer.length 
&& tempString.isNotEmpty() && tempString[low] != dictionary[i][dictLow] ) {

我认为唯一可以提高

StringIndexOutOfBoundsException
的部分是
dictionary[i][dictLow]
。您开始
dictLow=0
并稍后递增它,但是您没有保护条件,因此我认为,您不可避免地会遇到一个字符数少于
dictLow
的字符串...因此出现异常

© www.soinside.com 2019 - 2024. All rights reserved.