简体   繁体   English

AlgoExpert:验证子序列,不通过所有测试用例

[英]AlgoExpert: Validate Subsequence, not passing all test cases

AlgoExpert 问题提示

Question: Validate Subsequence on AlgoExpert.问题:在 AlgoExpert 上验证子序列。 Given two non-empty arrays of integers write a function that determines whether the second array is a subsequence of the first one.给定两个非空整数数组,编写一个函数来确定第二个数组是否是第一个数组的子序列。

My code is not passing all test cases, what am I doing wrong?我的代码没有通过所有测试用例,我做错了什么?

public static boolean isValidSubsequence(List<Integer> array, List<Integer> sequence) {
    int arrayIndex = 0;
    int sequenceIndex = 0;
    
    while(arrayIndex < array.size() && sequenceIndex < sequence.size()){
        if(array.get(arrayIndex).equals(sequence.get(sequenceIndex))){
            sequenceIndex++;
        } else {
            arrayIndex++;
        }
    }
    if(sequenceIndex == (sequence.size())){
        return true;
    }
    return false;
}

If you have matched a character in both arrays, you should be moving both pointers instead of just one.如果你在两个数组中都匹配了一个字符,你应该移动两个指针而不是一个。

if(array.get(arrayIndex).equals(sequence.get(sequenceIndex))){
    sequenceIndex++;
}
arrayIndex++;        

Thanks for everyone's help!感谢大家的帮助! Updated Solution:更新的解决方案:

  public static boolean isValidSubsequence(List<Integer> array, List<Integer> sequence) {
    int arrayIndex = 0;
    int sequenceIndex = 0;
    
    while(arrayIndex < array.size() && sequenceIndex < sequence.size()){
        if(array.get(arrayIndex).equals(sequence.get(sequenceIndex))){
            sequenceIndex++;
            arrayIndex++;       
    } else {
            arrayIndex++;
        }
    if(sequenceIndex == (sequence.size())){
        return true;
    }
}
            return false;
}

This solution passed all the tests, cheer!该解决方案通过了所有测试,加油!

 import java.util.*; class Program { public static boolean isValidSubsequence(List<Integer> array, List<Integer> sequence) { // Write your code here. int k=0; int truecounter=0; int totalsize=sequence.size(); for(int i=0; i<array.size(); i++){ if(k<totalsize){ int squencearray_value = sequence.get(k); if(squencearray_value == array.get(i)){ truecounter=truecounter+1; k++; } }else{ if(truecounter==totalsize) return true; else return false; } } if(truecounter==totalsize) return true; else return false; // return false; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hashmap 代码未通过所有测试用例 - Hashmap code not passing all the test cases HackerRank - No Prefix Set 未通过所有测试用例 - HackerRank - No Prefix Set not passing all the test cases 在 Valid Anagram 程序中未通过所有测试用例 - in Valid Anagram program not passing all test cases 当所有测试用例运行时,我的测试用例失败。 但是单独跑的时候通过 - My test cases fail when all test cases are run. But passing when ran individually 代码没有在LeetCode#451上通过最终测试用例,是否可用于所有其他测试用例? - Code not passing final test case on LeetCode #451, working for all other test cases? 竞争编码-以最小的成本清除所有级别:未通过所有测试用例 - Competitive Coding - Clearing all levels with minimum cost : Not passing all test cases 无法验证集成测试用例中的日期类型 - Unable to validate date type in Integration test cases 为什么我的联合查找不交集集算法不能通过所有测试用例? - Why is my Union Find Disjoint Sets algo for this problem not passing all test cases? 最大子序列乘积特例 - maximum subsequence product special cases Selenium MoveToElement 不工作但测试用例通过 - Selenium MoveToElement does not work but test cases are passing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM