我想知道是否有更简单的方法可以在数组中找到模式?
假设我正在寻找给定数组中的一种模式:A) 微笑、皱眉、微笑、皱眉等 B) 微笑、愤怒、皱眉、微笑、愤怒、皱眉等 C) 微笑、微笑、微笑
现在说给定的数组与模式 A 匹配:
生气,生气,生气,Smile, Frown, Smile, Frown, Smile, Frown,
生气,皱眉,生气,皱眉,微笑
突出显示的部分是与模式 A 匹配的部分以及我想要存储在列表中的部分。
现在我有这样的事情:
For each element in the array
check to see if element is smile
if element is smile, check to see if next element is frown
if element is smile and next element is frown - store away in a list
set a boolean saying we've found pattern A
if the boolean value is false and we did not find the smile frown pattern
For each element in the array
check to see if element is smile
if element is smile, check to see if next element is angry,
is next element is angry, check to see if next next element is frown
if element is smile, next element is angry, next next element is frown - store away in a list
set a boolean saying we've found pattern B
if boolean value is false for both finding pattern A and pattern B search for pattern C
有更好的方法吗?总感觉这个不好。。。。