+ -
当前位置:首页 → 问答吧 → java array

java array

时间:2013-11-24

来源:互联网

如果要用array做到好似下面咁应该点写?
我唔知点样去match 啲数字

Enter list: 5 2 5 6 6 1
Enter pattern: 66
There is 1 match

Enter list: 7 5 5 6 6 1 6 6Enter pattern: 66There are 2 matches

作者: hahawingy   发布时间: 2013-11-24

引用:原帖由 hahawingy 於 2013-10-20 07:54 PM 发表
如果要用array做到好似下面咁应该点写?
我唔知点样去match 啲数字

Enter list: 5 2 5 6 6 1
Enter pattern: 66
There is 1 match

Enter list: 7 5 5 6 6 1 6 6Enter pattern: 66There are 2 matches
It may be work:
复制内容到剪贴板代码:int[] list = {5, 2, 5, 6, 6, 1};
int[] pat = {6, 6};
int matchCount = 0;
int i, j = 0;

for(i=0; i<list.length; i++) {
if(list[i] == pat[j]) {
j++;
if(j == pat.length) { // matches full pattern
j = 0;
matchCount++;
}
} else {
j = 0;
}
}
[ 本帖最后由 assembly.jc 於 2013-10-20 10:38 PM 编辑 ]

作者: assembly.jc   发布时间: 2013-11-24

I don't think it works. Try finding the pattern {6, 1}.

作者: fitcat07   发布时间: 2013-11-24

引用:原帖由 fitcat07 於 2013-10-21 11:00 AM 发表
I don't think it works. Try finding the pattern {6, 1}.
如果list 同pattern 既range 系0-9呢

作者: hahawingy   发布时间: 2013-11-24

引用:原帖由 hahawingy 於 2013-10-20 07:54 PM 发表
如果要用array做到好似下面咁应该点写?
我唔知点样去match 啲数字

Enter list: 5 2 5 6 6 1
Enter pattern: 66
There is 1 match

Enter list: 7 5 5 6 6 1 6 6Enter pattern: 66There are 2 matches
.... Your homework?

作者: erichk112   发布时间: 2013-11-24

引用:原帖由 hahawingy 於 2013-10-22 01:35 PM 发表



如果list 同pattern 既range 系0-9呢



我其实唔太清楚你要尐乜,将整条题目贴上嚟,就大家唔驶估下估下...

作者: fitcat07   发布时间: 2013-11-24

can it covert to String firstly?

作者: ceap2003   发布时间: 2013-11-24

Hi hahawingy,

Sorry for mistake, please add a line (i -= j;) as below:

again, It may not work, please verify for yourself
复制内容到剪贴板代码:int[] list = {5, 2, 5, 6, 6, 1};
int[] pat = {6, 1};
int matchCount = 0;
int i, j = 0;

for(i=0; i<list.length; i++) {
if(list == pat[j]) {
j++;
if(j == pat.length) { // matches full pattern
j = 0;
matchCount++;
}
} else {
i -= j;
j = 0;
}
}

作者: assembly.jc   发布时间: 2013-11-24

It should work now.

作者: fitcat07   发布时间: 2013-11-24

热门下载

更多