gpt4 book ai didi

java - 在数组中查找模式

转载 作者:搜寻专家 更新时间:2023-11-01 01:54:09 24 4
gpt4 key购买 nike

我想知道是否有更简单的方法来查找数组中的模式?

假设我正在寻找给定数组中的一种模式:A) 微笑、皱眉、微笑、皱眉等B) 微笑、生气、皱眉、微笑、生气、皱眉等C)微笑,微笑,微笑

现在假设给定的数组与模式 A 匹配:

愤怒,愤怒,愤怒,微笑,皱眉,微笑,皱眉,微笑,皱眉, 愤怒,皱眉,愤怒,皱眉,微笑

突出显示的部分是与模式 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

有更好的方法吗?我觉得这总体上很糟糕....

最佳答案

您可以将数组转换为字符串并将其与任何正则表达式模式进行匹配。

UPD:可能是 prefix tree能帮你。首先将你所有的模式添加到一个特里树中,然后再次匹配你的数组。但这很像一个本土的正则表达式引擎。

关于java - 在数组中查找模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15251706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com