编程语言
首页 > 编程语言> > 刷题:简单----求b是否为a的子集(python&java)

刷题:简单----求b是否为a的子集(python&java)

作者:互联网

a = [5, 1, 22, 25, 6, -1, 8, 10]
b = [1, 6, -1, 10]

Java
重点:
size()//list里元素的个数
get(i)//i处的值

import java.util.*;

class Program {
  public static boolean isValidSubsequence(List<Integer> array, List<Integer> sequence) {
    int i = 0;
	int j = 0;
	while(i < array.size() && j < sequence.size()){
		if(array.get(i).equals(sequence.get(j))){
			j ++;
		}
		i ++;
	}
	return j == sequence.size();
  }
}

python

def isValidSubsequence(array, sequence):
	idx1 = 0
	idx2 = 0
	while idx1 < len(array) and idx2 < len(sequence):
		if array[idx1] == sequence[idx2]:
			idx2 += 1
		idx1 += 1
	return idx2 == len(sequence)
			

标签:java,sequence,python,get,----,idx2,array,idx1,size
来源: https://blog.csdn.net/goldseeker1/article/details/114818370